sap abap smartforms as PDF attachment in email


Objective :  we have one smartforms ZDEMO_PO1 for purchase order. This is to be send as pdf attachment with email. Email will also contain the PO details in HTML format.
                    The sender id will be pickup from the user master.

Selection Screen - 


Output 



Code block :

SELECTION-SCREEN BEGIN OF BLOCK B1 WITH FRAME TITLE TEXT-001.
PARAMETERS P_EBELN TYPE EKKO-EBELN OBLIGATORY,
             P_FROM  TYPE ADR6-SMTP_ADDR.
SELECT-OPTIONS S_TO FOR ADR6-SMTP_ADDR OBLIGATORY,
                 S_CC FOR ADR6-SMTP_ADDR,
                 S_BCC FOR ADR6-SMTP_ADDR.
SELECTION-SCREEN END OF BLOCK B1.


INITIALIZATION.
  PERFORM INITIALIZATION.


START-OF-SELECTION.
  PERFORM GET_DATA.
  PERFORM BUILD_EMAIL_BODY.
  perform sf_pdf_data.
  perform send_email.


END-OF-SELECTION.

" This block will get the PO header and line item details 

PERFORM GET_DATA.

"This block will build the HTML body using PO item table EKPO

PERFORM BUILD_EMAIL_BODY.

"This block will convert the smartforms output to binary pdf data

perform sf_pdf_data.

"This block will send the email along with attachment

perform send_email.

Complete code :

REPORT ZPO_SF_EMAIL.

TABLES ADR6EKKO.

DATA GS_EKKO TYPE EKKO,
       GT_EKPO TYPE STANDARD TABLE OF EKPO,
       GS_EKPO TYPE EKPO.

"for smartform calling
DATA FM_NAME            TYPE  RS38L_FNAM,
       CONTROL_PARAMETERS TYPE  SSFCTRLOP,
       OUTPUT_OPTIONS     TYPE  SSFCOMPOP,
       LW_SSFCRESCL       TYPE SSFCRESCL.

"for otf to pdf
DATA:  LI_OTF          TYPE TABLE OF ITCOO,
       LW_OTF          TYPE ITCOO,
       LI_OTF1         TYPE TABLE OF ITCOO,
       LI_PDF_TAB      TYPE TABLE OF TLINE,
       LI_CONTENT_TXT  TYPE SOLI_TAB,
       LW_CONTENT      TYPE SOLI,
       LI_CONTENT_HEX  TYPE SOLIX_TAB,
       LI_OBJHEAD      TYPE SOLI_TAB,
       lv_bin_filesize   TYPE i,
       LV_TRANSFER_BIN TYPE SX_BOOLEAN,
       LV_LEN          TYPE SO_OBJ_LEN.


"for sender details
DATA GS_ADDRESS LIKE BAPIADDR3,
       RETURN     LIKE STANDARD TABLE OF BAPIRET2 WITH HEADER LINE,
       ADDTEL     LIKE STANDARD TABLE OF BAPIADTEL.

"email data declaration
DATA IT_CONTENTS TYPE STANDARD TABLE OF SOLISTI1,
       WA_CONTENTS TYPE SOLISTI1.


"for email data declarations
data LV_SUB          TYPE STRING,         "so_obj_des.
       LV_SENT_TO_ALL  TYPE OS_BOOLEAN,
       LV_ATT_NAME     TYPE CHAR50,

       GO_SEND_REQUEST  TYPE REF TO  CL_BCS,
       GO_DOCUMENT      TYPE REF TO  CL_DOCUMENT_BCS,

       SENDER1         TYPE REF TO CL_CAM_ADDRESS_BCS,
       LO_RECIPIENT     TYPE REF TO  IF_RECIPIENT_BCS,

       LO_BCS_EXCEPTION TYPE REF TO  CX_BCS.



