Registration

Dear SAP Community Member,
In order to fully benefit from what the SAP Community has to offer, please register at:
http://scn.sap.com
Thank you,
The SAP Community team.
Skip to end of metadata
Go to start of metadata

Author: Pratik Mallick
Submitted: 5th August,2008

Description

In the code gallery I have found a number of examples showing how to attach an attachment to an email.  There are examples of multi attachments too. But what the code snippets lack is proper explanation. Most of them are just codes which needs to be copy pasted without proper understanding. I have attempted to simplify the understanding by this simple, basic example of attaching an attachment. In code comments I have tried to explain the requirement of each step. Hope this is appreciated. Thank You.

*CODE*
REPORT  z_pratik_attachment          LINE-SIZE 132
                                     LINE-COUNT 65
                                     NO STANDARD PAGE HEADING.


***********************************************************************
* INTERNAL TABLES                                                                                      *
***********************************************************************

DATA:
      i_objpack   TYPE STANDARD TABLE OF sopcklsti1,
      i_objtext   TYPE STANDARD TABLE OF solisti1,
      i_objbin    TYPE STANDARD TABLE OF solisti1,
      i_receivers TYPE STANDARD TABLE OF somlreci1.




***********************************************************************
* WORK AREAS                                                                                              *
***********************************************************************
DATA: wa_email_doc TYPE sodocchgi1,
      wa_objpack   TYPE sopcklsti1,
      wa_objtext   TYPE solisti1,
      wa_receivers TYPE somlreci1.

***********************************************************************
* CONSTANTS                                                                                                 *
***********************************************************************
CONSTANTS: c_x     TYPE flag   VALUE 'X',
           c_u     TYPE char1  VALUE 'U'.


***********************************************************************
* START-OF-SELECTION                                                                               *
***********************************************************************
START-OF-SELECTION.

***********************************************************************
* END-OF-SELECTION                                                                                   *
***********************************************************************
END-OF-SELECTION.

  wa_email_doc-obj_name = 'Attachment Example'.
  "Mail Subject
  wa_email_doc-obj_descr
  = 'How to attach Multiple Attachments to an Email'.

* Main Mail body Text in itab i_objtext
  wa_objtext+0(36)  = 'We are not going to print this line.'.
  APPEND wa_objtext TO i_objtext.
  CLEAR wa_objtext.
  wa_objtext+0(46)  = 'This is the body of the email. We have skipped'.
  APPEND wa_objtext TO i_objtext.
  CLEAR wa_objtext.
  wa_objtext+0(45)  = ' the first line of the mail and printed from '.
  wa_objtext+45(12)  = 'second line.'.
  APPEND wa_objtext TO i_objtext.
  CLEAR wa_objtext.

* Text for the attachments in itab i_objbin
  wa_objtext+0(21)
  = 'This is attachment 1.'.
  APPEND wa_objtext TO i_objbin.
  CLEAR wa_objtext.
  wa_objtext+0(31) = 'We have got another attachment.'.
  APPEND wa_objtext TO i_objbin.
  CLEAR wa_objtext.
  wa_objtext+0(28) = 'This is attachment number 2.'.
  APPEND wa_objtext TO i_objbin.
  CLEAR wa_objtext.




** Creating the entry for the compressed document
*--(1): Creating entry for the Main Mail body text in itab i_objtext

  " wa_objpack-transf_bin
  " Flag for indicating an obj. to be transported in binary form.
  " If this flag is activated ('X'), the table entry describes an
  " object stored in binary format. The content of the object is in
  " the table CONTENTS_BIN. If the flag is not activated, the object
  " content is in the table CONTENTS_TXT in ASCII format.
  wa_objpack-transf_bin = ' '.
  " Starting index(row) For header information in the itab i_objpack
  wa_objpack-head_start = 1.
  " No of lines for the header information in itab i_objpack
  wa_objpack-head_num   = 1.
  " The row(index) of the itab i_objtext from where the Mail Body starts
  wa_objpack-body_start = 2.  "Skipped the first Line
  " The number of lines in the Mail body
  wa_objpack-body_num   = 2.  "We have two lines from the 2nd row
  " Document type. There are also whole lot of other options
  wa_objpack-doc_type   = 'RAW'.
  wa_objpack-obj_name   = 'MAIN BODY'.
  wa_objpack-obj_descr  = 'MAIL BODY TEXT'.
  wa_objpack-obj_langu  = ' '.
  " In this case one can skip this. Normally ist calculated as
  " no of linex * 255
  wa_objpack-doc_size = ' '.

  APPEND wa_objpack TO i_objpack.
  CLEAR wa_objpack.

*--(2): Creating entry for the Attachment text in itab i_objbin
*----Attachment 1----*
  wa_objpack-transf_bin = c_x.
  wa_objpack-head_start = 2.
  wa_objpack-head_num   = 1.
  wa_objpack-body_start = 1. "Itab i_objbin row number
  wa_objpack-body_num   = 2.
  wa_objpack-doc_size =  510 . "Approximately for 2 Lines-> 255 * 2
  wa_objpack-doc_type   = 'RAW'.
  wa_objpack-obj_name   = 'Attachment 1'.
  wa_objpack-obj_descr  = 'Attachment 1'.
  wa_objpack-obj_langu  = ' '.
  APPEND wa_objpack TO i_objpack.

*--(3): Creating entry for the Attachment text in itab i_objbin
*----Attachment 2----*
  wa_objpack-transf_bin = c_x.
  wa_objpack-head_start = 3.
  wa_objpack-head_num   = 1.
  wa_objpack-body_start = 3.     "Itab i_objbin row number
  wa_objpack-body_num   = 1.
  wa_objpack-doc_size   =  255 . "Approximately for 1 Line
  wa_objpack-doc_type   = 'RAW'.
  wa_objpack-obj_name   = 'Attachment 2'.
  wa_objpack-obj_descr  = 'Attachment 2'.
  wa_objpack-obj_langu  = ' '.
  APPEND wa_objpack TO i_objpack.


*Building the recepient list
** Receipient information
  wa_receivers-receiver = sy-uname.
  wa_receivers-rec_type = 'B'. "To SAP Inbox
  APPEND wa_receivers TO i_receivers.
  CLEAR wa_receivers.

  wa_receivers-receiver = 'abc@gmail.com'.
  wa_receivers-rec_type = c_u.
  APPEND wa_receivers TO i_receivers.
  CLEAR wa_receivers.

* Finally Send the Document
  CALL FUNCTION 'SO_NEW_DOCUMENT_ATT_SEND_API1'
    EXPORTING
      document_data              = wa_email_doc
      put_in_outbox              = 'X'
      commit_work                = 'X'
    TABLES
      packing_list               = i_objpack
      contents_bin               = i_objbin
      contents_txt               = i_objtext
      receivers                  = i_receivers
    EXCEPTIONS
      too_many_receivers         = 1
      document_not_sent          = 2
      document_type_not_exist    = 3
      operation_no_authorization = 4
      parameter_error            = 5
      x_error                    = 6
      enqueue_error              = 7
      OTHERS                     = 8.
  IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  ENDIF.

1 Comment

  1. Guest

    Thanks for this code .. it was really helpful to understand this process at once... 

    I dont know if i can reward points to you for sharing this document.