cancel
Showing results for 
Search instead for 
Did you mean: 

How to Send a PDF attachment throug mail?

Former Member
0 Kudos

Hello Experts,

I have a requirement in which have a script output, i have to convert it into PDF format and this PDF has to be sent as an attachment through mail to the consern person.

I have converted the Script output to PDF format using the function module 'CONVERT_OTF'. I am able to generate a mail without attachment using function module 'SO_NEW_DOCUMENT_ATT_SEND_API1'.

Can anyone guide me on how to add the generated PDF as an attachment to the mail?

Regards,

Nikhil.

Accepted Solutions (0)

Answers (3)

Answers (3)

Former Member
0 Kudos

Hi,

Welcome To SDN!!

see the sample code

and do accordingly

-


INCLUDE ZINCUSMAIL *

-


include <symbol>.

data : i_doc_data like sodocchgi1.

data : begin of i_pack_list occurs 0.

include structure sopcklsti1.

data : end of i_pack_list.

data : begin of i_receivers occurs 0.

include structure somlreci1.

data : end of i_receivers.

data : begin of i_contents occurs 0.

include structure solisti1.

data : end of i_contents.

data : begin of i_header occurs 0.

include structure solisti1.

data : end of i_header.

data : begin of i_att occurs 0.

include structure solisti1.

data : end of i_att.

Internal Table for Internet address.

data: begin of it_inad occurs 0,

kunnr like kna1-kunnr, " Customer Code

name1 like kna1-name1, " Customer Name

ssobl like knkk-ssobl, " Security Deposit

klimk like knkk-klimk, " Credit Limit

opbal like bsid-wrbtr, " Opening Balance

clbal like bsid-wrbtr, " Closing Balance

smtp like adr6-smtp_addr, " Internet mail (SMTP) address

end of it_inad.

data : pdf_line(134),

asdf like pdf_line occurs 0 with header line.

data : pdf_table like tline occurs 0 with header line,

pdf_fsize type i.

data : stuff(65000),

len type i,

pos type i,

tab_lines like sy-tabix.

data: spoolid type tsp01-rqident,

spdel type tsp01sys.

data: v_gjahrt like bsid-gjahr,

fmondest(10),

tmondest(10),

kunnr1 like kna1-kunnr,

gjah(4),

fmon(10).

&----


*& Form hide_write

&----


form hide_write.

new-page print on

line-size 160

line-count 58

no-title

no-heading

destination 'LOCL'

immediately ' '

new list identification 'X'

no dialog.

set blank lines on.

endform. " hide_write

&----


*& Form end_write

&----


form end_write using kunnr1.

set blank lines off.

new-page print off.

***Using Spoolid we are getting PDF formated file

spoolid = spdel-rqident = sy-spono.

spdel-sys = sy-sysid.

call function 'CONVERT_ABAPSPOOLJOB_2_PDF'

exporting

src_spoolid = spoolid

no_dialog = 'X'

importing

pdf_bytecount = pdf_fsize

tables

pdf = pdf_table

exceptions

others = 0.

***Delleting Spool request

call function 'RSPO_IDELETE_SPOOLREQ'

exporting

spoolreq = spdel

exceptions

others = 2.

***Converting PDF table line size 134 into standard list size 255

loop at pdf_table into pdf_line.

if pos = 34170.

perform attach.

endif.

stuff+pos(134) = pdf_line.

add 134 to pos.

endloop.

if not ( stuff is initial ).

perform attach.

endif.

clear pdf_line.

clear pdf_table[].

describe table i_att lines tab_lines.

i_pack_list-transf_bin = 'X'.

i_pack_list-head_start = '1'.

i_pack_list-head_num = '1'.

i_pack_list-body_start = '1'.

i_pack_list-body_num = tab_lines.

i_pack_list-doc_type = 'PDF'.

i_pack_list-obj_name = 'LedgerMail'.

concatenate fmon '-' gjah into i_pack_list-obj_descr.

*i_pack_list-obj_descr = '2092-Oct03'.

i_pack_list-obj_langu = 'E'.

i_pack_list-doc_size = tab_lines * 255.

append i_pack_list.

***Data for receivers list

loop at it_inad where kunnr eq kunnr1.

i_receivers-receiver = it_inad-smtp.

i_receivers-rec_type = 'U'.

i_receivers-rec_date = sy-datum.

i_receivers-express = 'X'.

i_receivers-com_type = 'INT'.

i_receivers-notif_del = 'X'.

append i_receivers.

endloop.

call function 'SO_NEW_DOCUMENT_ATT_SEND_API1'

exporting

document_data = i_doc_data

PUT_IN_OUTBOX = ' '

IMPORTING

SENT_TO_ALL =

NEW_OBJECT_ID =

tables