CONSTANTS:
"-- Constants used in the body of the Email (HTML)
  C_HTM      TYPE CHAR3   VALUE 'HTM',
  C_SPACE(6TYPE C       VALUE ' ',   "for adding space
  C_NEW_LINE TYPE CHAR255 VALUE '<br>'.

DATA TEXT  TYPE CHAR100,
       TEXT3 TYPE CHAR100,
       TEXT1 TYPE CHAR20,
       TEXT2 TYPE STRING.


SELECTION-SCREEN BEGIN OF BLOCK B1 WITH FRAME TITLE TEXT-001.
PARAMETERS P_EBELN TYPE EKKO-EBELN OBLIGATORY,
             P_FROM  TYPE ADR6-SMTP_ADDR.
SELECT-OPTIONS S_TO FOR ADR6-SMTP_ADDR OBLIGATORY,
                 S_CC FOR ADR6-SMTP_ADDR,
                 S_BCC FOR ADR6-SMTP_ADDR.
SELECTION-SCREEN END OF BLOCK B1.


INITIALIZATION.
  PERFORM INITIALIZATION.


START-OF-SELECTION.
  PERFORM GET_DATA.
  PERFORM BUILD_EMAIL_BODY.
  perform sf_pdf_data.
  perform send_email.


END-OF-SELECTION.

FORM INITIALIZATION .

  "default values
  CALL FUNCTION 'BAPI_USER_GET_DETAIL'
    EXPORTING
      USERNAME SY-UNAME
"     CACHE_RESULTS       = 'X'
    IMPORTING
      ADDRESS  GS_ADDRESS
    TABLES
      RETURN   RETURN
      ADDTEL   ADDTEL.


  P_FROM GS_ADDRESS-E_MAIL.

ENDFORM.

FORM GET_DATA.

  SELECT SINGLE ' FROM EKKO INTO CORRESPONDING FIELDS OF GS_EKKO
  WHERE EBELN P_EBELN.

  IF GS_EKKO-EBELN IS NOT INITIAL.

    SELECT ' FROM EKPO INTO CORRESPONDING FIELDS OF TABLE GT_EKPO
    WHERE EBELN GS_EKKO-EBELN.


  ELSE.
    MESSAGE 'PO not found' TYPE 'I'.
  ENDIF.

ENDFORM.


FORM BUILD_EMAIL_BODY.

  CLEAR IT_CONTENTS[]WA_CONTENTS.

  "email body
  WA_CONTENTS-LINE '<HTML> <BODY>'.
  APPEND WA_CONTENTS TO IT_CONTENTS.
  CLEAR WA_CONTENTS.

  CLEAR TEXT3.
  CONCATENATE 'Dear Sir/Madam'  ',' INTO TEXT3 SEPARATED BY SPACE.

  CONCATENATE '<p style="font-family:Calibri;font-size:15;">' TEXT3 INTO WA_CONTENTS-LINE.
  APPEND WA_CONTENTS TO IT_CONTENTS.
  CLEAR WA_CONTENTS.

  PERFORM LINE_BREAK.

  CONCATENATE C_SPACE C_SPACE C_SPACE 'Kindly delivered the below mention Materials :' '' INTO
   WA_CONTENTS-LINE SEPARATED BY SPACE.
  APPEND WA_CONTENTS TO IT_CONTENTS.
  CLEAR WA_CONTENTS.

  PERFORM LINE_BREAK.
  PERFORM LINE_BREAK.

  "line item table
  WA_CONTENTS-LINE '<table style="font-family:calibri;font-size:15;MARGIN:10px;"'.   " bordercolor="blue"'.
  APPEND WA_CONTENTS TO IT_CONTENTS.
  CLEAR WA_CONTENTS.

  WA_CONTENTS-LINE 'cellspacing="0" cellpadding="1" width="75%" '.
  APPEND WA_CONTENTS TO IT_CONTENTS.
  CLEAR WA_CONTENTS.

  WA_CONTENTS-LINE 'border="1"><tbody><tr>'.
  APPEND WA_CONTENTS TO IT_CONTENTS.
  CLEAR WA_CONTENTS.

  "column heading

  WA_CONTENTS-LINE '<th bgcolor="#C0C0C0">Sr. No.</th>'.
  APPEND WA_CONTENTS TO IT_CONTENTS.
  CLEAR WA_CONTENTS.

  WA_CONTENTS-LINE '<th bgcolor="#C0C0C0">Material Code</th>'.
  APPEND WA_CONTENTS TO IT_CONTENTS.
  CLEAR WA_CONTENTS.

  WA_CONTENTS-LINE '<th bgcolor="#C0C0C0">Description</th>'.
  APPEND WA_CONTENTS TO IT_CONTENTS.
  CLEAR WA_CONTENTS.

  WA_CONTENTS-LINE '<th bgcolor="#C0C0C0">Quantity</th>'.
  APPEND WA_CONTENTS TO IT_CONTENTS.
  CLEAR WA_CONTENTS.

  WA_CONTENTS-LINE '<th bgcolor="#C0C0C0">UOM</th>'.
  APPEND WA_CONTENTS TO IT_CONTENTS.
  CLEAR WA_CONTENTS.

  WA_CONTENTS-LINE '<th bgcolor="#C0C0C0">Net Price</th>'.
  APPEND WA_CONTENTS TO IT_CONTENTS.
  CLEAR WA_CONTENTS.

  WA_CONTENTS-LINE '<th bgcolor="#C0C0C0">Total w/o Tax</th>'.
  APPEND WA_CONTENTS TO IT_CONTENTS.
  CLEAR WA_CONTENTS.

  "end of cloumn heading
  LOOP AT GT_EKPO INTO GS_EKPO.
    "table content
    WA_CONTENTS-LINE '<tr align = "center">'.
    APPEND WA_CONTENTS TO IT_CONTENTS.
    CLEAR WA_CONTENTS.

    CONCATENATE '<td>' GS_EKPO-EBELP '</td>' INTO WA_CONTENTS-LINE SEPARATED BY SPACE.
    APPEND WA_CONTENTS TO IT_CONTENTS.
    CLEAR WA_CONTENTS.

    clear text.
    CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT'
      EXPORTING
        INPUT         gs_ekpo-matnr
     IMPORTING
       OUTPUT        text.


    CONCATENATE '<td>' text '</td>' INTO WA_CONTENTS-LINE SEPARATED BY SPACE.
    APPEND WA_CONTENTS TO IT_CONTENTS.
    CLEAR WA_CONTENTS.

    CONCATENATE '<td>' GS_EKPO-TXZ01 '</td>' INTO WA_CONTENTS-LINE SEPARATED BY SPACE.
    APPEND WA_CONTENTS TO IT_CONTENTS.
    CLEAR WA_CONTENTS.

    CLEAR TEXT1.
    WRITE GS_EKPO-MENGE TO TEXT1 RIGHT-JUSTIFIED.
    CONCATENATE '<td>' TEXT1 '</td>' INTO WA_CONTENTS-LINE SEPARATED BY SPACE.
    APPEND WA_CONTENTS TO IT_CONTENTS.
    CLEAR WA_CONTENTS.

    CONCATENATE '<td>' GS_EKPO-MEINS '</td>' INTO WA_CONTENTS-LINE SEPARATED BY SPACE.
    APPEND WA_CONTENTS TO IT_CONTENTS.
    CLEAR WA_CONTENTS.

    CLEAR TEXT1.
    WRITE GS_EKPO-NETPR TO TEXT1 RIGHT-JUSTIFIED.
    CONCATENATE '<td>' TEXT1 '</td>' INTO WA_CONTENTS-LINE SEPARATED BY SPACE.
    APPEND WA_CONTENTS TO IT_CONTENTS.
    CLEAR WA_CONTENTS.

    CLEAR TEXT1.
    WRITE GS_EKPO-NETWR TO TEXT1 RIGHT-JUSTIFIED.
    CONCATENATE '<td>' TEXT1 '</td>' INTO WA_CONTENTS-LINE SEPARATED BY SPACE.
    APPEND WA_CONTENTS TO IT_CONTENTS.
    CLEAR WA_CONTENTS.

    WA_CONTENTS-LINE '</tr>'.
    APPEND WA_CONTENTS TO IT_CONTENTS.
    CLEAR WA_CONTENTS.


    CLEAR GS_EKPO.
  ENDLOOP.

  WA_CONTENTS-LINE '</tbody> </table>'.
  APPEND WA_CONTENTS TO IT_CONTENTS.
  CLEAR WA_CONTENTS.

  PERFORM LINE_BREAK.
  PERFORM LINE_BREAK.


  WA_CONTENTS-LINE 'Find the attach copy of purchase order details.'.
  APPEND WA_CONTENTS TO IT_CONTENTS.
  CLEAR WA_CONTENTS.

  PERFORM LINE_BREAK.
  PERFORM LINE_BREAK.

  WA_CONTENTS-LINE 'Thanking & Regards'.
  APPEND WA_CONTENTS TO IT_CONTENTS.
  CLEAR WA_CONTENTS.

  PERFORM LINE_BREAK.

  WA_CONTENTS-LINE 'Purchase Team'.
  APPEND WA_CONTENTS TO IT_CONTENTS.
  CLEAR WA_CONTENTS.


ENDFORM.

FORM LINE_BREAK .
  WA_CONTENTS-LINE '<br>'.
  APPEND WA_CONTENTS TO IT_CONTENTS.
  CLEAR WA_CONTENTS.

ENDFORM.


FORM SF_PDF_DATA.

CALL FUNCTION 'SSF_FUNCTION_MODULE_NAME'
  EXPORTING
    FORMNAME                 'ZDEMO_PO1'
"   VARIANT                  = ' '
"   DIRECT_CALL              = ' '
 IMPORTING
   FM_NAME                  FM_NAME
 EXCEPTIONS
   NO_FORM                  1
   NO_FUNCTION_MODULE       2
   OTHERS                   3
          .
IF SY-SUBRC <> 0.
" Implement suitable error handling here
ENDIF.


   CONTROL_PARAMETERS-GETOTF     'X'.
   CONTROL_PARAMETERS-PREVIEW    space.
   CONTROL_PARAMETERS-NO_DIALOG  'X'.

   OUTPUT_OPTIONS-TDDEST 'LP01'.


 CALL FUNCTION FM_NAME                       "'/1BCDWB/SF00000264'
  EXPORTING
"    ARCHIVE_INDEX              =
"    ARCHIVE_INDEX_TAB          =
"    ARCHIVE_PARAMETERS         =
    CONTROL_PARAMETERS         CONTROL_PARAMETERS
"    MAIL_APPL_OBJ              =
"    MAIL_RECIPIENT             =
"    MAIL_SENDER                =
    OUTPUT_OPTIONS             OUTPUT_OPTIONS
    USER_SETTINGS              'X'
    IP_EBELN                   P_EBELN
  IMPORTING
"    DOCUMENT_OUTPUT_INFO       =
    JOB_OUTPUT_INFO            LW_SSFCRESCL
"    JOB_OUTPUT_OPTIONS         =
  EXCEPTIONS
    FORMATTING_ERROR           1
    INTERNAL_ERROR             2
    SEND_ERROR                 3
    USER_CANCELED              4
    OTHERS                     5
           .
 IF SY-SUBRC <> 0.
" Implement suitable error handling here
 ENDIF.


".. Get OTF data to convert to PDF
    REFRESH LI_OTF[].
    LI_OTF[] LW_SSFCRESCL-OTFDATA[].
    CLEAR LV_BIN_FILESIZE.

    CALL FUNCTION 'CONVERT_OTF'
      EXPORTING
        FORMAT                'PDF'
      IMPORTING
        BIN_FILESIZE          LV_BIN_FILESIZE
      TABLES
        OTF                   LI_OTF
        LINES                 LI_PDF_TAB
      EXCEPTIONS
        ERR_MAX_LINEWIDTH     1
        ERR_FORMAT            2
        ERR_CONV_NOT_POSSIBLE 3
        ERR_BAD_OTF           4
        OTHERS                5.

    IF SY-SUBRC <> 0.
      MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
              WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
    ENDIF.


  REFRESH LI_CONTENT_TXT.
  LOOP AT LI_OTF  INTO  LW_OTF.
    CLEAR LW_CONTENT.
    CONCATENATE LW_OTF-TDPRINTCOM LW_OTF-TDPRINTPAR INTO LW_CONTENT.
    APPEND LW_CONTENT TO LI_CONTENT_TXT.
    CLEAR LW_CONTENT.
  ENDLOOP.

  REFRESH LI_CONTENT_HEX,
            LI_OBJHEAD.

  CLEAR   LV_TRANSFER_BIN.

"/.. FM to convert OTF to PDF
  CALL FUNCTION 'SX_OBJECT_CONVERT_OTF_PDF'
    EXPORTING
      FORMAT_SRC      'OTF'
      FORMAT_DST      'PDF'
    CHANGING
      TRANSFER_BIN    LV_TRANSFER_BIN
      CONTENT_TXT     LI_CONTENT_TXT
      CONTENT_BIN     LI_CONTENT_HEX
      OBJHEAD         LI_OBJHEAD
      LEN             LV_LEN
    EXCEPTIONS
      ERR_CONV_FAILED 1
      OTHERS          2.

  IF SY-SUBRC <> 0.
    MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
            WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  ENDIF.

ENDFORM.


FORM SEND_EMAIL .

"set subject
  clear LV_SUB.
  CONCATENATE 'Purchase Order -' p_ebeln into lv_sub SEPARATED BY space.

  TRY.
"     -------- create persistent send request ------------------------
      GO_SEND_REQUEST CL_BCS=>CREATE_PERSISTENT).

