ABAP Program to Send Mail with Attachment

ABAP Program to Send Mail with Attachment

Here are two ABAP Programs to send mail with and without attachment.

ABAP Program to send mail with Attachment

This ABAP program uses the function module ‘SO_NEW_DOCUMENT_ATT_SEND_API1’ to send the email with an attachment. There are five steps involved in sending the email with an attachment.

  1. Add Recipients
  2. Put in the mail contents
  3. Create the attachment
  4. Pack the mail contents and attachment
  5. Finally, send the mail out.

Before we start writing the code, ensure that everything is configured correctly in the SCOT. Get in touch with the basis team and they will help you configure it.

Declare the following

DATA: lt_mailrecipients  TYPE STANDARD TABLE OF somlrec90 WITH HEADER LINE,
      lt_mailtxt         TYPE STANDARD TABLE OF soli      WITH HEADER LINE,
      lt_attachment      TYPE STANDARD TABLE OF solisti1  WITH HEADER LINE,
      lt_mailsubject     TYPE sodocchgi1,
      lt_packing_list    TYPE STANDARD TABLE OF sopcklsti1 WITH HEADER LINE,
      gv_cnt             TYPE i.

Now, lets start formatting the mail.

Add Recipients

lt_mailrecipients-rec_type  = 'U'.
lt_mailrecipients-com_type  = 'INT'.
lt_mailrecipients-receiver  = '[email protected]'.
APPEND lt_mailrecipients .
CLEAR lt_mailrecipients .

Put in the Mail Contents

lt_mailtxt = 'Hi How are you'.      APPEND lt_mailtxt. CLEAR lt_mailtxt.
lt_mailtxt = 'Here is a test mail'. APPEND lt_mailtxt. CLEAR lt_mailtxt.
lt_mailtxt = 'Thanks'.              APPEND lt_mailtxt. CLEAR lt_mailtxt.

Create the attachment

  DATA: BEGIN OF lt_po_data_cons OCCURS 0,
         ebeln like ekpo-ebeln,
         ebelp like ekpo-ebelp,
        END OF lt_po_data_cons.

  SELECT ebeln ebelp INTO TABLE lt_po_data_cons
  UP TO 10 ROWS
  FROM ekpo.   

  CLASS cl_abap_char_utilities DEFINITION LOAD.
  CONCATENATE 'PO' 'PO Line'
              INTO lt_attachment SEPARATED BY
              cl_abap_char_utilities=>horizontal_tab.
  APPEND lt_attachment. CLEAR lt_attachment.

  LOOP AT lt_po_data_cons.
  CONCATENATE lt_po_data_cons-ebeln lt_po_data_cons-ebelp
              INTO lt_attachment SEPARATED BY
              cl_abap_char_utilities=>horizontal_tab.

  CONCATENATE cl_abap_char_utilities=>newline lt_attachment
              INTO lt_attachment.

  APPEND lt_attachment. CLEAR lt_attachment.
 ENDLOOP.

Pack the mail contents and attachment

  lt_packing_list-transf_bin  = space.
  lt_packing_list-head_start  = 1.
  lt_packing_list-head_num    = 0.
  lt_packing_list-body_start  = 1.
  lt_packing_list-body_num    = LINES( lt_mailtxt ).
  lt_packing_list-doc_type    = 'RAW'.
  APPEND lt_packing_list. CLEAR lt_packing_list.

  lt_packing_list-transf_bin  = 'X'.
  lt_packing_list-head_start  = 1.
  lt_packing_list-head_num    = 1.
  lt_packing_list-body_start  = 1.
  lt_packing_list-body_num    = LINES( lt_attachment ).
  lt_packing_list-doc_type    = 'XLS'. " You can give RAW incase if you want just a txt file.
  lt_packing_list-obj_name    = 'data.xls'.
  lt_packing_list-obj_descr   = 'data.xls'.
  lt_packing_list-doc_size    = lt_packing_list-body_num * 255.
  APPEND lt_packing_list. CLEAR lt_packing_list.

  lt_mailsubject-obj_name     = 'MAILATTCH'.
  lt_mailsubject-obj_langu    = sy-langu.
  lt_mailsubject-obj_descr    = 'You have got mail'.
  lt_mailsubject-sensitivty   = 'F'.
  gv_cnt = LINES( lt_attachment ).
  lt_mailsubject-doc_size     = ( gv_cnt - 1 ) * 255 + STRLEN(
  lt_attachment ).

