How to Email Smartform as PDF Attachment to Multiple Users?

How to Email Smartform as PDF Attachment to Multiple Users?

Sending Smartform yield as PDF in an email is a point which has been questioned and addressed on different occasions in various gatherings. However, at whatever point we get this necessity, we really want to scramble for our past notes or hit the web search tool. As of late, when I got this solicitation from my business, it was nothing unique to me also. How to Email Smartform as PDF Attachment to Multiple Users?

In the wake of conveying this turn of events, I was searching for a storehouse where I can put my code so that assuming that I have similar necessity in my future Ventures, I know, precisely where to look. As of now, which storehouse is superior to eLearningsolutions for SAP Specialized stuff. (simply joking). How to Email Smartform as PDF Attachment to Multiple Users?

Aside from the rationale of saving my scrap in the web, the other better goal was to uncover to all freshers in SAP ABAP that, you simply need to follow 4 stages for this prerequisite.

  1. Get the Smartform output in OTF (Other Text Format).
  2. Convert the OTF to Hexa decimal string format.
  3. Convert the Hexa decimal string format to Binary format.
  4. Build the email body and attach the Binary table as an attachment using CL_DOCUMENT_BCS-ADD_ATTACHMENT and send the email using CL_BCS class.

Assuming that you comprehend these four stages, you want not look the web crawler in future for such turns of events. .

Allow me to be minimal more unambiguous with the details of the four stages.

1. Smartform Output in OTF

When you call your smartform, you would get the Smart Forms OTF Table in IMPORTING parameter  JOB_OUTPUT_INFO component OTFDATA which is the structure for Smart Forms: Table OTF.

For the below snippet, the OTF data would be in w_return-otfdata.

 * Call Smartform
  CALL FUNCTION v_form_name
    EXPORTING
      control_parameters = w_ctrlop
      output_options     = w_compop
      user_settings      = abap_true
    IMPORTING
      job_output_info    = w_return " This will have all output
    EXCEPTIONS
      formatting_error   = 1
      internal_error     = 2
      send_error         = 3
      user_canceled      = 4
      OTHERS             = 5.
* Get Output Text Format (OTF)
  i_otf[] = w_return-otfdata[].

2. Convert the OTF to Hexa decimal string format.

The below snippet is to convert the OTF to XSTRING format.

* Import HexaString and filesize
  CALL FUNCTION 'CONVERT_OTF'
    EXPORTING
      format                = 'PDF'
      max_linewidth         = 132
    IMPORTING
      bin_filesize          = v_len_in
      bin_file              = i_xstring   " This is NOT Binary. This is Hexa
    TABLES
      otf                   = i_otf
      lines                 = i_tline
    EXCEPTIONS
      err_max_linewidth     = 1
      err_format            = 2
      err_conv_not_possible = 3
      OTHERS                = 4.

3. Convert the Hexa decimal string format to Binary format.

* Convert Hexa String to Binary format
  CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
    EXPORTING
      buffer     = i_xstring
    TABLES
      binary_tab = i_objbin[].

Simply an idea. In the event that you can track down any capability to change over OTF straightforwardly to Twofold Organize then you can skirt the second step of switching OTF over completely to Hexa.

4. Use CL_DOCUMENT_BCS class and method ADD_ATTACHMENT to attach the document and use CL_BCS class to email the document.

If you have any desire to peruse more about CL_BCS class use, then, at that point, kindly check this valuable post ‘Send an email with title more prominent than 50 characters’.

On the off chance that you have a cutoff time, I can comprehend, you don’t need more Gyan (Sanskrit work which generally means Teaching). You are more keen on getting a functioning code which you can squeeze into your framework. Sit back and relax, I won’t dishearten you. If it’s not too much trouble, find a functioning piece which switches the Smartform over completely to PDF and messages it as a PDF connection.

*--------------------------------------------------------------------*
* Program to convert smartform as PDF and email PDF to
* multiple recipients.

