Sending a SAP Message as E-Mail

Using the sample function module below, you can send a SAP message (SE91) as E-Mail. The short text will be transformed into the subject, and long text will be sent as the body of the E-Mail message.


This function also supports sending file attachments; so it is suitable for WorkFlow purposes.


function zipyy_send_msg_as_email.


*”*”Local Interface:


















      lt_cont  type standard table of solisti1 with header line,

      lt_rece  type standard table of somlreci1 with header line,

      lf_docdat type sodocchgi1,


      begin of lf_split,

        len type i,

        pos1 type i,

        pos2 type i,

        plen type i,


      end of lf_split,


      lv_class type dokhl-object,

      lv_msgnr type char3,

      lv_langu type char1,

      lv_text  type string.



        lo_email type ref to cl_bcs,

        lo_docum type ref to cl_document_bcs,

        lo_sende type ref to cl_sapuser_bcs,

        lo_recip type ref to if_recipient_bcs,


        lt_attx type soli_tab,

        lt_text type soli_tab with header line,

        lv_subj type so_obj_des,


        lv_atsiz type sood-objlen,

        lv_statu type bcs_stml,

        lv_rstat type bcs_rqst,

        lv_saddr type adr6-smtp_addr,

        lv_uname type uname.






  lf_docdat-obj_name  = ‘SAPOFFICE’.


  call function ‘MESSAGE_TEXT_BUILD’


      msgid               = i_msgid

      msgnr               = i_msgno

      msgv1               = i_msgv1

      msgv2               = i_msgv2

      msgv3               = i_msgv3

      msgv4               = i_msgv4


      message_text_output = lf_docdat-obj_descr.


  lv_class = i_msgid.

  lv_msgnr = i_msgno.

  lv_langu = sy-langu.




      msg_class = lv_class

      msg_nr    = lv_msgnr

      language  = lv_langu


      text      = lv_text.


  replace all occurrences of:

    ‘&V1&’ in lv_text with i_msgv1,

    ‘&V2&’ in lv_text with i_msgv2,

    ‘&V3&’ in lv_text with i_msgv3,

    ‘&V4&’ in lv_text with i_msgv4.


  clear lf_split.

  lf_split-con = ‘X’.

  lf_split-len = strlen( lv_text ).


    lf_split-pos1 = lf_split-pos2.

    add 255 to lf_split-pos2.

    if lf_split-pos2 gt lf_split-len.

      lf_split-pos2 = lf_split-len.

      clear lf_split-con.



    lf_split-plen = lf_split-pos2 – lf_split-pos1.

    lt_cont-line = lv_text+lf_split-pos1(lf_split-plen).

    append lt_cont.


    if lf_split-con is initial.





* İletiyi gönderelim

  if i_memai eq ‘X’.

    clear lt_rece.


    select single smtp_addr into lt_rece-receiver

    from adr6

    where addrnumber eq ( select addrnumber from usr21

                          where bname eq i_bname )

      and persnumber eq   ( select persnumber from usr21

                          where bname eq i_bname ).

    lt_rece-rec_type = ‘U’.

    append lt_rece.



  if i_mesap eq ‘X’.

    clear lt_rece.


    lt_rece-receiver = i_bname.

    lt_rece-rec_type = ‘B’.

    lt_rece-express  = ‘X’.

    append lt_rece.



* Send mail

  lo_email = cl_bcs=>create_persistent( ).


  call function ‘SO_RAW_TO_RTF’


      objcont_old = lt_cont

      objcont_new = lt_text.


  lv_subj = lf_docdat-obj_descr.

  lo_docum = cl_document_bcs=>create_document(

                                    i_type    = ‘RAW’

                                    i_text    = lt_text[]

                                    i_subject = lv_subj ).


  if not i_attxt[] is initial.

    call function ‘SO_RAW_TO_RTF’


        objcont_old = i_attxt

        objcont_new = lt_attx.


    call method lo_docum->add_attachment


        i_attachment_type    = i_attyp

        i_attachment_subject = i_atsub

        i_att_content_text   = lt_attx[].



  call method lo_email->set_document( lo_docum ).

  lo_sende = cl_sapuser_bcs=>create( sy-uname ).


  call method lo_email->set_sender


      i_sender = lo_sende.


  loop at lt_rece.


    case lt_rece-rec_type.

      when ‘U’.

        lv_saddr = lt_rece-receiver.


        translate lv_saddr to lower case.

        lo_recip = cl_cam_address_bcs=>create_internet_address(

                    lv_saddr ).

      when ‘B’.

        lv_uname = lt_rece-receiver.

        lo_recip = cl_sapuser_bcs=>create( lv_uname ).




    call method lo_email->add_recipient


        i_recipient  = lo_recip

        i_express    = ‘X’

        i_copy       = ‘ ‘

        i_blind_copy = ‘ ‘

        i_no_forward = ‘ ‘.



  move ‘N’ to lv_rstat.

  lv_statu = lv_rstat.


  call method lo_email->set_status_attributes


      i_requested_status = lv_rstat

      i_status_mail      = lv_statu.


  call method lo_email->send( ).


  commit work and wait.



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s