Copyright 2022 - BV TallVision IT

Can an email be sent from SAP ? Set up your mail message to an email address and supply subject, contents and even attachments... (Old style)

Simple mail

There's remarkably little to it. This first example shows how an email can be created and sent via SAP's outbox. This means monitoring transaction SCOT "SAPconnect - Administration" and SWUV "Send R3F Messages Once" which are probably already scheduled on your system.

Example: Sending an email from SAP

An e-mail without attachments can be send by:

  data: wa_docdata like sodocchgi1,
        I_CONTENTS type table of solisti1 with header line,
        I_RECEIVERS type table of somlreci1 with header line.

  clear: I_CONTENTS, I_CONTENTS[], I_RECEIVERS, I_RECEIVERS[].

* Subject line
  wa_docdata-obj_descr = 'Mail (mail generated by SAP $)'.
  replace '$' in wa_docdata-obj_descr with sy-sysid .
* The receiver is an email adress:
  I_RECEIVERS-rec_type = 'U'.
  I_RECEIVERS-com_type = 'INT'.
  I_RECEIVERS-receiver = This email address is being protected from spambots. You need JavaScript enabled to view it.'.
  append I_RECEIVERS.
* Mail contents
  APPEND 'This is a text line in the mail.' to I_CONTENTS.

  IF NOT I_RECEIVERS[] IS INITIAL.
    call function 'SO_NEW_DOCUMENT_SEND_API1'
      EXPORTING
        document_data              = wa_docdata
        document_type              = 'RAW'
        commit_work                = 'X'
      TABLES
        object_content             = I_CONTENTS
        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.
    if sy-subrc <> 0.
*
    endif.
  endif.

Note the function module SO_NEW_DOCUMENT_SEND_API1is called here.

Mail with attachment

Setting up a mail with an attachment is a little more work, as the actual positions in data for the contents and the attachment need to be calculated. Note: there is an alternative to setting up an email with attachment here: More mail.

Example: Issue an email with attachment

  data: wa_docdata like sodocchgi1,
        I_RECEIVERS type table of somlreci1 with header line,
        i_PACKING_LIST type table of sopcklsti1 with header line,
        i_CONTENTS_TXT type table of solisti1 with header line,
* Definitions needed for the data-gathering
        i_ekpo type table of ekpo with header line,
        begin of wa_po_item_line,
          action(6),
          s1,
          ebeln(10),
          s2,
          more(4),
          s3,
        end of wa_po_item_line.

  clear: I_RECEIVERS, I_RECEIVERS[].

* The receiver is an email adress:
  I_RECEIVERS-rec_type = 'U'.
  I_RECEIVERS-com_type = 'INT'.
  I_RECEIVERS-receiver = This email address is being protected from spambots. You need JavaScript enabled to view it.'.
  append I_RECEIVERS.

* Mail contents and attachment are arranged here.
  perform fill_document.

  IF NOT I_RECEIVERS[] IS INITIAL.
    call function 'SO_DOCUMENT_SEND_API1'
      EXPORTING
        document_data              = wa_docdata
        PUT_IN_OUTBOX              = 'X'
        commit_work                = 'X'
      TABLES
        PACKING_LIST               = i_PACKING_LIST
        CONTENTS_TXT               = i_CONTENTS_TXT
        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.
    if sy-subrc <> 0.
* Error handling here !
    endif.
  endif.

The routine Fill_document takes care of the Subject line (or mail title), the contents and the attachment.

*----------------------------------------------------------------------
*  Form  fill_document
*----------------------------------------------------------------------
*  Fill document/mail (format and content)
*----------------------------------------------------------------------
FORM fill_document .

  data:   l_tab_lines type i,
          l_tab_lines_2 type i,
          l_username like BAPIBNAME-BAPIBNAME,
          l_address like BAPIADDR3,
          i_return type table of bapiret2.

* Subject and Description
  wa_docdata-obj_name  = 'TEST_TXT'.
  wa_docdata-obj_descr = 'Purchase Order update of '
    &'Purchase Order update of creations/changes/deletions'(m00).

* Contents block of the mail
  append 'Dear madam, sir,'(m01) to i_CONTENTS_TXT.
  append space to i_CONTENTS_TXT.
  append 'In the enclosed attachment you will find information '
    &'about Purchase Orders'(m02) to i_CONTENTS_TXT.
  append space to i_CONTENTS_TXT.
  append 'Kind regards,'(m03) to i_CONTENTS_TXT.
  append space to i_CONTENTS_TXT.
  i_CONTENTS_TXT = 'Please do not reply to this mail, it was generated'
      &'from SAP $ at IBM'.
  replace '$' in i_CONTENTS_TXT with sy-sysid.
  append i_CONTENTS_TXT.

* Write Packing List (Main)
  clear: i_PACKING_LIST, i_PACKING_LIST[].
  describe table i_CONTENTS_TXT lines l_tab_lines.
  read     table i_CONTENTS_TXT index l_tab_lines.
  wa_docdata-doc_size =
    ( l_tab_lines - 1 ) * 255 + strlen( i_CONTENTS_TXT ).
  clear i_PACKING_LIST-transf_bin.
  i_PACKING_LIST-head_start = 1.
  i_PACKING_LIST-head_num   = 0.
  i_PACKING_LIST-body_start = 1.
  i_PACKING_LIST-body_num   = l_tab_lines.
  i_PACKING_LIST-doc_type   = 'RAW'.
  append i_PACKING_LIST.

*----------------------------------------------------------------------
* Create Message Attachment - header line
  append 'Action|PO number |More|More|More '(m04) to i_CONTENTS_TXT.

* Trailer record.
  loop at i_ekpo.
    clear: wa_po_item_line.
* Seperators
    move '|' to: wa_po_item_line-s1, wa_po_item_line-s2,
      wa_po_item_line-s3.

    wa_po_item_line-action = 'UPDATE'.
    wa_po_item_line-ebeln = i_ekpo-ebeln.
*    unpack l_quantity           to i_po_item_line-quantity.

    append wa_po_item_line to i_CONTENTS_TXT.
  endloop.

* Last line
* concatenate c_last_line c_plus into i_CONTENTS_TXT.

*   Write Packing List (Attachment)
  describe table i_CONTENTS_TXT lines l_tab_lines_2.
  read     table i_CONTENTS_TXT index l_tab_lines_2.
  i_PACKING_LIST-doc_size =
    ( l_tab_lines_2 - 1 ) * 255 + strlen( i_CONTENTS_TXT ).
  i_PACKING_LIST-head_start = l_tab_lines + 1.
  i_PACKING_LIST-head_num   = l_tab_lines_2 - l_tab_lines.
  i_PACKING_LIST-body_start = l_tab_lines + 1.
  i_PACKING_LIST-body_num   = l_tab_lines_2 - l_tab_lines.
  i_PACKING_LIST-doc_type   = 'RAW'.
  i_PACKING_LIST-obj_name   = 'ATTACHMENT'.
  i_PACKING_LIST-obj_descr = 'filename'(m05).
  append i_PACKING_LIST.

ENDFORM.                    " fill_document

Note the function module SO_DOCUMENT_SEND_API1 is called here.