"     -------- create and set document -------------------------------
"     create document from internal table with text

      GO_DOCUMENT CL_DOCUMENT_BCS=>CREATE_DOCUMENT(
                      I_TYPE    'HTM'
                      I_TEXT    IT_CONTENTS
"                      i_length  = '12'
                      I_SUBJECT '' ).             "'

"set subject

      CALL METHOD GO_SEND_REQUEST->SET_MESSAGE_SUBJECT
        EXPORTING
          IP_SUBJECT LV_SUB.

"smartforms pdf data as attachments
       CONCATENATE 'Purchase order' P_EBELN into LV_ATT_NAME SEPARATED BY space.

      IF NOT LI_CONTENT_HEX IS INITIAL.
        GO_DOCUMENT->ADD_ATTACHMENT(
             I_ATTACHMENT_TYPE      'PDF'
             I_ATTACHMENT_SUBJECT   LV_ATT_NAME    "gv_title   "attachment subj
             I_ATTACHMENT_SIZE      LV_LEN
             I_ATT_CONTENT_HEX      LI_CONTENT_HEX ).
      ENDIF.

      CALL METHOD GO_SEND_REQUEST->SET_DOCUMENTGO_DOCUMENT ).

" Send mail using Email_ID
      SENDER1 CL_CAM_ADDRESS_BCS=>CREATE_INTERNET_ADDRESSP_FROM ).
      CALL METHOD GO_SEND_REQUEST->SET_SENDER
        EXPORTING
          I_SENDER SENDER1.


      LOOP AT S_TO.

        LO_RECIPIENT CL_CAM_ADDRESS_BCS=>CREATE_INTERNET_ADDRESS(
                                       S_TO-LOW ).