packing_list = i_pack_list

object_header = i_header

contents_bin = i_att

contents_txt = i_contents

receivers = i_receivers

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

.

refresh i_att. clear i_att.

refresh i_receivers. clear i_receivers.

delete i_pack_list where doc_type = 'PDF'.

*refresh i_header.

*refresh i_contents.

*clear i_doc_data.

endform. " end_write

&----


*& Form doc_data

&----


form doc_data using fmondest v_gjahrt.

gjah = v_gjahrt.

fmon = fmondest.

***Data for Document Data

i_doc_data-obj_name = 'LedgerMail'.

concatenate 'Customer Ledger for : ' fmondest gjah

into i_doc_data-obj_descr separated by space.

i_doc_data-obj_langu = 'E'.

i_doc_data-obj_prio = '1'.

i_doc_data-no_change = 'X'.

i_doc_data-doc_size = '5101'.

***Data for Packing list

i_pack_list-head_start = '1'.

i_pack_list-head_num = '1'.

i_pack_list-body_start = '1'.

i_pack_list-body_num = '20'.

i_pack_list-doc_type = 'RAW'.

i_pack_list-obj_langu = 'E'.

append i_pack_list.

***Data for Header

i_header-line = 'Header'. append i_header.

***Data for contents

i_contents-line = 'Dear Customer,'. append i_contents.

i_contents-line = ' '. append i_contents.

concatenate 'Please find your enclosed Ledger for the month of : '

fmondest gjah into i_contents-line separated by space.

append i_contents.

i_contents-line = ' '. append i_contents.

i_contents-line = 'This is a computer generated document and does not

require a signature.'. append i_contents.

i_contents-line = ' '. append i_contents.

i_contents-line = 'Note : If you do not have Acrobat Reader please click

on the below link.'. append i_contents.

i_contents-line = ' '. append i_contents.

i_contents-line = 'http://www.adobe.com/products/acrobat/readstep2.html'

. append i_contents.

i_contents-line = ' '. append i_contents.

i_contents-line = ' '. append i_contents.

i_contents-line = ' '. append i_contents.

i_contents-line = ' '. append i_contents.

i_contents-line = ' '. append i_contents.

i_contents-line = ' '. append i_contents.

i_contents-line = ' '. append i_contents.

i_contents-line = ' '. append i_contents.

i_contents-line = ' '. append i_contents.

endform. " doc_data

&----


*& Form attach

&----


form attach.

clear pos.

len = strlen( stuff ).

while len > 0.

subtract 255 from len.

i_att = stuff+pos(255).

append i_att.

add 255 to pos.

endwhile.

clear pos.

clear stuff.

endform. " attach

Check this link

Regards

Kiran

Former Member
0 Kudos

Hi Kiran,

I am able to convert the script o/p to PDF and then generate a mail with this PDF as an attachment.

But i am facing one problem here. I am not able to see the script output in the print preview and neither able to print the o/p. When i execute the print program, i get the print preview screen and when i execute it i dont see the print preview, directly a mail is sent. It is happening because of the field TDGETOTF within ITCPO structure. When i pass 'X' to it i am able to send mail without print preview and when i comment it, i am able to see the print preview but the mail cannot be sent because OTF data is not filled.

How can i get the print preview as well as the mail.

Regards,

Nikhil.

Former Member
0 Kudos

Hi,

this is the BEst Link:

http://www.sapdevelopment.co.uk/reporting/rep_spooltopdf.htm

Best LInk:

/people/pavan.bayyapu/blog/2005/08/30/sending-html-email-from-sap-crmerp

Observe this:

REPORT Z_SCRIPT .

DATA: itcpo LIKE itcpo,

tab_lines LIKE sy-tabix.

Variables for EMAIL functionality

DATA: maildata LIKE sodocchgi1.

DATA: mailpack LIKE sopcklsti1 OCCURS 2 WITH HEADER LINE.

DATA: mailhead LIKE solisti1 OCCURS 1 WITH HEADER LINE.

DATA: mailbin LIKE solisti1 OCCURS 10 WITH HEADER LINE.

DATA: mailtxt LIKE solisti1 OCCURS 10 WITH HEADER LINE.

DATA: mailrec LIKE somlrec90 OCCURS 0 WITH HEADER LINE.

DATA: solisti1 LIKE solisti1 OCCURS 0 WITH HEADER LINE.

*PERFORM send_form_via_email.

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

FORM SEND_FORM_VIA_EMAIL *

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

FORM send_form_via_email.

CLEAR: maildata, mailtxt, mailbin, mailpack, mailhead, mailrec.

REFRESH: mailtxt, mailbin, mailpack, mailhead, mailrec.

Creation of the document to be sent File Name

maildata-obj_name = 'TEST'.

Mail Subject

