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

Sending Mails - Home Page

Hi,

https://forums.sdn.sap.com/thread.jspa?messageID=3065528&#30655283065528

https://forums.sdn.sap.com/thread.jspa?messageID=2959234&#29592342959234

https://forums.sdn.sap.com/thread.jspa?messageID=2571717&#25717172571717

https://forums.sdn.sap.com/thread.jspa?messageID=2989716&#29897162989716

https://forums.sdn.sap.com/thread.jspa?messageID=2933362&#29333622933362

 There are many threads like this to send external and internal emails from SAP. I thought to consolidate them. So, here is the code to send email from SAP with and with out attachments.

Error rendering macro 'code': Invalid value specified for parameter 'lang'
REPORT zemail.

DATA: itcpo LIKE itcpo,
      tab_lines LIKE sy-tabix.

* Variables for EMAIL functionality
DATA: maildata   LIKE sodocchgi1.
DATA: mailpack   LIKE sopcklsti1 OCCURS 2 WITH HEADER LINE.
DATA: mailhead   LIKE solisti1 OCCURS 1 WITH HEADER LINE.
DATA: mailbin    LIKE solisti1 OCCURS 10 WITH HEADER LINE.
DATA: mailtxt    LIKE solisti1 OCCURS 10 WITH HEADER LINE.
DATA: mailrec    LIKE somlrec90 OCCURS 0  WITH HEADER LINE.
DATA: solisti1   LIKE solisti1 OCCURS 0 WITH HEADER LINE.


PERFORM send_form_via_email.


************************************************************************
*       FORM  SEND_FORM_VIA_EMAIL                                      *
************************************************************************
FORM  send_form_via_email.

  CLEAR:    maildata, mailtxt, mailbin, mailpack, mailhead, mailrec.
  REFRESH:  mailtxt, mailbin, mailpack, mailhead, mailrec.

* Creation of the document to be sent File Name
  maildata-obj_name = 'TEST'.
* Mail Subject
  maildata-obj_descr = 'Subject'.

* Mail Contents
  mailtxt-line = 'Here is your file'.
  APPEND mailtxt.

* Prepare Packing List
  PERFORM prepare_packing_list.

* Set recipient - email address here!!!
  mailrec-receiver = 'you@yourcompany.com'.
  mailrec-rec_type  = 'U'.
  APPEND mailrec.

* Sending the document
  CALL FUNCTION 'SO_NEW_DOCUMENT_ATT_SEND_API1'
    EXPORTING
      document_data              = maildata
      put_in_outbox              = ' '
    TABLES
      packing_list               = mailpack
      object_header              = mailhead
      contents_bin               = mailbin
      contents_txt               = mailtxt
      receivers                  = mailrec
    EXCEPTIONS
      too_many_receivers         = 1
      document_not_sent          = 2
      operation_no_authorization = 4
      OTHERS                     = 99.

  IF sy-subrc = 0.
    SUBMIT rsconn01 WITH mode = 'INT' AND RETURN.
  ENDIF.

ENDFORM.                    "send_form_via_email

************************************************************************
*      Form  PREPARE_PACKING_LIST
************************************************************************
FORM prepare_packing_list.

  CLEAR:    mailpack, mailbin, mailhead.
  REFRESH:  mailpack, mailbin, mailhead.

  DESCRIBE TABLE mailtxt LINES tab_lines.
  READ TABLE mailtxt INDEX tab_lines.
  maildata-doc_size = ( tab_lines - 1 ) * 255 + strlen( mailtxt ).

* Creation of the entry for the compressed document
  CLEAR mailpack-transf_bin.
  mailpack-head_start = 1.
  mailpack-head_num = 0.
  mailpack-body_start = 1.
  mailpack-body_num = tab_lines.
  mailpack-doc_type = 'RAW'.
  APPEND mailpack.

  mailhead = 'TEST.TXT'.
  APPEND mailhead.



* File 1
  mailbin = 'This is file 1'.
  APPEND mailbin.

  DESCRIBE TABLE mailbin LINES tab_lines.

  mailpack-transf_bin = 'X'.
  mailpack-head_start = 1.
  mailpack-head_num = 1.
  mailpack-body_start = 1.
  mailpack-body_num = tab_lines.
  mailpack-doc_type = 'TXT'.
  mailpack-obj_name = 'TEST1'.
  mailpack-obj_descr = 'Subject'.
  mailpack-doc_size = tab_lines * 255.
  APPEND mailpack.



