cancel
Showing results for 
Search instead for 
Did you mean: 

Sending Script output as E-mail

Former Member
0 Kudos

Can any body help me how to use Function module Open_Form to send the output of script as an email.

Accepted Solutions (0)

Answers (3)

Answers (3)

Former Member
0 Kudos

User below code, to send as attachment in PDF format

data: it_ITCPP like ITCPP.

CONSTANTS: c_dev LIKE sy-sysid VALUE 'DEV',

c_no(1) TYPE c VALUE ' ',

c_device(4) TYPE c VALUE 'LOCL'.

  • Spool to PDF conversions

DATA: v_spool_nr LIKE tsp01-rqident,

v_destination LIKE rlgrap-filename,

v_bytecount LIKE tst01-dsize,

v_buffer TYPE string.

DATA:v_pdfspoolid LIKE tsp01-rqident.

  • Binary store for PDF

DATA: BEGIN OF it_pdf_output OCCURS 0.

INCLUDE STRUCTURE tline.

DATA: END OF it_pdf_output.

  • Job Runtime Parameters

DATA: v_eventid LIKE tbtcm-eventid,

v_eventparm LIKE tbtcm-eventparm,

v_external_program_active LIKE tbtcm-xpgactive,

v_jobcount LIKE tbtcm-jobcount,

v_jobname LIKE tbtcm-jobname,

v_stepcount LIKE tbtcm-stepcount,

v_error TYPE sy-subrc,

v_reciever TYPE sy-subrc.

DATA: v_recsize TYPE i.

constants: c_email1 LIKE somlreci1-receiver

  • value 'email id.

DATA: it_message TYPE STANDARD TABLE OF solisti1 INITIAL SIZE 0

WITH HEADER LINE.

DATA: it_attach TYPE STANDARD TABLE OF solisti1 INITIAL SIZE 0

WITH HEADER LINE.

DATA: it_packing_list LIKE sopcklsti1 OCCURS 0 WITH HEADER LINE,

it_contents LIKE solisti1 OCCURS 0 WITH HEADER LINE,

it_receivers LIKE somlreci1 OCCURS 0 WITH HEADER LINE,

it_attachment LIKE solisti1 OCCURS 0 WITH HEADER LINE,

it_object_header LIKE solisti1 OCCURS 0 WITH HEADER LINE,

w_cnt TYPE i,

w_sent_all(1) TYPE c,

w_doc_data LIKE sodocchgi1.

DATA: w_recsize TYPE i.

DATA: v_subject LIKE sodocchgi1-obj_descr,

it_mess_bod LIKE solisti1 OCCURS 0 WITH HEADER LINE,

it_mess_att LIKE solisti1 OCCURS 0 WITH HEADER LINE,

v_sender_type LIKE soextreci1-adr_typ,

v_attachment_desc TYPE so_obj_des,

v_attachment_name TYPE so_obj_nam.

PERFORM PDF_CONVERT.

CHECK RETCODE = 0.

DESCRIBE TABLE it_mess_att LINES v_recsize.

CHECK v_recsize > 0.

PERFORM send_email USING c_email1.

CALL FUNCTION 'CLOSE_FORM'

IMPORTING

  • RESULT = it_ITCPP

  • RDI_RESULT =

  • TABLES

  • OTFDATA =

EXCEPTIONS

  • UNOPENED = 1

  • BAD_PAGEFORMAT_FOR_PRINT = 2

  • SEND_ERROR = 3

  • SPOOL_ERROR = 4

  • CODEPAGE = 5

OTHERS = 1

.

IF SY-SUBRC <> 0.

  • MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO

  • WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

ENDIF.

----


  • FORM send_email *

----


  • --> p_email *

----


FORM send_email USING p_email.

CHECK NOT ( p_email IS INITIAL ).

REFRESH it_mess_bod.

  • Default subject matter

  • gd_subject = 'Subject'.

CONCATENATE 'Order Confirmation for' VBCO3-VBELN INTO v_subject separated by space.

  • gd_attachment_desc = 'Attachname'.

CONCATENATE 'Order Confirmation_' VBCO3-VBELN INTO v_attachment_desc.

  • CONCATENATE 'attach_name' ' ' INTO gd_attachment_name.

it_mess_bod = 'Please see attachment for'.

APPEND it_mess_bod.

it_mess_bod = 'Order Confirmation'.

APPEND it_mess_bod.

  • Send file by email as .xls speadsheet

PERFORM send_file_as_email_attachment

tables it_mess_bod

it_mess_att

using p_email

  • 'Order Acnowldgement'

v_subject

'PDF'

v_attachment_name

v_attachment_desc

  • p_sender

' '

v_sender_type

changing v_error

v_reciever.

ENDFORM.

*&----


**& Form SEND_FILE_AS_EMAIL_ATTACHMENT

*&----


    • Send email

*----


FORM send_file_as_email_attachment tables it_message

it_attach

using p_email

p_mtitle

p_format

p_filename

p_attdescription

p_sender_address

p_sender_addres_type

changing p_error

p_reciever.

