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

Back to Sending Mails - Home Page

I have seen many approaches for sending mail with attachment. Here i am writing a function module to send mail with attachment using object oriented approach. The use of this approach would eliminate bulky coding.

Error rendering macro 'code': Invalid value specified for parameter 'lang'
FUNCTION ZNPI_SEND_MAIL.
*"----------------------------------------------------------------------
*"*"Local Interface:
*"  IMPORTING
*"     VALUE(SUBJECT) TYPE  SO_OBJ_DES
*"     VALUE(MESSAGE_BODY) TYPE  BCSY_TEXT
*"     VALUE(ATTACHMENTS) TYPE  RMPS_T_POST_CONTENT OPTIONAL
*"     VALUE(SENDER_UID) TYPE  SYUNAME OPTIONAL
*"     VALUE(RECIPIENT_UID) TYPE  SYUNAME OPTIONAL
*"     VALUE(SENDER_MAIL) TYPE  ADR6-SMTP_ADDR OPTIONAL
*"     VALUE(RECIPIENT_MAIL) TYPE  ADR6-SMTP_ADDR OPTIONAL
*"  EXPORTING
*"     VALUE(RESULT) TYPE  BOOLEAN
*"  TABLES
*"      RECIPIENTS STRUCTURE  UIYS_IUSR OPTIONAL
*"----------------------------------------------------------------------
*Data Declaration 
  DATA: lo_sender TYPE REF TO if_sender_bcs VALUE IS INITIAL,
          l_send type ADR6-SMTP_ADDR ,
          l_rec type  ADR6-SMTP_ADDR .
   Data : ITAB TYPE TABLE OF SVAL,
          LS_ITAB TYPE SVAL,
          i_RETURN.
     DATA:
     lo_send_request TYPE REF TO cl_bcs VALUE IS INITIAL.
*     DATA:
*    lt_message_body TYPE bcsy_text VALUE IS INITIAL,
     DATA: lx_document_bcs TYPE REF TO cx_document_bcs VALUE IS INITIAL,
     attachment_subject type SO_OBJ_DES.
 
   DATA: lo_recipient TYPE REF TO if_recipient_bcs VALUE IS INITIAL.
   data: ls_recipient like line of recipients,
   ls_attachment like line of attachments.
   DATA: lv_recipient_uid TYPE uname,
         lv_recipient_mail TYPE adr6-smtp_addr.

*Prepare Mail Object 
     CLASS cl_bcs DEFINITION LOAD.
     lo_send_request = cl_bcs=>create_persistent( ).
* Message body and subject
 data: lo_document TYPE REF TO cl_document_bcs VALUE IS INITIAL.
     lo_document = cl_document_bcs=>create_document(
     i_type = 'RAW'
     i_text =  message_body
     i_subject = subject ).
 
 
 
*Send  attachment 
 loop at attachments into ls_attachment.
     attachment_subject = ls_attachment-subject.
     TRY.
         lo_document->add_attachment(
         EXPORTING
         i_attachment_type = ls_attachment-OBJTP
         i_attachment_subject = attachment_subject
 
         i_att_content_hex = ls_attachment-CONT_HEX ).
       CATCH cx_document_bcs INTO lx_document_bcs.
     ENDTRY.
 endloop.
 
* Pass the document to send request 
     lo_send_request->set_document( lo_document ).
 try.
     if sender_mail is not initial.
       lo_sender = cl_cam_address_bcs=>create_internet_address( sender_mail ).
           elseif sender_uid is not initial.
       lo_sender = cl_sapuser_bcs=>create( sender_uid ).
     else.
       lo_sender = cl_sapuser_bcs=>create( sy-uname ).
     endif.
* Set sender 
     lo_send_request->set_sender(
     EXPORTING
     i_sender = lo_sender ).
        catch CX_ADDRESS_BCS.
          return.
        endtry.
* Set  recipients 
 if recipients[] is initial.
 
     if recipient_mail is not initial.
       lo_recipient = cl_cam_address_bcs=>create_internet_address( recipient_mail ).
           elseif recipient_uid is not initial.
       lo_recipient = cl_sapuser_bcs=>create( recipient_uid ).
     else.
       lo_recipient = cl_sapuser_bcs=>create( sy-uname ).
     endif.
 
     lo_send_request->add_recipient(
     EXPORTING
     i_recipient = lo_recipient
     i_express = 'X' ).
 else.
 
 loop at recipients into ls_recipient.
     if ls_recipient-iusrid is not initial.
       lv_recipient_uid = ls_recipient-iusrid.
       lo_recipient = cl_sapuser_bcs=>create( lv_recipient_uid ).
     elseif ls_recipient-email is not initial.
       lv_recipient_mail = ls_recipient-email .
       lo_recipient = cl_cam_address_bcs=>create_internet_address( lv_recipient_mail ).
     endif.
 
 lo_send_request->add_recipient(
     EXPORTING
     i_recipient = lo_recipient
     i_express = 'X' ).
 
 endloop.
 endif.
 try.
** Send email 
     lo_send_request->send(
     EXPORTING
     i_with_error_screen = 'X'
     RECEIVING
     result = result ).
     COMMIT WORK.
     wait up to 1 seconds.
catch cx_send_req_bcs.
      result = ''.
endtry.

ENDFUNCTION.
  • No labels

10 Comments

  1. Guest

    Very nice code.

  2. May i ask 1 question, if the result = 'X'. How can i check what is the problem in this function ?

  3. Hello,

    Is it possible to use this code to send a multiple worsheet Excel file?

    Thanks in advance!

    Emanuel

  4. Guest

    How do you populate  table ATTACHMENTS for a pdf file?

      1. Hi Jerry,

         

        the linked site doesn't show how the table ATTACHMENTS should be populated...

        I'm just receiving errors about the type of RMPS_T_POST_CONTENT variables.

        Best regards
        Seb

  5. Guest

    I need to Send e-mail with Multiple line times for a Sales order(Containing Multiple Line items) created for particular Vendor.

    Also, i require to keep Vendor in 'TO'  and the 'StakeHolder' in CC of e-mail.

     Expecting a positive reply from you guys.

    Thanks & Regards,

    Ajay

  6. Hi Monalisa. 

    Many thanks for the help.  

    Juan

  7. Dear Monalisa,

    Good job ...

    I want to ask a question.

    Think of almost 5 mail adress like (a1@sss.com, ...............a5@sss.com)

    How can I send this mails ?

    (I mean think of 'TO' section and looking like a1@sss.com, a2@sss.com, a3@sss.com, a4@sss.com, a5@sss.com)

    Regards

    Tanju