Send E-Mails with File Attachments from ABAP

You may use the sample program below to send an E-Mail containing a file attachment.

If you have already scheduled RSCONN01 (SCOT), you can delete the lines submitting RSCONN01. Otherwise, the user would have to wait until the E-Mail is actually sent.

*&———————————————————————*
*& Report ZGILL_SENDMAIL_PDF *
*& *
*&———————————————————————*
*& *
*& *
*&———————————————————————*

report zgill_sendmail_pdf .

*———————————————————-
* Data
*———————————————————-

data: docdata like sodocchgi1,
objpack like sopcklsti1 occurs 1 with header line,
objhead like solisti1 occurs 1 with header line,
objtxt like solisti1 occurs 10 with header line,
objbin like solisti1 occurs 10 with header line,
objhex like solix occurs 10 with header line,
reclist like somlreci1 occurs 1 with header line.
data: tab_lines type i,
doc_size type i,
att_type like soodk-objtp.
data: listobject like abaplist occurs 1 with header line.

*———————————————————-
* FORM
*———————————————————-
form ml_customize using objname objdesc.

*———– Clear Variables
clear docdata.
refresh objpack.
clear objpack.
refresh objhead.
refresh objtxt.
clear objtxt.
refresh objbin.
clear objbin.
refresh objhex.
clear objhex.
refresh reclist.
clear reclist.
refresh listobject.
clear listobject.
clear tab_lines.
clear doc_size.
clear att_type.

*——— Set Variables
docdata-obj_name = objname.
docdata-obj_descr = objdesc.

endform. “ml_customize

*———————————————————-
* FORM
*———————————————————-
form ml_addrecp using preceiver prec_type.

clear reclist.
reclist-receiver = preceiver.
reclist-rec_type = prec_type.
append reclist.

endform. “ml_customize

*———————————————————-
* FORM
*———————————————————-
form ml_addtxt using ptxt.

clear objtxt.
objtxt = ptxt.
append objtxt.

endform. “ml_customize

*———————————————————-
* FORM
*———————————————————-
form ml_prepare using bypassmemory whatatt_type whatname.

if bypassmemory = ”.
*——— Fetch List From Memory

call function ‘LIST_FROM_MEMORY’
tables
listobject = listobject
exceptions
others = 1.

if sy-subrc 0.
message id ’61’ type ‘E’ number ‘731’
with ‘LIST_FROM_MEMORY’.
endif.

call function ‘TABLE_COMPRESS’
* IMPORTING
* COMPRESSED_SIZE =
tables
in = listobject
out = objbin
exceptions
others = 1
.
if sy-subrc 0.
message id ’61’ type ‘E’ number ‘731’
with ‘TABLE_COMPRESS’.
endif.

endif.

* ———–
* Header Data
* Already Done Thru FM
* ———–

* ———–
* Main Text
* Already Done Thru FM
* ———–

* ———–
* Packing Info For Text Data
* ———–

describe table objtxt lines tab_lines.
read table objtxt index tab_lines.
docdata-doc_size = ( tab_lines – 1 ) * 255 + strlen( objtxt ).
clear objpack-transf_bin.
objpack-head_start = 1.
objpack-head_num = 0.
objpack-body_start = 1.
objpack-body_num = tab_lines.
objpack-doc_type = ‘TXT’.
append objpack.

* ———–
* Packing Info Attachment
* ———–

att_type = whatatt_type..
describe table objbin lines tab_lines.
read table objbin index tab_lines.
objpack-doc_size = ( tab_lines – 1 ) * 255 + strlen( objbin ).
objpack-transf_bin = ‘X’.
objpack-head_start = 1.
objpack-head_num = 0.
objpack-body_start = 1.
objpack-body_num = tab_lines.
objpack-doc_type = att_type.
objpack-obj_name = ‘ATTACHMENT’.
objpack-obj_descr = whatname.
append objpack.

* ———–
* Receiver List
* Already done thru fm
* ———–

endform. “ml_prepare