DATA: ld_error TYPE sy-subrc,

ld_reciever TYPE sy-subrc,

ld_mtitle LIKE sodocchgi1-obj_descr,

ld_email LIKE somlreci1-receiver,

ld_format TYPE so_obj_tp ,

ld_attdescription TYPE so_obj_nam ,

ld_attfilename TYPE so_obj_des ,

ld_sender_address LIKE soextreci1-receiver,

ld_sender_address_type LIKE soextreci1-adr_typ,

ld_receiver LIKE sy-subrc.

data: t_packing_list like sopcklsti1 occurs 0 with header line,

t_contents like solisti1 occurs 0 with header line,

t_receivers like somlreci1 occurs 0 with header line,

t_attachment like solisti1 occurs 0 with header line,

t_object_header like solisti1 occurs 0 with header line,

w_cnt type i,

w_sent_all(1) type c,

w_doc_data like sodocchgi1.

ld_email = p_email.

ld_mtitle = p_mtitle.

ld_format = p_format.

ld_attdescription = p_attdescription.

ld_attfilename = p_filename.

ld_sender_address = p_sender_address.

ld_sender_address_type = p_sender_addres_type.

  • Fill the document data.

w_doc_data-doc_size = 1.

  • Populate the subject/generic message attributes

w_doc_data-obj_langu = sy-langu.

w_doc_data-obj_name = 'SAPRPT'.

w_doc_data-obj_descr = ld_mtitle .

w_doc_data-sensitivty = 'F'.

  • Fill the document data and get size of attachment

CLEAR w_doc_data.

READ TABLE it_attach INDEX w_cnt.

w_doc_data-doc_size =

( w_cnt - 1 ) * 255 + STRLEN( it_attach ).

w_doc_data-obj_langu = sy-langu.

w_doc_data-obj_name = 'SAPRPT'.

w_doc_data-obj_descr = ld_mtitle.

w_doc_data-sensitivty = 'F'.

CLEAR t_attachment.

REFRESH t_attachment.

t_attachment[] = it_attach[].

  • Describe the body of the message

CLEAR t_packing_list.

REFRESH t_packing_list.

t_packing_list-transf_bin = space.

t_packing_list-head_start = 1.

t_packing_list-head_num = 0.

t_packing_list-body_start = 1.

DESCRIBE TABLE it_message LINES t_packing_list-body_num.

t_packing_list-doc_type = 'RAW'.

APPEND t_packing_list.

  • Create attachment notification

t_packing_list-transf_bin = 'X'.

t_packing_list-head_start = 1.

t_packing_list-head_num = 1.

t_packing_list-body_start = 1.

DESCRIBE TABLE t_attachment LINES t_packing_list-body_num.

t_packing_list-doc_type = ld_format.

t_packing_list-obj_descr = ld_attdescription.

t_packing_list-obj_name = ld_attfilename.

t_packing_list-doc_size = t_packing_list-body_num * 255.

APPEND t_packing_list.

  • Add the recipients email address

CLEAR t_receivers.

REFRESH t_receivers.

t_receivers-receiver = ld_email.

t_receivers-rec_type = 'U'.

t_receivers-com_type = 'INT'.

t_receivers-notif_del = 'X'.

t_receivers-notif_ndel = 'X'.

APPEND t_receivers.

CALL FUNCTION 'SO_DOCUMENT_SEND_API1'

EXPORTING

document_data = w_doc_data

put_in_outbox = 'X'

sender_address = ld_sender_address

sender_address_type = ld_sender_address_type

  • commit_work = 'X'

IMPORTING

sent_to_all = w_sent_all

TABLES

packing_list = t_packing_list

contents_bin = t_attachment

contents_txt = it_message

receivers = t_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.

  • Populate zerror return code

ld_error = sy-subrc.

  • Populate zreceiver return code

LOOP AT t_receivers.

ld_receiver = t_receivers-retrn_code.

ENDLOOP.

ENDFORM.

*&----


**& Form PDF_CONVERT

*&----


    • text

*----


    • --> p1 text

    • <-- p2 text

*----


FORM PDF_CONVERT .

CALL FUNCTION 'CONVERT_OTFSPOOLJOB_2_PDF'

EXPORTING

src_spoolid = it_itcpp-TDSPOOLID

no_dialog = c_no

  • DST_DEVICE = 'PDF1'

  • PDF_DESTINATION =

IMPORTING

pdf_bytecount = v_bytecount

pdf_spoolid = v_pdfspoolid

  • OTF_PAGECOUNT =

btc_jobname = v_jobname

btc_jobcount = v_jobcount

TABLES

pdf = it_pdf_output

EXCEPTIONS

err_no_otf_spooljob = 1

err_no_spooljob = 2

err_no_permission = 3

err_conv_not_possible = 4

err_bad_dstdevice = 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

.

IF sy-subrc <> 0.

MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno

WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.

ENDIF.

CHECK sy-subrc = 0.

  • Transfer the 132-long strings to 255-long strings

LOOP AT it_pdf_output.

TRANSLATE it_pdf_output USING ' ~'.

