Application Development Blog Posts
Learn and share on deeper, cross technology development topics such as integration and connectivity, automation, cloud extensibility, developing at scale, and security.
cancel
Showing results for 
Search instead for 
Did you mean: 
hafizul_mollah2
Participant
79,842


Hi All,This blog is with reference to one of the threads(question) in SCN (http://scn.sap.com/thread/3373098) wherein the requirement was to send email with PDF attachment.

As I have implemented the same for one similar requirement I replied to this question and wanted to share the steps/code if required.I have documented all the steps and code in a MS word document but as SCN does not allow to upload MS Word document ,I told to send one email to my personal email ID(mentioned in the reply) and I will send the MS word document.

 

Thereafter I kept on receiving a lot of email requests to share the word document.So thought of creating this blog with all those steps and code so that anyone needs those can get them here in SCN itself .And also they will not need to wait for my email reply instead they can get it immediately here in SCN itself.

 

I will maintain this blog link to that original thread/question.

 

Here are the steps and codes for the development of the class, method to send email with PDF attachment(content of internal table) and also one sample program to test the same.

 

Step1:  Go to tcode SE24-> Provide the class name

 



 

Properties:

 



 

Step2 : Declare the PDF Method: SEND_PDF_FILE

 

PDF: Parameters:

 



 

Parameters list to copy and paste:

 

*----------------------------------------------------------------------------------------------------------------------------------*

IM_FILE_HEADER_TEXT        Importing    Type    STRING                 Header Text

IM_EMAIL_SUBJECT_LINE     Importing   Type    SO_OBJ_DES       Subject Line

IM_EMAIL_BODY_TEXT         Importing    Type    BCSY_TEXT          Body Text

IM_EMAIL_ADDRESS            Importing   Type    ZTP_SMTP_TAB    Email Address

IM_EMAIL_DIST_NAME          Importing   Type   SO_OBJ_NAM       Email Distribution Name

IM_EMAIL_DIST_ID                Importing   Type    SO_OBJ_ID           Email Distribution Id

IM_ATTACHMENT_NAME       Importing   Type    SOOD-OBJDES     Attachment Name

IM_FILE_DELIMETER             Importing   Type    C                          File Delimiter

IM_ITAB_1                             Importing   Type    ZTP_TEXT_TAB      Itab with data content

IM_ITAB_2                             Importing   Type    SOLI_TAB             Itab with column headers

EX_SEND_TO_ALL                Exporting   Type    OS_BOOLEAN      Send Email

IM_EMAIL_DO_NOT               Importing   Type    CHAR1                  Flag to determine whether mail needs to be sent or not

IM_LAYOUT                          Importing    Type    SYPAART             Layout mode Portrait or Landscape

EX_PDF_TABLE                    Exporting   Type    RCL_BAG_TLINE   Contains converted data in PDF format

 

*-------------------------------------------------------------------------------------------------------------------------------------------------------------*

 

Custom types are highlighted , please find them below:

 

Step3 : Custom types needs to be created which is used above:

Email Table Type :



 

Corresponding structure:

 



 

Data Content table type:

 



 

Corresponding structure:

 



 

Exceptions:

 



 

Step4 : Paste the below code in the method body.

Sample code :

*-----------------------------------------------------------------------------------------------------------------------------------*

Please find below the method code.
METHOD send_pdf_file.
*******************************************************************************
* S-H-O-R-T-----D-E-S-C-R-I-P-T-I-O-N *
*******************************************************************************
*Program Description : Method to send email with pdf attatchment. *
*Developer : Hafizul Mollah *
*Brief Description : This method is used to send email with *
* a pdf attachment. It can be used to send the email *
* to multiple email addresses(To, CC,BCC) or to a *
* distribution name or distribution id. *
*******************************************************************************


* CONSTANTS----------------------------------------------------------------*
CONSTANTS:
c_tab TYPE c VALUE cl_bcs_convert=>gc_tab,
c_crlf TYPE c VALUE cl_bcs_convert=>gc_crlf,
gc_tab TYPE c VALUE cl_bcs_convert=>gc_tab,
gc_crlf TYPE c VALUE cl_bcs_convert=>gc_crlf.

* TYPES----------------------------------------------------------------*
TYPES: BEGIN OF ty_itab,
line TYPE ztp_text_struct,
END OF ty_itab,
BEGIN OF ty_itab2,
line TYPE soli,
END OF ty_itab2,
BEGIN OF ty_header,
field TYPE char80,
END OF ty_header,
BEGIN OF ty_field,
field TYPE char40,
END OF ty_field,
BEGIN OF ty_maxlen,
maxlen TYPE i,
END OF ty_maxlen.

* INTERNAL TABLES------------------------------------------------------*
DATA:
li_dlientries TYPE STANDARD TABLE OF sodlienti1,
li_binary_content TYPE solix_tab,
li_main_text TYPE bcsy_text,
li_recipient TYPE ztp_smtp_tab,
i_header TYPE STANDARD TABLE OF ty_header,
i_maxlen TYPE STANDARD TABLE OF ty_maxlen,
li_final TYPE ztp_text_tab,
i_field TYPE STANDARD TABLE OF ty_field.

* STRUCTURES-----------------------------------------------------------*
DATA:
st_itab1 TYPE ty_itab,
st_itab2 TYPE ty_itab2,
st_email_addr TYPE ztp_smtp_struct,
st_dlientry TYPE sodlienti1,
st_body TYPE soli.

* FIELD-SYMBOLS----------------------------------------------------*
FIELD-SYMBOLS: <f_source> TYPE any,
<f_wa_filetab> TYPE any,
<l_xline> TYPE x,
<t_dyntable> TYPE STANDARD TABLE , "Dynamic internal table name
<fs_dyntable> TYPE any , "Field symbol to create work area
<fs_fldval> TYPE any, " Field symbol to assign value
<fs> TYPE any.

* VARIABLES------------------------------------------------------------*
DATA:
wa_header TYPE ty_header,
send_request TYPE REF TO cl_bcs,
document TYPE REF TO cl_document_bcs,
recipient TYPE REF TO if_recipient_bcs,
bcs_exception TYPE REF TO cx_bcs,
l_size TYPE so_obj_len,
l_sent_to_all TYPE os_boolean,
l_dliname TYPE so_obj_nam,
l_dlidata TYPE sodlidati1,
l_string TYPE string,
l_cl_name TYPE string,
l_mailcc TYPE c,
l_mailbcc TYPE c,
l_recipient TYPE ad_smtpadr,
t_newtable TYPE REF TO data,
t_newline TYPE REF TO data,
t_fldcat TYPE lvc_t_fcat,
wa_it_fldcat TYPE lvc_s_fcat,
wa_colno(2) TYPE n,
wa_flname TYPE string,
t_attachment TYPE solisti1 ,
pdf_content TYPE solix_tab,
lp_pdf_size TYPE so_obj_len,
main_text TYPE bcsy_text,
binary_content TYPE solix_tab,
size TYPE so_obj_len,
sent_to_all TYPE os_boolean,
lv_string TYPE string,
wa_tab1 TYPE ztp_text_struct,
wa_tab2 TYPE soli,
pdf_xstring TYPE xstring,
it_pdf_table TYPE rcl_bag_tline,
wa_pdfline LIKE LINE OF it_pdf_table,
l_column_index TYPE sy-index,
l_rc TYPE c,
l_handle TYPE sy-tabix,
l_spoolid TYPE tsp01-rqident,
l_str(255) TYPE c,
params TYPE pri_params,
valid TYPE char1,
l_pdf_bytecount TYPE i,
wa_im_itab_1 TYPE ztp_text_struct,
g_data TYPE char25,
l_output TYPE char40,
l_col_cnt TYPE i,
l_maxcnt TYPE i,
wa_field TYPE ty_field,
wa_maxlen TYPE ty_maxlen,
l_index TYPE char2,
lwa_final TYPE ztp_text_struct,
l_field TYPE string,
l_rec TYPE i,
wa_im_itab_2 TYPE soli,
wa_im_itab_3 TYPE soli,
l_field_conv TYPE string,
l_comp TYPE string,
l_pos TYPE i VALUE 1,
l_sp_id TYPE tsp01_sp0r-rqid_char.



l_field = ' '.

CONCATENATE im_file_header_text
gc_crlf gc_crlf
INTO lv_string.

SET LOCALE LANGUAGE 'E'."added
CALL FUNCTION 'GET_PRINT_PARAMETERS'
EXPORTING
destination = 'LOCL'
*LINE_SIZE = 100
immediately = ' '
no_dialog = 'X'
layout = im_layout
*LINE_COUNT = 500000
line_size = 200
IMPORTING
out_parameters = params
valid = valid.


SPLIT im_file_header_text AT '#' INTO TABLE i_header.
SET LOCALE LANGUAGE 'E'.

NEW-PAGE PRINT ON PARAMETERS params NO DIALOG NO-TITLE NO-HEADING.
LOOP AT i_header INTO wa_header.
WRITE : wa_header-field.
ENDLOOP.


LOOP AT im_itab_2 INTO wa_im_itab_2.
IF sy-tabix = 1.
l_field = wa_im_itab_2.
ELSE.
CONCATENATE l_field wa_im_itab_2 INTO l_field SEPARATED BY '|'.
ENDIF.
ENDLOOP.
APPEND l_field TO li_final.

LOOP AT im_itab_1 INTO wa_im_itab_1.
APPEND wa_im_itab_1 TO li_final.
ENDLOOP.


READ TABLE li_final INTO lwa_final INDEX 1. "reading the columns name
IF sy-subrc = 0.
SPLIT lwa_final AT '|' INTO TABLE i_field.
ENDIF.

DESCRIBE TABLE i_field LINES l_rec.

LOOP AT i_field INTO wa_field.
l_index = sy-tabix.
CONCATENATE 'COL' l_index INTO wa_flname.
wa_it_fldcat-fieldname = wa_flname.
wa_it_fldcat-outputlen = 40.
wa_it_fldcat-datatype = 'CHAR'.
APPEND wa_it_fldcat TO t_fldcat.

CLEAR wa_it_fldcat.

ENDLOOP.

* Create dynamic internal table and assign to FS

CALL METHOD cl_alv_table_create=>create_dynamic_table
EXPORTING
it_fieldcatalog = t_fldcat
IMPORTING
ep_table = t_newtable.

ASSIGN t_newtable->* TO <t_dyntable>.

REFRESH <t_dyntable>.

* Create dynamic work area and assign to FS
CREATE DATA t_newline LIKE LINE OF <t_dyntable>.
ASSIGN t_newline->* TO <fs_dyntable>.
CLEAR <fs_dyntable>.

*****************reading the data value********************************************
REFRESH i_field.
LOOP AT li_final INTO lwa_final.
SPLIT lwa_final AT '|' INTO TABLE i_field.
SET LOCALE LANGUAGE 'E'.
LOOP AT i_field INTO wa_field.
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT'
EXPORTING
input = wa_field-field
IMPORTING
output = l_output.

CONCATENATE l_field_conv l_output INTO l_field_conv RESPECTING BLANKS.

ENDLOOP.
<fs_dyntable> = l_field_conv.
APPEND <fs_dyntable> TO <t_dyntable>.
CLEAR:l_field_conv.
ENDLOOP.

DO l_rec TIMES.
REFRESH: i_field.
l_col_cnt = l_col_cnt + 1.
LOOP AT <t_dyntable> INTO <fs_dyntable>.
IF <fs> IS ASSIGNED.
UNASSIGN <fs>.
ENDIF.
DO.
ASSIGN COMPONENT l_col_cnt OF STRUCTURE <fs_dyntable> TO <fs>.
IF sy-subrc <> 0.
EXIT.
ENDIF.
wa_field-field = <fs>.
*condense wa_field-field.
APPEND wa_field TO i_field.
CLEAR: wa_field.
EXIT.
ENDDO.

ENDLOOP.
*****************calculating tha maximum length of domain values********************************************
LOOP AT i_field INTO wa_field.
l_maxcnt = strlen( wa_field-field ).
IF wa_maxlen-maxlen < l_maxcnt .
wa_maxlen-maxlen = l_maxcnt.
ENDIF.
CLEAR:l_maxcnt.
ENDLOOP.
APPEND wa_maxlen TO i_maxlen .
CLEAR: wa_maxlen,l_maxcnt.
ENDDO.

SKIP 2.
*****************writing the column names and data value********************************************
LOOP AT <t_dyntable> INTO <fs_dyntable>.
IF <fs> IS ASSIGNED.
UNASSIGN <fs>.
ENDIF.
DO.
ASSIGN COMPONENT sy-index OF STRUCTURE <fs_dyntable> TO <fs>.
IF sy-subrc <> 0.
EXIT.
ENDIF.
l_comp = <fs>.
IF sy-index = 1.
WRITE:/ l_comp.
ELSE.
l_rec = sy-index - 1.
READ TABLE i_maxlen INTO wa_maxlen INDEX l_rec.
IF sy-subrc = 0.
l_pos = l_pos + wa_maxlen-maxlen + 1.
WRITE AT l_pos l_comp.
ENDIF.
ENDIF.
CLEAR:l_rec,l_comp.
ENDDO.
l_pos = 1.
ENDLOOP.

NEW-PAGE PRINT OFF.
l_spoolid = sy-spono.
SET LOCALE LANGUAGE 'E'.
CALL FUNCTION 'CONVERT_ABAPSPOOLJOB_2_PDF'
EXPORTING
src_spoolid = l_spoolid "'30221'
* NO_DIALOG = 'X'
dst_device = 'LOCL'
* PDF_DESTINATION =
* NO_BACKGROUND =
* GET_SIZE_FROM_FORMAT =
IMPORTING
pdf_bytecount = l_pdf_bytecount
* PDF_SPOOLID =
* LIST_PAGECOUNT =
* BTC_JOBNAME =
* BTC_JOBCOUNT =
* BIN_FILE =
TABLES
pdf = it_pdf_table
EXCEPTIONS
err_no_abap_spooljob = 1
err_no_spooljob = 2
err_no_permission = 3
err_conv_not_possible = 4
err_bad_destdevice = 5
user_cancelled = 6
err_spoolerror = 7
err_temseerror = 8
err_btcjob_open_failed = 9
err_btcjob_submit_failed = 10
err_btcjob_close_failed = 11
OTHERS = 12.


MOVE l_spoolid TO l_sp_id.

CALL FUNCTION 'RSPO_R_RDELETE_SPOOLREQ'
EXPORTING
spoolid = l_sp_id.


* Map PDF table into 'flat' table acording to Unicode flag
ex_pdf_table = it_pdf_table .

IF im_email_do_not IS INITIAL.
LOOP AT it_pdf_table INTO wa_pdfline.
ASSIGN wa_pdfline TO <l_xline> CASTING.
CONCATENATE pdf_xstring <l_xline> INTO pdf_xstring IN BYTE MODE.
ENDLOOP.

* get PDF xstring and convert it to BCS format
lp_pdf_size = xstrlen( pdf_xstring ).
pdf_content = cl_document_bcs=>xstring_to_solix(
ip_xstring = pdf_xstring ).
TRY.

*-------- create persistent send request ------------------------
send_request = cl_bcs=>create_persistent( ).

*-------- create and set document with attachment ---------------
*create document object from internal table with text
LOOP AT im_email_body_text INTO st_body.
APPEND st_body TO li_main_text.
ENDLOOP.
document = cl_document_bcs=>create_document(
i_type = 'RAW'
i_text = li_main_text
i_subject = im_email_subject_line ).

*add the spread sheet as attachment to document object
document->add_attachment(
i_attachment_type = 'PDF' "#EC NOTEXT
i_attachment_subject = im_attachment_name "#EC NOTEXT
i_attachment_size = lp_pdf_size "Size
i_att_content_hex = pdf_content ).
*add document object to send request
send_request->set_document( document ).

*get email addresses from table
LOOP AT im_email_address INTO st_email_addr.
APPEND st_email_addr TO li_recipient.
ENDLOOP.

*Get email addresses from Distribution list
CALL FUNCTION 'SO_DLI_READ_API1'
EXPORTING
shared_dli = 'X'
dli_id = space
dli_name = im_email_dist_name
IMPORTING
dli_data = l_dlidata
TABLES
dli_entries = li_dlientries
EXCEPTIONS
dli_not_exist = 9001
operation_no_authorization = 9002
parameter_error = 9003
x_error = 9004
OTHERS = 01.

CLEAR st_email_addr.
LOOP AT li_dlientries INTO st_dlientry.
st_email_addr-smtpadr = st_dlientry-member_adr.
st_email_addr-mail_to = 'X'.
APPEND st_email_addr TO li_recipient.
ENDLOOP.

*Get email addresses from Distribution Id
CALL FUNCTION 'SO_DLI_READ_API1'
EXPORTING
shared_dli = 'X'
dli_id = im_email_dist_id
dli_name = space
IMPORTING
dli_data = l_dlidata
TABLES
dli_entries = li_dlientries
EXCEPTIONS
dli_not_exist = 9001
operation_no_authorization = 9002
parameter_error = 9003
x_error = 9004
OTHERS = 01.

LOOP AT li_dlientries INTO st_dlientry.
st_email_addr-smtpadr = st_dlientry-member_adr.
st_email_addr-mail_to = 'X'.
APPEND st_email_addr TO li_recipient.
ENDLOOP.


*--------- add recipient (e-mail address)-----------------------
LOOP AT li_recipient INTO st_email_addr.
l_recipient = st_email_addr-smtpadr.
recipient = cl_cam_address_bcs=>create_internet_address( l_recipient ).
IF st_email_addr-mail_to = 'X'.
CLEAR l_mailcc.
CLEAR l_mailbcc.
ELSE.
IF st_email_addr-mail_cc = 'X'.
l_mailcc = 'X'.
CLEAR l_mailbcc.
ELSE.
IF st_email_addr-mail_bcc = 'X'.
l_mailbcc = 'X'.
CLEAR l_mailcc.
ENDIF.
ENDIF.
ENDIF.
TRY.
CALL METHOD send_request->add_recipient
EXPORTING
i_recipient = recipient
i_express = 'X'
i_copy = l_mailcc
i_blind_copy = l_mailbcc.
ENDTRY .

ENDLOOP.

*add recipient object to send request
*send_request->add_recipient( recipient ).

*---------- send document ---------------------------------------
send_request->send_request->set_link_to_outbox( 'X' ).

l_sent_to_all = send_request->send( i_with_error_screen = 'X' ).

COMMIT WORK.

IF l_sent_to_all IS INITIAL.
ex_send_to_all = '1'.
ELSE.
ex_send_to_all = '0'.
ENDIF.

SUBMIT rsconn01 WITH mode = 'INT'
WITH output = 'X'
AND RETURN.

* ------------ exception handling ----------------------------------
CATCH cx_bcs INTO bcs_exception.
* message i865(so) with bcs_exception->error_type.
ENDTRY.

ENDIF.


ENDMETHOD.

*----------------------------------------------------------------------------------------------------------------------------------*

 

 

Step5:Activate the method and class and write the below small executable test program to test it.Go to SE38 create the below program to test:

 



 

Sample Code :

*-------------------------------------------------------------------------------------------------------------------------------*

Please find below the sample code for the test program.
*&---------------------------------------------------------------------*
*& Report ZTP_SEND_EMAIL
*&
*&-----------------------------------------------------------------------------*
*&Short Description : This is a Test Program which calls the method *
*& 'SEND_PDF_FILE' of the Global Class 'ZCL_SEND_ATTACHMENT'*
*& and will send the email with a PDF file as an attachment.*
*&-----------------------------------------------------------------------------*

REPORT ztp_send_email.

PARAMETERS : p_email TYPE char100 DEFAULT 'hafizul.mollah@test.com'."Email ID in the format test@xxxx.com

TYPES: BEGIN OF ty_outfield,
outdata TYPE soli,
END OF ty_outfield,
BEGIN OF ty_data,
outdata TYPE ztp_text_struct,
END OF ty_data.

DATA:ls_body TYPE soli,
lt_body TYPE bcsy_text,
lt_headings TYPE soli_tab,
ls_headings TYPE ty_outfield,
ls_data TYPE ty_data,
lt_data TYPE ztp_text_tab,
ls_email TYPE ztp_smtp_struct,
lt_email TYPE ztp_smtp_tab,
l_dist_list TYPE so_obj_nam,
l_dist_list1 TYPE so_obj_id,
pdf_file TYPE rcl_bag_tline,
li_email TYPE ztp_smtp_tab,
lst_email TYPE ztp_smtp_struct,
l_return TYPE os_boolean,
li_body TYPE bcsy_text,
lst_body TYPE soli.


*----Populate Column names in the PDF file--------*

ls_headings-outdata = 'PERNR'. APPEND ls_headings TO lt_headings.
ls_headings-outdata = 'LNAME'. APPEND ls_headings TO lt_headings.
ls_headings-outdata = 'FNAME'. APPEND ls_headings TO lt_headings.

*----Populate Actual data names in the PDF file(Use Internal Table content here)--------*
ls_data-outdata = '10000001|Romo|Tony'. APPEND ls_data TO lt_data.
ls_data-outdata = '10000002|Favre|Brett'. APPEND ls_data TO lt_data.

*----Populate Email IDs here(To ,CC , Bcc etc.)--------*

ls_email-smtpadr = p_email.
ls_email-mail_to = 'X'.
APPEND ls_email TO li_email.
CLEAR : ls_email.

*ls_email-smtpadr = 'souvik.bhattacharya@test.com'.
*ls_email-mail_to = ' '.
*ls_email-mail_cc = 'X'.
*APPEND ls_email TO li_email. "


*----Populate Body of the email here--------*

lst_body = 'Hello User,'.
APPEND lst_body TO li_body. "Body of the email
CLEAR : lst_body.
lst_body = 'Please find attached the PDF file.'.
APPEND lst_body TO li_body. "Body of the email

CALL METHOD zcl_send_attachment=>send_pdf_file
EXPORTING
im_file_header_text = 'Employee Name Details'
im_email_subject_line = 'Employee Details in attached PDF file '
im_email_body_text = li_body
im_email_address = li_email
im_email_dist_name = 'DISTRIBU_LIS'
im_email_dist_id = 'DISTRIBU_LIS'
im_attachment_name = 'Employee_data.pdf'
im_file_delimeter = '|'
im_itab_1 = lt_data
im_itab_2 = lt_headings
im_email_do_not = space
IMPORTING
ex_send_to_all = l_return
EXCEPTIONS
error_transferring = 1.

IF l_return <> 0.

ELSE.
SET LOCALE LANGUAGE 'E'.
COMMIT WORK.
SUBMIT rsconn01 WITH mode = 'INT'
WITH output = 'X'
AND RETURN.

WRITE : 'Email successfully Sent'.
ENDIF.

*------------------------------------------------------------------------------------------------------------------------------*

 

Step6: Activate the program and execute it.Please change the email ID to yours one to receive the email to Lotus Notes / Outlook /Other email configured.

 

Execute the program and test whether the email is sent successfully or not (Check in SOST tcode):

 



 

Open the PDF file attached in the email and make sure the content is correct:

 



 

Please let me know if anyone face any issue while developing or testing it.

 

Thanks & Regrads,

Hafizul Mollah,

SAP ABAP Consultant @Capgemini India Pvt. Ltd.

18 Comments
Labels in this area