maildata-obj_descr = 'Subject'.

Mail Contents

mailtxt-line = 'Here is your file'.

APPEND mailtxt.

Prepare Packing List

PERFORM prepare_packing_list.

Set recipient - email address here!!!

mailrec-receiver = 'email.COM'.

mailrec-rec_type = 'U'.

APPEND mailrec.

Sending the document

CALL FUNCTION 'SO_NEW_DOCUMENT_ATT_SEND_API1'

EXPORTING

document_data = maildata

put_in_outbox = ' '

TABLES

packing_list = mailpack

object_header = mailhead

contents_bin = mailbin

contents_txt = mailtxt

receivers = mailrec

EXCEPTIONS

too_many_receivers = 1

document_not_sent = 2

operation_no_authorization = 4

OTHERS = 99.

ENDFORM.

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

Form PREPARE_PACKING_LIST

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

FORM prepare_packing_list.

CLEAR: mailpack, mailbin, mailhead.

REFRESH: mailpack, mailbin, mailhead.

DESCRIBE TABLE mailtxt LINES tab_lines.

READ TABLE mailtxt INDEX tab_lines.

maildata-doc_size = ( tab_lines - 1 ) * 255 + STRLEN( mailtxt ).

Creation of the entry for the compressed document

CLEAR mailpack-transf_bin.

mailpack-head_start = 1.

mailpack-head_num = 0.

mailpack-body_start = 1.

mailpack-body_num = tab_lines.

mailpack-doc_type = 'RAW'.

APPEND mailpack.

Creation of the document attachment

This form gets the OTF code from the SAPscript form.

If you already have your OTF code, I believe that you may

be able to skip this form. just do the following code, looping thru

your SOLISTI1 and updating MAILBIN.

PERFORM get_otf_code.

LOOP AT solisti1.

MOVE-CORRESPONDING solisti1 TO mailbin.

APPEND mailbin.

ENDLOOP.

DESCRIBE TABLE mailbin LINES tab_lines.

mailhead = 'TEST.OTF'.

APPEND mailhead.

Creation of the entry for the compressed attachment

mailpack-transf_bin = 'X'.

mailpack-head_start = 1.

mailpack-head_num = 1.

mailpack-body_start = 1.

mailpack-body_num = tab_lines.

mailpack-doc_type = 'OTF'.

mailpack-obj_name = 'TEST'.

mailpack-obj_descr = 'Subject'.

mailpack-doc_size = tab_lines * 255.

APPEND mailpack.

ENDFORM.

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

Form GET_OTF_CODE

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

FORM get_otf_code.

DATA: BEGIN OF otf OCCURS 0.

INCLUDE STRUCTURE itcoo .

DATA: END OF otf.

DATA: itcpo LIKE itcpo.

DATA: itcpp LIKE itcpp.

CLEAR itcpo.

itcpo-tdgetotf = 'X'.

Start writing OTF code

CALL FUNCTION 'OPEN_FORM'

EXPORTING

form = 'Z08V3_COLLI'

language = sy-langu

options = itcpo

dialog = ' '

EXCEPTIONS

OTHERS = 1.

CALL FUNCTION 'START_FORM'

EXCEPTIONS

error_message = 01

OTHERS = 02.

CALL FUNCTION 'WRITE_FORM'

EXPORTING

window = 'MAIN'

EXCEPTIONS

error_message = 01

OTHERS = 02.

Close up Form and get OTF code

CALL FUNCTION 'END_FORM'

EXCEPTIONS

error_message = 01

OTHERS = 02.

MOVE-CORRESPONDING itcpo TO itcpp.

CALL FUNCTION 'CLOSE_FORM'

IMPORTING

result = itcpp

TABLES

otfdata = otf

EXCEPTIONS

OTHERS = 1.

Move OTF code to structure SOLI form email

CLEAR solisti1. REFRESH solisti1.

LOOP AT otf.

solisti1-line = otf.

APPEND solisti1.

ENDLOOP.

ENDFORM

Below points is for ur testing purpose.

U can check the status of the mail in the transaction SOST.

Execute this transaction for the date range. If the traffic light is in yellow colour then do like this.

Utilities->Start send process->Give trans method as INT and press enter. Mail will go immediately.

If the above logic don't work for u then TRY FM SO_OBJECT_SEND for sending mail.

Regards,

SHIVA(reward if helpful).

Former Member
0 Kudos

Hi,

For creating PDF and sending as email attachment, Proceed as below:

1. While closing the form using FM: CLOSE_FORM fetch the OTFDATA.

2. Pass the same to FM: CONVERT_OTF_2_PDF, which returns the converted data in tables parameter LINES.

3. Now use the converted data to send the mail as per the link i have provided.

Reward points if helpful

Thanks,

suma.