Pages

Tuesday, 26 November 2013

FM Send Email and Attachment Using SO_DOCUMENT_SEND_API1

Sample code for sending email using  SO_DOCUMENT_SEND_API1.

 

FUNCTION zsendmail.
*"----------------------------------------------------------------------
*"*"Local Interface:
*"  IMPORTING
*"     REFERENCE(IM_MAIL) TYPE  SO_RECNAME
*"     REFERENCE(IM_FAX) TYPE  SO_FAX_NR OPTIONAL
*"     REFERENCE(IM_JUDUL) TYPE  SO_OBJ_DES
*"     REFERENCE(IM_MAIL2) TYPE  SO_RECNAME OPTIONAL
*"     REFERENCE(IM_MAIL3) TYPE  SO_RECNAME OPTIONAL
*"     REFERENCE(IM_MAIL4) TYPE  SO_RECNAME OPTIONAL
*"     REFERENCE(IM_MAIL5) TYPE  SO_RECNAME OPTIONAL
*"     REFERENCE(IM_MAIL_CC) TYPE  SO_RECNAME OPTIONAL
*"     REFERENCE(IM_SENDER) LIKE  SOEXTRECI1-RECEIVER OPTIONAL
*"  EXPORTING
*"     VALUE(NEW_OBJECT_ID) LIKE  SOFOLENTI1-OBJECT_ID
*"  TABLES
*"      ZOTF STRUCTURE  ITCOO
*"      T_TEXT STRUCTURE  SOLISTI1 OPTIONAL
*"----------------------------------------------------------------------


  DATA: lt_otf TYPE itcoo OCCURS 0 WITH HEADER LINE.
  DATA: lt_doct TYPE docs OCCURS 0 WITH HEADER LINE.
  DATA: lt_pdf_tab  LIKE tline OCCURS 0 WITH HEADER LINE.
  DATA: ld_bin_filesize TYPE i.
  REFRESH : lt_otf, lt_doct, lt_pdf_tab.
  CLEAR   : lt_otf, lt_doct, lt_pdf_tab, ld_bin_filesize.
*=================================
  TYPES : ty_line TYPE string.
  DATA : lt_attachment TYPE soli OCCURS 0 WITH HEADER LINE.
  DATA : lt_attachment_long TYPE ty_line OCCURS 0 WITH HEADER LINE.
  DATA : lv_counter TYPE i.
  DATA : lv_from TYPE i.

  REFRESH : lt_attachment, lt_attachment_long.
  CLEAR   : lt_attachment, lt_attachment_long, lv_counter, lv_from.
*=================================
  DATA : p_judul TYPE so_obj_des,
         p_text  TYPE so_text255 VALUE 'Please Read The Attachment Below',
         p_email TYPE so_recname,
         p_email2 TYPE so_recname,
         p_email3 TYPE so_recname,
         p_email4 TYPE so_recname,
         p_email5 TYPE so_recname,
         p_fax(30) TYPE n.

  DATA : document_data LIKE sodocchgi1,
         objpack LIKE sopcklsti1 OCCURS 0 WITH HEADER LINE,
         objhead LIKE solisti1 OCCURS 0 WITH HEADER LINE,
         objtxt LIKE solisti1 OCCURS 0 WITH HEADER LINE,
         receivers LIKE somlreci1 OCCURS 0 WITH HEADER LINE,
         tab_lines TYPE i.
  DATA : l_att_lines TYPE i.
  DATA : ld_newobjectid LIKE  sofolenti1-object_id.
*=================================
  CLEAR : p_email, document_data, objpack, objhead, objtxt, receivers, ld_newobjectid,
          tab_lines, l_att_lines, p_judul, p_fax, p_email2, p_email3, p_email4, p_email5.
  REFRESH : objpack, objhead, objtxt, receivers.
