Sending PDF as an attachment via MAIL


Sending PDF in mail:

*DATA DECLARATIONS
    data: lr_docdata  type sodocchgi1,                      "Mail Subject
          lt_mailbody type table of solisti1,               "Mail Body
          lr_mailbody type solisti1,
          lt_mailrec  type table of somlrec90,              "Mail recievers
          lr_mailrec  type somlrec90,
          sender_addr type soextreci1-receiver.            "Sender address

    data:lr_pack_list type sopcklsti1,
         lt_pack_list type table of sopcklsti1.

    data:lt_bcs_pdf  type solix_tab.

    data:lf_lines type i.

***Data declarations to read Standard TEXT
    data: lt_lines_sub  type table of tline,
          lt_lines_body type table of tline,
          lt_lines_sign type table of tline.

    data:lf_name_sub  type thead-tdname,
         lf_name_body type thead-tdname,
         lf_name_sign type thead-tdname.

    data:lr_param type ztsd_parameter.

    constants:
      lc_repid   type repid value 'ZR_SD_INVOICE_PRINT01',
      lc_sub     type param_id value 'EMAIL_APP_TITLE',
      lc_body    type param_id value 'EMAIL_APP_BODY',
      lc_sign    type param_id value 'EMAIL_APP_SIGN',
      lc_id      type tdid value 'ST',
      lc_obj     type tdobject value 'TEXT',
      lc_1       type char1 value '1',
      lc_x       type char1 value 'X',
      lc_pdf     type so_obj_tp value 'PDF',
      lc_raw     type so_obj_tp value 'RAW',
      lc_invoice type char8 value 'Invoice_'  ##NO_TEXT ,
      lc_f       type char1 value 'F',
      lc_int     type char6 value 'INT',
      lc_u       type char1 value 'U',
      lc_file    type char12 value 'FILE',
      lc_xblnr   type char2 value '%%',
      lc_parvw   type parvw value 'RE',
      lc_vbtyp   type vbtypl value 'M'.


*Fetch param entries
    select *
        from ztsd_parameter
        into table @data(lt_param)
        where repid = @lc_repid.

*Fetch ADRNR from VBPA
    select single vbeln,parvw,adrnr
      from vbpa
      into @data(lr_vbpa)
      where vbeln = @gs_nast-objky and
            parvw = @lc_parvw.

*Fetch adr6-smtp_addr for reciever mail id
    if sy-subrc = 0.
      select single smtp_addr
        from adr6
        into @data(lf_mail_rec)
        where addrnumber = @lr_vbpa-adrnr and
              flg_nouse = ' '.
    endif.

*Fetch Sales org(VKORG), Ref Doc(XBLNR) of Billing Document
*Mail will trigger for which the doc type(vbtyp) is 'M' and FKSTO is blank
    select single vbeln,vkorg,xblnr,vbtyp,fksto
      from vbrk
      into @data(lr_vbrk)
      where vbeln = @gs_nast-objky
      and vbtyp = @lc_vbtyp
      and fksto = ' '.

    if sy-subrc = 0.
****Mail Subject
      read table lt_param into lr_param with key param = lc_sub
                                                 value1 = lr_vbrk-vkorg.
      if sy-subrc = 0.
        lf_name_sub = lr_param-value2.

        call function 'READ_TEXT'
          exporting
            id                      = lc_id
            language                = gs_nast-spras
            name                    = lf_name_sub
            object                  = lc_obj
          tables
            lines                   = lt_lines_sub
          exceptions
            id                      = 1
            language                = 2
            name                    = 3
            not_found               = 4
            object                  = 5
            reference_check         = 6
            wrong_access_to_archive = 7
            others                  = 8.

        if sy-subrc = 0.
          loop at lt_lines_sub into data(lr_mail_subject).
            lr_docdata-obj_descr = lr_mail_subject-tdline.
            clear:lr_mail_subject.
          endloop.
        endif.
      endif.