"     add recipient with its respective attributes to send request
        CALL METHOD GO_SEND_REQUEST->ADD_RECIPIENT
          EXPORTING
            I_RECIPIENT LO_RECIPIENT
            I_EXPRESS   'X'.

      ENDLOOP.

      "cc
      LOOP AT S_CC.

        LO_RECIPIENT CL_CAM_ADDRESS_BCS=>CREATE_INTERNET_ADDRESS(
                                       S_CC-LOW ).

"     add recipient with its respective attributes to send request
        CALL METHOD GO_SEND_REQUEST->ADD_RECIPIENT
          EXPORTING
            I_RECIPIENT LO_RECIPIENT
            I_COPY      'X'.

      ENDLOOP.

      "cc
      LOOP AT S_BCC.

        LO_RECIPIENT CL_CAM_ADDRESS_BCS=>CREATE_INTERNET_ADDRESS(
                                       S_BCC-LOW ).

"     add recipient with its respective attributes to send request
        CALL METHOD GO_SEND_REQUEST->ADD_RECIPIENT
          EXPORTING
            I_RECIPIENT  LO_RECIPIENT
            I_BLIND_COPY 'X'.

      ENDLOOP.

    CALL METHOD GO_SEND_REQUEST->SEND_REQUEST->SET_LINK_TO_OUTBOX'X' ).

 CALL METHOD GO_SEND_REQUEST->SET_SEND_IMMEDIATELY
        EXPORTING
          I_SEND_IMMEDIATELY 'X'.