CONCATENATE v_buffer it_pdf_output INTO v_buffer.

ENDLOOP.

TRANSLATE v_buffer USING '~ '.

DO.

it_mess_att = v_buffer.

APPEND it_mess_att.

SHIFT v_buffer LEFT BY 255 PLACES.

IF v_buffer IS INITIAL.

EXIT.

ENDIF.

ENDDO.

ENDFORM. " PDF_CONVERT

Former Member
0 Kudos

using Open_form mail option is going to SAP inbox, if you want send a mail to inbox u can use below logic...

&----


*& Form SEND_FILE_AS_EMAIL_ATTACHMENT

&----


Send email

-


FORM send_file_as_email_attachment tables it_message

it_attach

using p_email

p_mtitle

p_format

p_filename

p_attdescription

p_sender_address

p_sender_addres_type

changing p_error

p_reciever.

DATA: ld_error TYPE sy-subrc,

ld_reciever TYPE sy-subrc,

ld_mtitle LIKE sodocchgi1-obj_descr,

ld_email LIKE somlreci1-receiver,

ld_format TYPE so_obj_tp ,

ld_attdescription TYPE so_obj_nam ,

ld_attfilename TYPE so_obj_des ,

ld_sender_address LIKE soextreci1-receiver,

ld_sender_address_type LIKE soextreci1-adr_typ,

ld_receiver LIKE sy-subrc.

data: t_packing_list like sopcklsti1 occurs 0 with header line,

t_contents like solisti1 occurs 0 with header line,

t_receivers like somlreci1 occurs 0 with header line,

t_attachment like solisti1 occurs 0 with header line,

t_object_header like solisti1 occurs 0 with header line,

w_cnt type i,

w_sent_all(1) type c,

w_doc_data like sodocchgi1.

ld_email = p_email.

ld_mtitle = p_mtitle.

ld_format = p_format.

ld_attdescription = p_attdescription.

ld_attfilename = p_filename.

ld_sender_address = p_sender_address.

ld_sender_address_type = p_sender_addres_type.

Fill the document data.

w_doc_data-doc_size = 1.

Populate the subject/generic message attributes

w_doc_data-obj_langu = sy-langu.

w_doc_data-obj_name = 'SAPRPT'.

w_doc_data-obj_descr = ld_mtitle .

w_doc_data-sensitivty = 'F'.

Fill the document data and get size of attachment

CLEAR w_doc_data.

READ TABLE it_attach INDEX w_cnt.

w_doc_data-doc_size =

( w_cnt - 1 ) * 255 + STRLEN( it_attach ).

w_doc_data-obj_langu = sy-langu.

w_doc_data-obj_name = 'SAPRPT'.

w_doc_data-obj_descr = ld_mtitle.

w_doc_data-sensitivty = 'F'.

CLEAR t_attachment.

REFRESH t_attachment.

t_attachment] = it_attach[.

Describe the body of the message

CLEAR t_packing_list.

REFRESH t_packing_list.

t_packing_list-transf_bin = space.

t_packing_list-head_start = 1.

t_packing_list-head_num = 0.

t_packing_list-body_start = 1.

DESCRIBE TABLE it_message LINES t_packing_list-body_num.

t_packing_list-doc_type = 'RAW'.

APPEND t_packing_list.

Create attachment notification

t_packing_list-transf_bin = 'X'.

t_packing_list-head_start = 1.

t_packing_list-head_num = 1.

t_packing_list-body_start = 1.

DESCRIBE TABLE t_attachment LINES t_packing_list-body_num.

t_packing_list-doc_type = ld_format.

t_packing_list-obj_descr = ld_attdescription.

t_packing_list-obj_name = ld_attfilename.

t_packing_list-doc_size = t_packing_list-body_num * 255.

APPEND t_packing_list.

Add the recipients email address

CLEAR t_receivers.

REFRESH t_receivers.

t_receivers-receiver = ld_email.

t_receivers-rec_type = 'U'.

t_receivers-com_type = 'INT'.

t_receivers-notif_del = 'X'.

t_receivers-notif_ndel = 'X'.

APPEND t_receivers.

CALL FUNCTION 'SO_DOCUMENT_SEND_API1'

EXPORTING

document_data = w_doc_data

put_in_outbox = 'X'

sender_address = ld_sender_address

sender_address_type = ld_sender_address_type

commit_work = 'X'

IMPORTING

sent_to_all = w_sent_all

TABLES

packing_list = t_packing_list

contents_bin = t_attachment

contents_txt = it_message

receivers = t_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.

Populate zerror return code

ld_error = sy-subrc.

Populate zreceiver return code

LOOP AT t_receivers.

ld_receiver = t_receivers-retrn_code.

ENDLOOP.

ENDFORM.

former_member585060
Active Contributor
0 Kudos

Hi,

Just check this following blog of Erwan LE BRUN he clearly explained the steps, it might be helpful to you.

/people/erwan.lebrun/blog/2007/04/16/tips-tricks-email-fax-through-the-same-output-type

Regards

Bala Krishna