Finally, send the mail out.
That’s it. You are all done. Just call the function module to send the mail out.

CALL FUNCTION 'SO_NEW_DOCUMENT_ATT_SEND_API1'
    EXPORTING
      document_data              = lt_mailsubject
    TABLES
      packing_list               = lt_packing_list
      contents_bin               = lt_attachment
      contents_txt               = lt_mailtxt
      receivers                  = lt_mailrecipients
    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.
  IF sy-subrc EQ 0.
    COMMIT WORK.
    SUBMIT rsconn01 WITH mode = 'INT' AND RETURN.
  ENDIF.

ABAP Program to send email (without attachments)

* Data Declarations
DATA: lt_mailsubject     TYPE sodocchgi1.
DATA: lt_mailrecipients  TYPE STANDARD TABLE OF somlrec90 WITH HEADER LINE.
DATA: lt_mailtxt         TYPE STANDARD TABLE OF soli      WITH HEADER LINE.
* Recipients
lt_mailrecipients-rec_type  = 'U'.
lt_mailrecipients-receiver = '[email protected]'.
APPEND lt_mailrecipients .
CLEAR lt_mailrecipients .
* Subject.
lt_mailsubject-obj_name = 'TEST'.
lt_mailsubject-obj_langu = sy-langu.
lt_mailsubject-obj_descr = 'Mail Subject'.
* Mail Contents
lt_mailtxt = 'This is a test mail'.
APPEND lt_mailtxt. CLEAR lt_mailtxt.
* Send Mail
CALL FUNCTION 'SO_NEW_DOCUMENT_SEND_API1'
  EXPORTING
    document_data              = lt_mailsubject
  TABLES
    object_content             = lt_mailtxt
    receivers                  = lt_mailrecipients
  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.
IF sy-subrc EQ 0.
  COMMIT WORK.

*   Push mail out from SAP outbox
  SUBMIT rsconn01 WITH mode = 'INT' AND RETURN.
ENDIF.

check ABAP tutorials for more.

ERP Avatar

11 responses to “ABAP Program to Send Mail with Attachment”

  1. naren Avatar

    hi can u give the selection screen details for this code……

  2. Vishwanath Avatar
    Vishwanath

    Hi, can you please provide basic programs to learn in detail with explanation. I am beginner in ABAP. Thanks in advance..

  3. yup when multiple fields Avatar

    When I initially commented I clicked the “Notify me when new comments are added” checkbox and now each
    time a comment is added I get four e-mails with the same comment.
    Is there any way you can remove me from that service?
    Thank you!

  4. Cheapest SR22 insurance Avatar

    This post was precisely what I was searching for.
    Can not stand by to dig deeper in to your blog.

  5. c6705118554500652901 Avatar

    Its like you read my mind! You appear to know a lot about this, like you wrote the book in it or something.
    I think that you can do with some pics to drive the message home
    a little bit, but other than that, this is magnificent blog.
    A fantastic read. I’ll certainly be back.

  6. Auto insurance quotes Avatar

    Since getting cheap auto insurance coverage, I have actually believed even more
    economically stable. It behaves certainly not to fret about high
    insurance coverage bills.

  7. Sammy Avatar

    Everything is very open with a very clear description of the issues.

    It was definitely informative. Your site is very useful.
    Thank you for sharing!

  8. online assignment scammer Avatar

    Does your blog have a contact page? I’m
    having trouble locating it but, I’d like to shoot you an e-mail.
    I’ve got some suggestions for your blog you might be interested in hearing.
    Either way, great blog and I look forward to seeing it develop over time.

  9. yupoo vendors Avatar

    Hi there all, here every person is sharing these kinds of
    know-how, so it’s good to read this weblog, and I used to pay
    a visit this web site every day.

  10. ?????? ?????? Avatar

    Simply wish to say your article is as astonishing.
    The clarity in your post is just excellent and i could assume you are an expert on this subject.
    Well with your permission let me to grab your RSS feed to keep up to date with forthcoming post.
    Thanks a million and please keep up the enjoyable work.

Leave a Reply to Vishwanath Cancel reply

Your email address will not be published. Required fields are marked *