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

Send Message to External email id and SAP User id via ABAP

by P surjith kumar, Enteg Infotech, Bangalore, India.


 In a Function Module the email id and SAP User id has to enter as an input. On successful execution it should send a mail to external Email id and SAP Inbox.

Creating Mail sending Function Module and Execution Step by Step

    The following steps are necessary the Creation.
1.    Log to Transaction Code SE37.
2.    Create a Function Module.
3.    Declare the input variables for external email id and SAP user id.
4.    Write the coding as shown below.
5.    Save and Active it.
6.    Execute the Function Module.
7.    Enter the Input i.e. External email id and SAP User id.
8.    See the output in SAP In box (SBWP) of SAP User id and SAP Connect (SOST) for external email id.

Where this can be used?

1.  In the place where the mail need to send while execute a report.
2.  In the Workflow Task you can call this method to send Mails.
3.  In Workflow 'Send Mail' step is available to send mail, but if you want to send lengthy website link and more contents in the Email via this program you can send.

1. Log to Transaction Code SE37.


2. Create a Function Module.

2.1 Create the Function group as indicated below. 

2.2 Give the Function Group Name as mention below.

Click the save button to create function group.

2.3 Give the Function Module Name "ZSUR_MAIL_SENDING_PRG"  and the Function Group Name.

 Click the save button to create function module.

3. Declare the input variables for external email id and SAP user id.


4. Write the coding as depicted below.


FUNCTION zsur_mail_sending_prg.
""Local Interface:
* Created by: P.Surjith Kumar, Enteg InfoTech, Bangalore, India.
* Created on: 03-11-2008
* Purpose   : Sending Mail for the Respective Person's SAP Inbox and External Email id.
* *&Get the Email id and User id Whom you want to Send  ******
  DATA:it_receivers    TYPE STANDARD TABLE OF  somlreci1,
       wa_it_receivers LIKE LINE OF it_receivers,
       it_packing_list TYPE STANDARD TABLE OF  sopcklsti1,
       gd_doc_data     TYPE sodocchgi1,
       wa_it_packing_list LIKE LINE OF  it_packing_list,
       psubject(90)       TYPE c,
       it_message         TYPE STANDARD TABLE OF solisti1,
       wa_it_message      LIKE LINE OF it_message,
       c1(99)    TYPE c,
       c2(15)    TYPE c,
       num_lines TYPE i.
&-- Assign the Email id and User id to  Whom you want to Send  -------------&
  FREE wa_it_receivers.
  wa_it_receivers-receiver   = lv_emp_emailid. "&---- Assign Email id
  wa_it_receivers-rec_type   = 'U'.                    "&---- Send to External Email id
  wa_it_receivers-com_type   = 'INT'.
  wa_it_receivers-notif_del  = 'X'.
  wa_it_receivers-notif_ndel = 'X'.
  APPEND wa_it_receivers TO it_receivers .
  FREE wa_it_receivers.
  wa_it_receivers-receiver   = lv_emp_userid.  "&----- Assign SAP User Id
  wa_it_receivers-rec_type   = 'B'.                    "&-- Send to SAP Inbox
  wa_it_receivers-com_type   = 'INT'.
  wa_it_receivers-notif_del  = 'X'.
  wa_it_receivers-notif_ndel = 'X'.
  APPEND wa_it_receivers TO it_receivers .
*& - END of  Assign the Email id and User id to  Whom you want to Send  --&
"&--- Read the Number of lines in the Internal Table
DESCRIBE TABLE it_receivers LINES num_lines.  
"&--- Check the Sender Email id or SAP User id is got or not.
IF num_lines IS NOT INITIAL.                  
* Add thetext to mail text table
*&-- Subject of the mail -------------&*
psubject = 'Send Mail from ABAP Program.'(001).
&--  Body  of the mail ----------------&*
CLEAR wa_it_message.
c1 = 'Dear'(005).
c2 = lv_emp_userid.
    CONCATENATE c1 c2 ',' INTO
    wa_it_message-line SEPARATED BY space.
APPEND wa_it_message TO it_message.
*** insert Blank Line *********************************************
CLEAR wa_it_message.
wa_it_message-line = '                               '.
APPEND wa_it_message TO it_message.
******* Assign your Text  below *************************************
CLEAR wa_it_message.
wa_it_message-line = 'A Test Mail sent from "Enteg InfoTech" through ABAP Program.'(002).
APPEND wa_it_message TO it_message.
*** insert Blank Line{} *********************************************
CLEAR wa_it_message.
wa_it_message-line = '                                        '.
APPEND wa_it_message TO it_message.
**********Assign your Text  below ********************************
CLEAR wa_it_message.
wa_it_message-line = 'This mail generate automatically. Please do not reply.'(003).
   APPEND wa_it_message TO it_message.
**********& Send EMAIL MESSAGE  &********************************* 
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 wa_it_packing_list.
REFRESH it_packing_list.
wa_it_packing_list-transf_bin = space.
wa_it_packing_list-head_start = 1.
wa_it_packing_list-head_num = 0.
wa_it_packing_list-body_start = 1.
DESCRIBE TABLE it_message LINES wa_it_packing_list-body_num.
wa_it_packing_list-doc_type = 'RAW'.
APPEND wa_it_packing_list TO it_packing_list.
*&------ Call the Function Module to send the message to External and SAP Inbox
    document_data                   = gd_doc_data
   put_in_outbox                    = 'X'
   commit_work                      = 'X'
    packing_list                    = it_packing_list
   contents_txt                     = it_message
   receivers                        = it_receivers
   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. "&---- END of Check the Sender Email id or SAP User id is got or not.


5. Save and Active it.


6. Execute the Function Module.



7. Enter the Input i.e. External email id & SAP User id and execute the Function Module


8.1. See the output in SAP In box (SBWP) of SAP User id.

8.2 See the output in SAP Connect (SOST) for external email id.