Skip to Content
0
Former Member
Oct 04, 2012 at 04:14 AM

Problem with email body and pdf attachement

94 Views

Dear all,

I am trying to generate a pdf attachment (smartform) from a report and send it via email. The email is sent properly but the attachment is getting corrupted and not opening. If I exclude the code for email body (Bold in the code below) then the attachment works properly. So please help me where I m making mistake in coding.

Please refer to the following code I have written.

*********************************************************************************

FORM send_mail .
DATA: l_sub(100) TYPE c.
DATA : fname TYPE rs38l_fnam.

DATA : sloc1 TYPE string.

sloc1 = s_lgort-low.

CONCATENATE 'Stock Report For Storage Location -' sloc1 INTO l_sub.

DATA : gc_text(11) TYPE c VALUE 'Form Output',
gc_tst(100) TYPE c,
gc_testing(100) TYPE c.

DATA:
gs_objtxt LIKE solisti1 OCCURS 10 WITH HEADER LINE,
gs_ctrlop TYPE ssfctrlop, " Smart Forms: Control structure
gs_outopt TYPE ssfcompop, " SAP Smart Forms: Smart Composer (transfer) options
gs_otfdata TYPE ssfcrescl, " Smart Forms: Return value at end of form printing
gt_otf TYPE TABLE OF itcoo, " OTF Structure
gv_bin_filesize TYPE i, " Store the file size
gt_pdf_tab TYPE TABLE OF tline, " SAPscript: Text Lines
gs_docdata TYPE sodocchgi1, " Data of an object which can be changed
gt_reclist TYPE TABLE OF somlreci1, " SAPoffice: Structure of the API Recipient List
gs_reclist TYPE somlreci1, " SAPoffice: Structure of the API Recipient List
gs_objbin TYPE solisti1, " SAPoffice: Single List with Column Length 255
gs_pdf_tab TYPE tline, " Workarea for SAP Script Text Lines
gv_pos TYPE i,
gv_len TYPE i,
gt_objbin TYPE TABLE OF solisti1, " SAPoffice: Single List with Column Length 255
gv_tab_lines LIKE sy-tabix,
gs_objpack TYPE sopcklsti1, " SAPoffice: Description of Imported Object Components
gt_objpack TYPE TABLE OF sopcklsti1. " SAPoffice: Description of Imported Object Components
gs_ctrlop-getotf = 'X'.
gs_ctrlop-device = 'PRINTER'.
gs_ctrlop-preview = ''.
gs_ctrlop-no_dialog = 'X'.
gs_outopt-tddest = 'LOCL'.

CALL FUNCTION 'SSF_FUNCTION_MODULE_NAME'
EXPORTING
formname = 'ZMM_STOCK_FG03'
* VARIANT = ' '
* DIRECT_CALL = ' '
IMPORTING
fm_name = fname
* 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.

CALL FUNCTION fname

EXPORTING
control_parameters = gs_ctrlop
output_options = gs_outopt
user_settings = ''
sloc = sloc1
IMPORTING
job_output_info = gs_otfdata
TABLES
it_summ = it_summ
* 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.

CLEAR gt_otf.
gt_otf[] = gs_otfdata-otfdata[].
* Convert the OTF DATA to SAP Script Text lines
CALL FUNCTION 'CONVERT_OTF'
EXPORTING
format = 'PDF'
max_linewidth = 132
* ARCHIVE_INDEX = ' '
* COPYNUMBER = 0
* ASCII_BIDI_VIS2LOG = ' '
* PDF_DELETE_OTFTAB = ' '
* PDF_USERNAME = ' '
IMPORTING
bin_filesize = gv_bin_filesize
* BIN_FILE =
TABLES
otf = gt_otf
lines = gt_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.

* Assigning the Description of the object sent in the mail
CLEAR gs_docdata.
gs_docdata-obj_name = l_sub.
gs_docdata-obj_descr = l_sub.

* Assigning the email id to Structure of the API Recipient List table
CLEAR : gt_reclist, gs_reclist.
*
gs_reclist-receiver = mail_1.
gs_reclist-rec_type = 'U'.
APPEND gs_reclist TO gt_reclist.
CLEAR : gs_reclist.