"     CATCH cx_send_req_bcs .
"    ENDTRY.

      CALL METHOD GO_SEND_REQUEST->SEND(
        EXPORTING
          I_WITH_ERROR_SCREEN 'X'
        RECEIVING
          RESULT              LV_SENT_TO_ALL ).

      IF LV_SENT_TO_ALL 'X'.
          MESSAGE S000(8IWITH 'Email send successfully'.
      ELSEIF LV_SENT_TO_ALL IS INITIAL.
          MESSAGE S000(8IWITH 'Email not send'.
      ENDIF.


   COMMIT WORK.

  CATCH CX_BCS INTO LO_BCS_EXCEPTION.
  ENDTRY.


ENDFORM.


"""""""" for complete smartforms videos kindly refer below play list



Thanks,
 Kindly share your feedback.











Post a Comment

6 Comments

  1. Hello sir
    I follow your blog and youtube and I have learned alot from your channel and sir one request I need ABAP proxy am trying to learn
    If possible can you provide ABAP proxy for us

    ReplyDelete
    Replies
    1. Hi,
      Thanks for your feedback.
      I will try to provide some training on proxy.

      Delete
  2. Hi sir,
    When ever start the Chanel and blogs very helpfull me thanks for sharing knowledge if you possible plsshare the bdc and bapi tutorial sir .

    ReplyDelete
    Replies
    1. Hi...
      Currently idoc videos are in process....once this is done....
      Next I will cover data migration using lsmw , bdc and bapi

      Delete
  3. Hi....
    Your blogs are very helpful, thanks for sharing knowledge if you have time plz share below question blogs and you can answer it here.
    How we change the smart-form text or field into Arabic language?

    ReplyDelete
  4. Hi....
    Your blogs are very helpful, thanks for sharing knowledge if you have time plz share below question blogs
    How to communication with other 3rd party web application and sap using web API ?

    Ex : get sales order data from 3rd party web app And returen sale order number from sap using web api?

    ReplyDelete

Total Pageviews