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
REPORT zabap_send_mail.
PARAMETERS: psubject(40) TYPE c DEFAULT 'Hello',
            p_email(40) TYPE c DEFAULT 'write email address' .
DATA: it_packing_list LIKE sopcklsti1 OCCURS 0 WITH HEADER LINE,
      it_contents LIKE solisti1 OCCURS 0 WITH HEADER LINE,
      it_receivers LIKE somlreci1 OCCURS 0 WITH HEADER LINE,
      it_attachment LIKE solisti1 OCCURS 0 WITH HEADER LINE,
      gd_cnt TYPE i,
      gd_sent_all(1) TYPE c,
      gd_doc_data LIKE sodocchgi1,
      gd_error TYPE sy-subrc.
DATA: it_message TYPE STANDARD TABLE OF solisti1 INITIAL SIZE 0
                  WITH HEADER LINE.
************************************************************************
START-OF-SELECTION.
  PERFORM populate_message_table.
*Send email message, although is not sent from SAP until mail send
*program has been executed(rsconn01)
  PERFORM send_email_message.
*Instructs mail send program for SAPCONNECT to send email(rsconn01)
  PERFORM initiate_mail_execute_program.
*&---------------------------------------------------------------------
*& Form POPULATE_MESSAGE_TABLE
*&---------------------------------------------------------------------
*Adds text to email text table
*----------------------------------------------------------------------
FORM populate_message_table.
  APPEND 'Email line 1' TO it_message.
  APPEND 'Email line 2' TO it_message.
  APPEND 'Email line 3' TO it_message.
  APPEND 'Email line 4' TO it_message.
ENDFORM. " POPULATE_MESSAGE_TABLE
*&---------------------------------------------------------------------
*& Form SEND_EMAIL_MESSAGE
*&---------------------------------------------------------------------
*Send email message
*----------------------------------------------------------------------
FORM send_email_message.
*Fill the document data.
  gd_doc_data-doc_size = 1.

*Populate the subject/generic message attributes
  gd_doc_data-obj_langu = sy-langu.
  gd_doc_data-obj_name = 'SAPRPT'.
  gd_doc_data-obj_descr = psubject.
  gd_doc_data-sensitivty = 'F'.

*Describe the body of the message
  CLEAR it_packing_list.
  REFRESH it_packing_list.
  it_packing_list-transf_bin = space.
  it_packing_list-head_start = 1.
  it_packing_list-head_num = 0.
  it_packing_list-body_start = 1.
  DESCRIBE TABLE it_message LINES it_packing_list-body_num.
  it_packing_list-doc_type = 'RAW'.
  APPEND it_packing_list.

*Add the recipients email address
  CLEAR it_receivers.
  REFRESH it_receivers.
  it_receivers-receiver = p_email.
  it_receivers-rec_type = 'U'.
  it_receivers-com_type = 'INT'.
  it_receivers-notif_del = 'X'.
  it_receivers-notif_ndel = 'X'.
  APPEND it_receivers.

*Call the FM to post the message to SAPMAIL
  CALL FUNCTION 'SO_NEW_DOCUMENT_ATT_SEND_API1'
    EXPORTING
      document_data              = gd_doc_data
      put_in_outbox              = 'X'
    IMPORTING
      sent_to_all                = gd_sent_all
    TABLES
      packing_list               = it_packing_list
      contents_txt               = it_message
      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.

*Store function module return code
  gd_error = sy-subrc.

*Get it_receivers return code
  LOOP AT it_receivers.
  ENDLOOP.
ENDFORM. " SEND_EMAIL_MESSAGE
*&---------------------------------------------------------------------
*& Form INITIATE_MAIL_EXECUTE_PROGRAM
*&---------------------------------------------------------------------
*Instructs mail send program for SAPCONNECT to send email.
*----------------------------------------------------------------------
FORM initiate_mail_execute_program.
  WAIT UP TO 2 SECONDS.
  IF gd_error EQ 0.
    SUBMIT rsconn01 WITH mode = 'INT'
    WITH output = 'X'
    AND RETURN.
  ENDIF.
ENDFORM. " INITIATE_MAIL_EXECUTE_PROGRAM
 
  • No labels