*=================================
  p_email  = im_mail.
  p_email2 = im_mail2.
  p_email3 = im_mail3.
  p_email4 = im_mail4.
  p_email5 = im_mail5.
  p_judul  = im_judul.

  lt_otf[] = zotf[].
  CLEAR ld_bin_filesize.
  REFRESH : lt_pdf_tab.
  CALL FUNCTION 'CONVERT_OTF_2_PDF'
    IMPORTING
      bin_filesize           = ld_bin_filesize
    TABLES
      otf                    = lt_otf
      doctab_archive         = lt_doct
      lines                  = lt_pdf_tab
    EXCEPTIONS
      err_conv_not_possible  = 1
      err_otf_mc_noendmarker = 2
      OTHERS                 = 3.

  REFRESH: lt_attachment_long, lt_attachment.

  LOOP AT lt_pdf_tab.
    TRANSLATE  lt_pdf_tab USING ' ~' .
    CONCATENATE lt_attachment_long lt_pdf_tab INTO lt_attachment_long.
  ENDLOOP.
  TRANSLATE  lt_attachment_long USING '~ ' .
  APPEND lt_attachment_long.
  CLEAR : lv_counter.

  DO.
    lv_counter = STRLEN( lt_attachment_long ).
    IF lv_counter GE 255.
      lt_attachment = lt_attachment_long(255).
      APPEND lt_attachment.
      SHIFT lt_attachment_long BY 255 PLACES.
    ELSE.
      lt_attachment = lt_attachment_long(lv_counter).
      APPEND lt_attachment.
      EXIT.
    ENDIF.
  ENDDO.

  "ambil nilai text isi email
  CLEAR objtxt[].
  objtxt[] = t_text[].


  CLEAR document_data.
  DESCRIBE TABLE objtxt LINES tab_lines.
  READ TABLE objtxt INDEX tab_lines.
  document_data-doc_size = ( tab_lines - 1 ) * 255 + STRLEN( objtxt ).
  WRITE p_judul TO document_data-obj_descr.

  DESCRIBE TABLE objtxt LINES tab_lines.
  CLEAR objpack-transf_bin.
  objpack-head_start = 1.
  objpack-head_num = 0.
  objpack-body_start = 1.
  objpack-body_num = tab_lines.
  objpack-doc_type = 'RAW'.
  APPEND objpack.

  CLEAR receivers.
  receivers-receiver = p_email. "Email Address
  receivers-rec_type = 'U'. "Intenet Address
  APPEND receivers.

  IF NOT p_email2 IS INITIAL.
    CLEAR receivers.
    receivers-receiver = p_email2. "Email Address 2
    receivers-rec_type = 'U'. "Intenet Address
    APPEND receivers.
  ENDIF.

  IF NOT p_email3 IS INITIAL.
    CLEAR receivers.
    receivers-receiver = p_email3. "Email Address 3
    receivers-rec_type = 'U'. "Intenet Address
    APPEND receivers.
  ENDIF.

  IF NOT p_email4 IS INITIAL.
    CLEAR receivers.
    receivers-receiver = p_email4. "Email Address 4
    receivers-rec_type = 'U'. "Intenet Address
    APPEND receivers.
  ENDIF.

  IF NOT p_email5 IS INITIAL.
    CLEAR receivers.
    receivers-receiver = p_email5. "Email Address 5
    receivers-rec_type = 'U'. "Intenet Address
    APPEND receivers.
  ENDIF.

* {{ -- Add by Xxxxx 22.11.2013 / xxxxxxx. Purpose : Add cc email address.
  IF NOT im_mail_cc IS INITIAL.
    receivers-receiver = im_mail_cc.
    receivers-copy     = 'X'. "Cc
    receivers-rec_type = 'U'. "Internet address
    APPEND receivers.
  ENDIF.
* }} -- End Add by Xxxxx

  DESCRIBE TABLE lt_attachment LINES l_att_lines.
  READ TABLE lt_attachment INDEX l_att_lines.

  CLEAR objpack.
  objpack-transf_bin = 'X'.
  objpack-head_start = 1.
  objpack-head_num = 1.
  objpack-body_start = 1.
  objpack-body_num = l_att_lines.
  objpack-doc_type = 'PDF'.
  objpack-obj_name = 'email'.
  objpack-obj_descr = 'Attachment'.

  IF objpack-obj_descr IS INITIAL.
    objpack-obj_descr = 'File_Attachment'.
  ENDIF.
  objpack-doc_size = ( 255 * ( l_att_lines - 1 ) ) + STRLEN(
  lt_attachment-line ).
  APPEND objpack.

  CALL FUNCTION 'SO_DOCUMENT_SEND_API1'
    EXPORTING
      document_data              = document_data
      put_in_outbox              = 'X'
      sender_address             = im_sender
      sender_address_type        = 'SMTP'
      commit_work                = 'X'
    IMPORTING
      new_object_id              = ld_newobjectid
    TABLES
      packing_list               = objpack
      object_header              = objhead
      contents_bin               = lt_attachment
      contents_txt               = objtxt
      receivers                  = receivers
    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.
  CASE sy-subrc.
    WHEN 0.
    WHEN 1.
      RAISE too_many_receivers.
    WHEN 2.
      RAISE document_not_sent  .
    WHEN 3.
      RAISE document_type_not_exist.
    WHEN 4.
      RAISE operation_no_authorization.
    WHEN 5.
      RAISE parameter_error.
    WHEN 7.
      RAISE enqueue_error .
    WHEN OTHERS.
      RAISE x_error.
  ENDCASE.

  CLEAR new_object_id.
  new_object_id = ld_newobjectid.

ENDFUNCTION. 

No comments:

Post a Comment