*File 2
  mailbin = 'This is file 2'.
  APPEND mailbin.

  DATA: start TYPE i.
  DATA: end TYPE i.

  start = tab_lines + 1.

  DESCRIBE TABLE mailbin LINES end.

  mailpack-transf_bin = 'X'.
  mailpack-head_start = 1.
  mailpack-head_num = 1.
  mailpack-body_start = start.
  mailpack-body_num = end.
  mailpack-doc_type = 'TXT'.
  mailpack-obj_name = 'TEST2'.
  mailpack-obj_descr = 'Subject'.
  mailpack-doc_size = tab_lines * 255.
  APPEND mailpack.


ENDFORM.                    "prepare_packing_list

*With PDF Attachment:*
CALL FUNCTION 'SSF_FUNCTION_MODULE_NAME'
  EXPORTING
    formname = 'Z_TEST'
  IMPORTING
    fm_name  = v_fname.

CALL FUNCTION v_fname
  EXPORTING
    control_parameters = x_ctrl_p
  IMPORTING
    job_output_info    = x_output_data.

CALL FUNCTION 'CONVERT_OTF'
  EXPORTING
    format                = 'PDF'
    max_linewidth         = 134
  IMPORTING
    bin_filesize          = v_size
  TABLES
    otf                   = x_output_data-otfdata
    lines                 = it_lines
  EXCEPTIONS
    err_max_linewidth     = 1
    err_format            = 2
    err_conv_not_possible = 3
    OTHERS                = 4.

CALL FUNCTION 'SX_TABLE_LINE_WIDTH_CHANGE'
  EXPORTING
    line_width_dst              = 255
  TABLES
    content_in                  = it_lines
    content_out                 = it_soli
  EXCEPTIONS
    err_line_width_src_too_long = 1
    err_line_width_dst_too_long = 2
    err_conv_failed             = 3
    OTHERS                      = 4.

CALL FUNCTION 'FUNC_CONVERT_DATA_ODC01'
  EXPORTING
    iv_byte_mode = 'X'
  TABLES
    it_data      = it_lines
    et_data      = it_table.

*-----To caluculate total number of lines of internal table
DESCRIBE TABLE it_table LINES v_lines.

*-----Create Message Body and Title and Description
it_mess = 'successfully converted smartform from otf format to pdf' .
APPEND it_mess.

wa_doc_data-obj_name = 'smartform'.
wa_doc_data-expiry_dat = sy-datum + 10.
wa_doc_data-obj_descr = 'smartform'.
wa_doc_data-sensitivty = 'F'.
wa_doc_data-doc_size = v_lines * 255.
APPEND it_pcklist.

*-----PDF Attachment
it_pcklist-transf_bin = 'X'.
it_pcklist-head_start = 1.
it_pcklist-head_num = 0.
it_pcklist-body_start = 1.
it_pcklist-doc_size = v_lines_bin * 255 .
it_pcklist-body_num = v_lines.
it_pcklist-doc_type = 'PDF'.
it_pcklist-obj_name = 'smartform'.
it_pcklist-obj_descr = 'smart_desc'.
it_pcklist-obj_langu = 'E'.
it_pcklist-doc_size = v_lines * 255.
APPEND it_pcklist.

*-----Giving the receiver email-id
CLEAR it_receivers.
it_receivers-receiver = 'abcd@yahoo.com'.
it_receivers-rec_type = 'U'.
APPEND it_receivers.

*-----Calling the function module to sending email
CALL FUNCTION 'SO_NEW_DOCUMENT_ATT_SEND_API1'
  EXPORTING
    document_data              = wa_doc_data
    put_in_outbox              = 'X'
    commit_work                = 'X'
  TABLES
    packing_list               = it_pcklist
    contents_txt               = it_mess
    contents_hex               = it_table
    receivers                  = it_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.

2 Comments

  1. Please remember to format your code and of course LABEL it otherwise I will not be able to assign any points for putting this all together (sad)

  2. Rich Heilman never ceases to amaze me this is one of his from his bag of tricks - he's got like 200+ in there thanks Vinnie for putting it online for us. Now to get Rich to do the rest as well!