ABAP Program to Send Mail through SAP

0
931

There are many posts like this to send external and internal emails from SAP. I thought to consolidate them. So, here is the code to send email from SAP with and with out attachments.

report zemail.


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 = ‘you@yourcompany.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.

if sy-subrc = 0.
submit rsconn01 with mode = ‘INT’ and return.
endif.

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.

mailhead = ‘TEST.TXT’.
append mailhead.



* File 1
mailbin = ‘This is file 1’.
append mailbin.

describe table mailbin lines tab_lines.

mailpack-transf_bin = ‘X’.
mailpack-head_start = 1.
mailpack-head_num = 1.
mailpack-body_start = 1.
mailpack-body_num = tab_lines.
mailpack-doc_type = ‘TXT’.
mailpack-obj_name = ‘TEST1’.
mailpack-obj_descr = ‘Subject’.
mailpack-doc_size = tab_lines * 255.
append mailpack.



*File 2
mailbin = ‘This is file 2’.
append mailbin.

data: start type i.
data: end type i.

start = tab_lines + 1.

describe table mailbin lines end.

mailpack-transf_bin = ‘X’.
mailpack-head_start = 1.
mailpack-head_num = 1.
mailpack-body_start = start.
mailpack-body_num = end.
mailpack-doc_type = ‘TXT’.
mailpack-obj_name = ‘TEST2’.
mailpack-obj_descr = ‘Subject’.
mailpack-doc_size = tab_lines * 255.
append mailpack.


endform.

*With PDF Attachment:*

CALL FUNCTION ‘SSF_FUNCTION_MODULE_NAME’
EXPORTING
formname = ‘Z_TEST’
IMPORTING
fm_name = v_fname.

CALL FUNCTION v_fname
EXPORTING
control_parameters = x_ctrl_p
IMPORTING
job_output_info = x_output_data.

CALL FUNCTION ‘CONVERT_OTF’
EXPORTING
format = ‘PDF’
max_linewidth = 134
IMPORTING
bin_filesize = v_size
TABLES
otf = x_output_data-otfdata
lines = it_lines
EXCEPTIONS
err_max_linewidth = 1
err_format = 2
err_conv_not_possible = 3
OTHERS = 4.

CALL FUNCTION ‘SX_TABLE_LINE_WIDTH_CHANGE’
EXPORTING
line_width_dst = 255
TABLES
content_in = it_lines
content_out = it_soli
EXCEPTIONS
err_line_width_src_too_long = 1
err_line_width_dst_too_long = 2
err_conv_failed = 3
OTHERS = 4.

CALL FUNCTION ‘FUNC_CONVERT_DATA_ODC01’
EXPORTING
iv_byte_mode = ‘X’
TABLES
it_data = it_lines
et_data = it_table.

*—–To caluculate total number of lines of internal table
DESCRIBE TABLE it_table LINES v_lines.

*—–Create Message Body and Title and Description
it_mess = ‘successfully converted smartform from otf format to pdf’ .
APPEND it_mess.

wa_doc_data-obj_name = ‘smartform’.
wa_doc_data-expiry_dat = sy-datum + 10.
wa_doc_data-obj_descr = ‘smartform’.
wa_doc_data-sensitivty = ‘F’.
wa_doc_data-doc_size = v_lines * 255.
APPEND it_pcklist.

*—–PDF Attachment
it_pcklist-transf_bin = ‘X’.
it_pcklist-head_start = 1.
it_pcklist-head_num = 0.
it_pcklist-body_start = 1.
it_pcklist-doc_size = v_lines_bin * 255 .
it_pcklist-body_num = v_lines.
it_pcklist-doc_type = ‘PDF’.
it_pcklist-obj_name = ‘smartform’.
it_pcklist-obj_descr = ‘smart_desc’.
it_pcklist-obj_langu = ‘E’.
it_pcklist-doc_size = v_lines * 255.
APPEND it_pcklist.

*—–Giving the receiver email-id
CLEAR it_receivers.
it_receivers-receiver = abcd@yahoo.com’.
it_receivers-rec_type = ‘U’.
APPEND it_receivers.

*—–Calling the function module to sending email
CALL FUNCTION ‘SO_NEW_DOCUMENT_ATT_SEND_API1’
EXPORTING
document_data = wa_doc_data
put_in_outbox = ‘X’
commit_work = ‘X’
TABLES
packing_list = it_pcklist
contents_txt = it_mess
contents_hex = it_table
receivers = it_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.

*Source= Vinni

LEAVE A REPLY

Please enter your comment!
Please enter your name here