***Mail Body
      read table lt_param into lr_param with key param = lc_body
                                                 value1 = lr_vbrk-vkorg.
      if sy-subrc = 0.
        lf_name_body = lr_param-value2.
        call function 'READ_TEXT'
          exporting
            id                      = lc_id
            language                = gs_nast-spras
            name                    = lf_name_body
            object                  = lc_obj
          tables
            lines                   = lt_lines_body
          exceptions
            id                      = 1
            language                = 2
            name                    = 3
            not_found               = 4
            object                  = 5
            reference_check         = 6
            wrong_access_to_archive = 7
            others                  = 8.

        if sy-subrc = 0.
          loop at lt_lines_body into data(lr_lines_body).
            if lr_lines_body-tdline cs lc_xblnr.
              replace lc_xblnr in lr_lines_body-tdline with lr_vbrk-xblnr.
            endif.
            lr_mailbody-line = lr_lines_body-tdline.
            append lr_mailbody to lt_mailbody.
            clear:lr_mailbody,lr_lines_body.
          endloop.
        endif.
      endif.

****Mail Signature
      read table lt_param into lr_param with key param = lc_sign
                                                 value1 = lr_vbrk-vkorg.
      if sy-subrc = 0.
        lf_name_sign = lr_param-value2.
        call function 'READ_TEXT'
          exporting
            id                      = lc_id
            language                = gs_nast-spras
            name                    = lf_name_sign
            object                  = lc_obj
          tables
            lines                   = lt_lines_sign
          exceptions
            id                      = 1
            language                = 2
            name                    = 3
            not_found               = 4
            object                  = 5
            reference_check         = 6
            wrong_access_to_archive = 7
            others                  = 8.

        if sy-subrc = 0.
          loop at lt_lines_sign into data(lr_lines_sign).
            lr_mailbody-line = lr_lines_sign-tdline.
            append lr_mailbody to lt_mailbody.
            clear:lr_mailbody,lr_lines_sign.
          endloop.
        endif.
      endif.

      call function 'SCMS_XSTRING_TO_BINARY'
        exporting
          buffer     = us_pdf_file-pdf
        tables
          binary_tab = lt_bcs_pdf.

**** MAIL BODY-LENGTH
      describe table lt_mailbody lines lf_lines.
      clear lr_pack_list-transf_bin.
      lr_pack_list-head_start = lc_1.
      lr_pack_list-head_num = lc_1.
      lr_pack_list-body_start = lc_1.
      lr_pack_list-body_num = lf_lines.
      lr_pack_list-doc_type = lc_raw.
      append lr_pack_list to lt_pack_list.
      clear: lr_pack_list.

** Create attachment notification
      lr_pack_list-transf_bin = lc_x.
      lr_pack_list-head_start = lc_1.
      lr_pack_list-head_num = lc_1.
      lr_pack_list-body_start = lc_1.

      describe table lt_bcs_pdf lines lr_pack_list-body_num.
      lr_pack_list-doc_type = lc_pdf.
      concatenate lc_invoice gs_nast-objky into lr_pack_list-obj_descr.
      lr_pack_list-obj_name = lc_file.
      lr_pack_list-doc_size = lr_pack_list-body_num * 255.
      append lr_pack_list to lt_pack_list.
      clear:lr_pack_list.

**Subject details
      lr_docdata-sensitivty = lc_f.
      lr_docdata-doc_size = lc_1.

*Sender address
      sender_addr = sy-uname.

*Mail recievers
      lr_mailrec-receiver = lf_mail_rec.   "Biil to party address
      lr_mailrec-rec_type = lc_u.
      lr_mailrec-com_type = lc_int.
      append lr_mailrec to lt_mailrec.
      clear:lr_mailrec.

    endif.

    if lr_docdata is not initial and lt_mailbody is not initial.
*Trigerring mail
      call function 'SO_DOCUMENT_SEND_API1'
        exporting
          document_data              = lr_docdata
          sender_address             = sender_addr
          sender_address_type        = 'B'
          commit_work                = lc_x
        tables
          packing_list               = lt_pack_list
          contents_txt               = lt_mailbody
          contents_hex               = lt_bcs_pdf
          receivers                  = lt_mailrec
        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.



Comments

Popular posts from this blog

CDS Views: Table Functions with Example

Passing Dynamic Variables to SO10 TEXT

Adobe Form/PDF Form Introduction