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:

*”  IMPORTING

*”     REFERENCE(I_BNAME) TYPE  XUBNAME

*”     REFERENCE(I_MSGID) TYPE  SY-MSGID

*”     REFERENCE(I_MSGNO) TYPE  SY-MSGNO

*”     REFERENCE(I_MSGV1) OPTIONAL

*”     REFERENCE(I_MSGV2) OPTIONAL

*”     REFERENCE(I_MSGV3) OPTIONAL

*”     REFERENCE(I_MSGV4) OPTIONAL

*”     REFERENCE(I_MESAP) TYPE  CHECKBOX OPTIONAL

*”     REFERENCE(I_MEMAI) TYPE  CHECKBOX OPTIONAL

*”     REFERENCE(I_ATTYP) TYPE  SOODK-OBJTP OPTIONAL

*”     REFERENCE(I_ATSUB) TYPE  SOOD-OBJDES OPTIONAL

*”     REFERENCE(I_ATTXT) TYPE  SOLI_TAB OPTIONAL

*”———————————————————————-

 
 

 
 

  data:

      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,

        con(1),

      end of lf_split,

 
 

      lv_class type dokhl-object,

      lv_msgnr type char3,

      lv_langu type char1,

      lv_text  type string.

 
 

  data:

        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.

 
 

  refresh:

    lt_cont,

    lt_rece.

 
 

  lf_docdat-obj_name  = ‘SAPOFFICE’.

 
 

  call function ‘MESSAGE_TEXT_BUILD’

    exporting

      msgid               = i_msgid

      msgnr               = i_msgno

      msgv1               = i_msgv1

      msgv2               = i_msgv2

      msgv3               = i_msgv3

      msgv4               = i_msgv4

    importing

      message_text_output = lf_docdat-obj_descr.

 
 

  lv_class = i_msgid.

  lv_msgnr = i_msgno.

  lv_langu = sy-langu.

 
 

  call function ‘SVMCRT_INT_GETMSG_LONGTEXT’

    exporting

      msg_class = lv_class

      msg_nr    = lv_msgnr

      language  = lv_langu

    importing

      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 ).

  do.

    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.

    endif.

 
 

    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.

      exit.

    endif.

  enddo.

 
 

* İ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.

  endif.

 
 

  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.

  endif.

 
 

* Send mail

  lo_email = cl_bcs=>create_persistent( ).

 
 

  call function ‘SO_RAW_TO_RTF’

    tables

      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’

      tables

        objcont_old = i_attxt

        objcont_new = lt_attx.

 
 

    call method lo_docum->add_attachment

      exporting

        i_attachment_type    = i_attyp

        i_attachment_subject = i_atsub

        i_att_content_text   = lt_attx[].

  endif.

 
 

  call method lo_email->set_document( lo_docum ).

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

 
 

  call method lo_email->set_sender

    exporting

      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 ).

    endcase.

 
 

 
 

    call method lo_email->add_recipient

      exporting

        i_recipient  = lo_recip

        i_express    = ‘X’

        i_copy       = ‘ ‘

        i_blind_copy = ‘ ‘

        i_no_forward = ‘ ‘.

  endloop.

 
 

  move ‘N’ to lv_rstat.

  lv_statu = lv_rstat.

 
 

  call method lo_email->set_status_attributes

    exporting

      i_requested_status = lv_rstat

      i_status_mail      = lv_statu.

 
 

  call method lo_email->send( ).

 
 

  commit work and wait.

endfunction.

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s