gs_reclist-receiver = mail_2.
gs_reclist-rec_type = 'U'.
APPEND gs_reclist TO gt_reclist.
CLEAR : gs_reclist.

gs_reclist-receiver = mail_3.
gs_reclist-rec_type = 'U'.
APPEND gs_reclist TO gt_reclist.
CLEAR : gs_reclist.

gs_reclist-receiver = mail_4.
gs_reclist-rec_type = 'U'.
APPEND gs_reclist TO gt_reclist.
CLEAR : gs_reclist.

gs_reclist-receiver = mail_5.
gs_reclist-rec_type = 'U'.
APPEND gs_reclist TO gt_reclist.
CLEAR : gs_reclist.

* Passing the SAP Script text lines to SAPoffice: Single List with Column Length 255 table
CLEAR : gs_objbin, gs_pdf_tab.
LOOP AT gt_pdf_tab INTO gs_pdf_tab.
gv_pos = 255 - gv_len.
IF gv_pos > 134. "length of pdf_table
gv_pos = 134.
ENDIF.
gs_objbin+gv_len = gs_pdf_tab(gv_pos).
gv_len = gv_len + gv_pos.
IF gv_len = 255. "length of out (contents_bin)
APPEND gs_objbin TO gt_objbin.
CLEAR: gs_objbin, gv_len.
IF gv_pos < 134.
gs_objbin = gs_pdf_tab+gv_pos.
gv_len = 134 - gv_pos.
ENDIF.
ENDIF.
ENDLOOP.
IF gv_len > 0.
APPEND gs_objbin TO gt_objbin.
ENDIF.

gs_objtxt = 'Dear Sir, Please find the attached document for your reference.'.
APPEND gs_objtxt.
gs_objtxt = 'This is a system generated email, please do not reply'.
APPEND gs_objtxt.
DESCRIBE TABLE gs_objtxt LINES gv_tab_lines.
READ TABLE gs_objtxt INDEX gv_tab_lines.
gs_docdata-doc_size = ( gv_tab_lines - 1 ) * 255 + STRLEN( gs_objtxt ).
* Creating the entry for the compressed document
CLEAR gs_objpack-transf_bin.
gs_objpack-head_start = 1.
gs_objpack-head_num = 0.
gs_objpack-body_start = 1.
gs_objpack-body_num = gv_tab_lines.
gs_objpack-doc_type = 'RAW'.
APPEND gs_objpack to gt_objpack.
CLEAR gs_objpack.
gv_tab_lines = 0 .

* Filling the details in SAPoffice: Description of Imported Object Components table
DESCRIBE TABLE gt_objbin LINES gv_tab_lines.
CLEAR gs_objbin.
READ TABLE gt_objbin INTO gs_objbin INDEX gv_tab_lines.
IF sy-subrc = 0.
gs_objpack-doc_size = ( gv_tab_lines - 1 ) * 255 + STRLEN( gs_objbin ).
gs_objpack-transf_bin = 'X'.
gs_objpack-head_start = 1.
gs_objpack-head_num = 1.
gs_objpack-body_start = 1.
gs_objpack-body_num = gv_tab_lines.
gs_objpack-doc_type = 'PDF'.
gs_objpack-obj_name = 'ATTACHMENT'.
gs_objpack-obj_descr = l_sub.
APPEND gs_objpack TO gt_objpack.
ENDIF.

* Sending the Form Output in the PDF format to email
CALL FUNCTION 'SO_NEW_DOCUMENT_ATT_SEND_API1'
EXPORTING
document_data = gs_docdata
put_in_outbox = 'X'
commit_work = 'X'
TABLES
packing_list = gt_objpack
contents_bin = gt_objbin
contents_txt = gs_objtxt
receivers = gt_reclist
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.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ELSE.
WRITE 'Mail Sent Successfully'.
ENDIF.
SUBMIT rsconn01
WITH mode EQ 'INT'
AND RETURN.

ENDFORM. " SEND_MAIL

**********************************************************************************************

Please help me to solve this problem.

Regards,

Vishal