*--------------------------------------------------------------------*
TABLES adr6.
*--------------------------------------------------------------------*
*--------------------------------------------------------------------*
* Global Data Declaration for ease
* In actual project, please do not define all these in global
*--------------------------------------------------------------------*
DATA: i_otf       TYPE itcoo    OCCURS 0 WITH HEADER LINE,
      i_tline     LIKE tline    OCCURS 0 WITH HEADER LINE,
      i_record    LIKE solisti1 OCCURS 0 WITH HEADER LINE,
      i_xstring   TYPE xstring,
* Objects to send mail.
      i_objpack   LIKE sopcklsti1 OCCURS 0 WITH HEADER LINE,
      i_objtxt    LIKE solisti1   OCCURS 0 WITH HEADER LINE,
      i_objbin    LIKE solix      OCCURS 0 WITH HEADER LINE,
      i_reclist   LIKE somlreci1  OCCURS 0 WITH HEADER LINE,
* Work Area declarations
      wa_objhead  TYPE soli_tab,
      w_ctrlop    TYPE ssfctrlop,
      w_compop    TYPE ssfcompop,
      w_return    TYPE ssfcrescl,
      wa_buffer   TYPE string,
* Variables declarations
      v_form_name TYPE rs38l_fnam,
      v_len_in    LIKE sood-objlen.

*--------------------------------------------------------------------*
* Selection Screen
*--------------------------------------------------------------------*
SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text-t01.
PARAMETERS:     p_sform TYPE tdsfname OBLIGATORY.
SELECT-OPTIONS: s_email FOR adr6-smtp_addr NO INTERVALS OBLIGATORY.
SELECTION-SCREEN END OF BLOCK b1.

*--------------------------------------------------------------------*
END-OF-SELECTION.
*--------------------------------------------------------------------*
* Call your Smart
  PERFORM smartform_driver_call.

* Convert Smartform data to Hexa
  PERFORM convert_otf_to_xstring.

* Convert Hexa to Binary
  PERFORM str_to_binary.

* Send the email using cl_bcs class
  PERFORM send_multiple_user.

*--------------------------------------------------------------------*
* Sub Routines
*--------------------------------------------------------------------*
FORM smartform_driver_call .

* Call you smartform
  CALL FUNCTION 'SSF_FUNCTION_MODULE_NAME'
    EXPORTING
      formname           = p_sform
    IMPORTING
      fm_name            = v_form_name
    EXCEPTIONS
      no_form            = 1
      no_function_module = 2
      OTHERS             = 3.

  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
    WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.

* Set the control parameter
  w_ctrlop-getotf = abap_true.
  w_ctrlop-no_dialog = abap_true.
  w_compop-tdnoprev = abap_true.
  w_ctrlop-preview = space.
  w_compop-tddest = 'LOCL'.

* Call Smartform
  CALL FUNCTION v_form_name
    EXPORTING
      control_parameters = w_ctrlop
      output_options     = w_compop
      user_settings      = abap_true
    IMPORTING
      job_output_info    = w_return " This will have all output
    EXCEPTIONS
      formatting_error   = 1
      internal_error     = 2
      send_error         = 3
      user_canceled      = 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.

ENDFORM. " SMARTFORM_DRIVER_CALL

FORM str_to_binary .

* Convert Hexa String to Binary format
  CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
    EXPORTING
      buffer     = i_xstring
    TABLES
      binary_tab = i_objbin[].
* Sy-subrc check not required.

ENDFORM. " STR_TO_BINARY

FORM send_mail USING in_mailid.

  DATA: salutation TYPE string.
  DATA: body TYPE string.
  DATA: footer TYPE string.

  DATA: lo_send_request TYPE REF TO cl_bcs,
        lo_document     TYPE REF TO cl_document_bcs,
        lo_sender       TYPE REF TO if_sender_bcs,
        lo_recipient    TYPE REF TO if_recipient_bcs VALUE IS INITIAL,lt_message_body TYPE bcsy_text,
        lx_document_bcs TYPE REF TO cx_document_bcs,
        lv_sent_to_all  TYPE os_boolean.

  "create send request
  lo_send_request = cl_bcs=>create_persistent( ).

  "create message body and subject
  salutation ='Dear Sir/Madam,'.
  APPEND salutation TO lt_message_body.
  APPEND INITIAL LINE TO lt_message_body.

  body = 'Please find the attached the Smartform in PDF format.'.
  APPEND body TO lt_message_body.
  APPEND INITIAL LINE TO lt_message_body.

  footer = 'With Regards,'.
  APPEND footer TO lt_message_body.
  footer = 'www.softat.in.'.
  APPEND footer TO lt_message_body.
  "put your text into the document
  lo_document = cl_document_bcs=>create_document(
  i_type = 'RAW'
  i_text = lt_message_body
  i_subject = 'Smartform in PDF' ).