*———————————————————-
* FORM
*———————————————————-

form ml_dosend.

call function ‘SO_NEW_DOCUMENT_ATT_SEND_API1’
exporting
document_data = docdata
put_in_outbox = ‘X’
commit_work = ‘X’ “used from rel. 6.10
* IMPORTING
* SENT_TO_ALL =
* NEW_OBJECT_ID =
tables
packing_list = objpack
object_header = objhead
contents_bin = objbin
contents_txt = objtxt
* CONTENTS_HEX = objhex
* OBJECT_PARA =
* object_parb =
receivers = 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 ‘SO’ type ‘S’ number ‘023’
with docdata-obj_name.
endif.

endform. “ml_customize

*———————————————————-
* FORM
*———————————————————-
form ml_spooltopdf using whatspoolid.

data : pdf like tline occurs 0 with header line.

*————– Call Function

call function ‘CONVERT_OTFSPOOLJOB_2_PDF’
exporting
src_spoolid = whatspoolid
tables
pdf = pdf
exceptions
err_no_otf_spooljob = 1
others = 12.

*————- Convert

perform doconv tables pdf objbin.

endform. “ml_spooltopdf

*———————————————————-
* FORM
*———————————————————-

form doconv tables
mypdf structure tline
outbin structure solisti1.

*——– Data
data : pos type i.
data : len type i.

*———— Loop And Put Data

loop at mypdf.
pos = 255 – len.
if pos > 134. “length of pdf_table
pos = 134.
endif.
outbin+len = mypdf(pos).
len = len + pos.
if len = 255. “length of out (contents_bin)
append outbin.
clear: outbin, len.
if pos < 134.
outbin = mypdf+pos.
len = 134 – pos.
endif.
endif.
endloop.
if len > 0.
append outbin.
endif.

endform. “doconv

*———————————————————————-
* DATA
*———————————————————————-

data : itab like tline occurs 0 with header line.
data : file_name type string.
data : path like pcfile-path.
data : extension(5) type c.
data : name(100) type c.

*———————————————————————-
* SELECTION SCREEN
*———————————————————————-

parameters : receiver type somlreci1-receiver lower case default
‘AMIT_TYAGI@GILLETTE.COM’.
parameters : p_file like rlgrap-filename
obligatory default ‘C:\TEMP\SALARY_SLIP1.PDF’.

*———————————————————————-
* AT SELECTION SCREEN
*———————————————————————-

at selection-screen on value-request for p_file.
clear p_file.

call function ‘F4_FILENAME’
importing
file_name = p_file.

*———————————————————————-
* START-OF-SELECTION
*———————————————————————-

start-of-selection.
perform ml_customize using ‘Tst’ ‘Testing’.
perform ml_addrecp using receiver ‘U’.
perform upl.
perform doconv tables itab objbin.
perform ml_prepare using ‘X’ extension name.
perform ml_dosend.

*—————————-

submit rsconn01
with mode eq ‘INT’
and return.

*———————————————————————-
* FORM
*———————————————————————-

form upl.

file_name = p_file.

call function ‘GUI_UPLOAD’
exporting
filename = file_name
filetype = ‘BIN’
tables
data_tab = itab
exceptions
file_open_error = 1
file_read_error = 2
no_batch = 3
gui_refuse_filetransfer = 4
invalid_type = 5
no_authority = 6
unknown_error = 7
bad_data_format = 8
header_not_allowed = 9
separator_not_allowed = 10
header_too_long = 11
unknown_dp_error = 12
access_denied = 13
dp_out_of_memory = 14
disk_full = 15
dp_timeout = 16
others = 17.

path = file_name.

call function ‘PC_SPLIT_COMPLETE_FILENAME’
exporting
complete_filename = path
* CHECK_DOS_FORMAT =
importing
* DRIVE =
extension = extension
name = name
* NAME_WITH_EXT =
* PATH =
exceptions
invalid_drive = 1
invalid_extension = 2
invalid_name = 3
invalid_path = 4
others = 5
.

endform. “upl

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s