*DATA: l_size TYPE sood-objlen. " Size of Attachment
*l_size = l_lines * 255.
  TRY.
      lo_document->add_attachment(
      EXPORTING
      i_attachment_type = 'PDF'
      i_attachment_subject = 'Your Smartform'
      i_att_content_hex = i_objbin[] ).
    CATCH cx_document_bcs INTO lx_document_bcs.
  ENDTRY.

* Add attachment
* Pass the document to send request
  lo_send_request->set_document( lo_document ).

  "Create sender
  lo_sender = cl_sapuser_bcs=>create( sy-uname ).

  "Set sender
  lo_send_request->set_sender( lo_sender ).

  "Create recipient
  lo_recipient = cl_cam_address_bcs=>create_internet_address( in_mailid ).

*Set recipient
  lo_send_request->add_recipient(
  EXPORTING
  i_recipient = lo_recipient
  i_express = abap_true
  ).

  lo_send_request->add_recipient( lo_recipient ).

* Send email
  lo_send_request->send(
  EXPORTING
  i_with_error_screen = abap_true
  RECEIVING
  result = lv_sent_to_all ).

CONCATENATE 'Email sent to' in_mailid INTO data(lv_msg) SEPARATED BY space.
  WRITE:/ lv_msg COLOR COL_POSITIVE.
  SKIP.
* Commit Work to send the email
  COMMIT WORK.

ENDFORM.

FORM send_multiple_user.

  DATA: in_mailid TYPE ad_smtpadr.

* Begin of sending email to multiple users
* If business want email to be sent to all users at one time, it can be done

* For now we do not want to send 1 email to multiple users
* Mail has to be sent one email at a time
  LOOP AT s_email.
    CLEAR in_mailid.
    in_mailid = s_email-low.
    PERFORM send_mail USING in_mailid .
  ENDLOOP.

ENDFORM. " SEND_MULTIPLE_USER



FORM convert_otf_to_xstring .

* Get Output Text Format (OTF)
  i_otf[] = w_return-otfdata[].

* Import Binary file and filesize
  CALL FUNCTION 'CONVERT_OTF'
    EXPORTING
      format                = 'PDF'
      max_linewidth         = 132
    IMPORTING
      bin_filesize          = v_len_in
      bin_file              = i_xstring   " This is NOT Binary. This is Hexa
    TABLES
      otf                   = i_otf
      lines                 = i_tline
    EXCEPTIONS
      err_max_linewidth     = 1
      err_format            = 2
      err_conv_not_possible = 3
      OTHERS                = 4.
* Sy-subrc check not checked

ENDFORM.

 

For our test, we made a straightforward smartform with simply a logo and one text line.

Stir things up around town symbol to test the smartform remain solitary.

You would see the capability module connected to this smartform. Stir things up around town symbol once more. Presently you see the boundaries of the capability, hit test symbol once more.

This is our smartform yield.

Presently, our program ought to connect this smartform yield as PDF and email it to different clients.

We should execute our program. Give the smartform name and the email ids. For the test, we are sending just to two clients. You could enter as numerous clients you need.

Execute and you would receive this message (assuming that you are trying our program).

Go to t-code SOST to check whether email truly went out. Indeed!! Indeed it did.

Allow us to browse what this email has for us.

Cool, we have the email body and an association.

Permit us to check accepting that the association is the comparable smartform we truly see above in stay lone mode.

Bingo!! It is in deed the smartform in the PDF design.

 

YOU MAY LIKE THIS

Introduction to SAP ABAP for HANA

Bridging the Gap: Integrating ABAP with Other Cloud Services

ABAP ON CLOUD

WhatsApp WhatsApp us