To send an email with attachment ABAP

Tuesday, December 30, 2008

I learned how to do this from this link.
It uses a function module which you have to create.
But I added those required code inside the program itself and added the zipping feature.

This sends the e-mail immediately, so no scot is needed.

The program is tested and works fine just modify according to your needs.
Copy the code part and paste in the ABAP editor.
Good luck. :)

EDIT 06/01/09
According to my knowledge this works on SAP systems which has 4.6c higher versions.
A way to find out is by going to SE80 and entering the relevant class names and find out whether they are available in the system.
Thanks IndiaKing for pointing out the version issue.

Here is the source code link of the program for download.



*&---------------------------------------------------------------------*
*& Report ZTHILANKA_TEST
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT ZTHILANKA_TEST.


data: binary_content type solix_tab.
data: xl_content type xstring .

constants:
con_tab type c value cl_abap_char_utilities=>horizontal_tab,
con_cret type c value cl_abap_char_utilities=>cr_lf.

data: xlcontent type xstring,
conlength type i,
conlengths type so_obj_len,
result_content type string,
wa_string type string, "This holds all of your data
dummy type string.

data: send_request type ref to cl_bcs.
data: text type bcsy_text.
data: document type ref to cl_document_bcs.
data: sender type ref to if_sender_bcs.
data: recipient type ref to if_recipient_bcs.
data: recipients type bcsy_smtpa.
data: bcs_exception type ref to cx_bcs.
data: sent_to_all type os_boolean.

data: e_r_page type ref to cl_rsr_www_page.
data: content_length type w3param-cont_len ,
content_type type w3param-cont_type,
return_code type w3param-ret_code .
data: html type standard table of w3html .
data: server type string ,
port type string .
data: wa_rec type ad_smtpadr .
data: bcs_message type string .

data: subject type so_obj_des.
data: sender_id type ad_smtpadr.
data: email type ad_smtpadr.

data: gv_file type string,
gv_zipfilehex type xstring,
go_zipper type ref to cl_abap_zip. "The zip folder object

concatenate 'Customer no.'
'CUSTOMER name'
'Subcustomer'
'Sales incoterms'
'Currency'
'Material'
'Mat long text'
'level1'
'level2'
'level3'
'Level 1 description'
'Level 2 description'
'Level 3 description'
'Data'
'Valid from'
'Valid to'
into wa_string separated by con_tab.
concatenate con_cret con_tab wa_string into wa_string.
* After this keep on appending the data to the wa_string
data: i type i,
true type string.

clear: xl_content .
* convert the string data to xstring
call function 'SCMS_STRING_TO_XSTRING'
exporting
text = wa_string
* MIMETYPE = ' '
* ENCODING =
importing
buffer = xl_content.
* EXCEPTIONS
* FAILED = 1
* OTHERS = 2
.
if sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
endif.

gv_file = 'Data.xls'. "Zip file name

create OBJECT go_zipper.
go_zipper->add( name = gv_file
content = xl_content )."If you have other content to be added to the zip folder, do it here

gv_zipfilehex = go_zipper->save( ).


concatenate 'Data list ' 'date' into subject .

sender_id = '[email protected]'.
email = '[email protected]'.

refresh binary_content .

call function 'SCMS_XSTRING_TO_BINARY'
exporting
buffer = gv_zipfilehex
tables
binary_tab = binary_content.


*refresh text .
clear result_content .
*The body message
concatenate
'<p><font color="#000080">Dear Customer,</font></p>'
'<p><font color="#000080">Attached herewith is the data list you requested.</font></p>'

into result_content .
conlength = strlen( result_content ) .
conlengths = conlength .
call function 'SCMS_STRING_TO_FTEXT'
exporting
text = result_content
tables
ftext_tab = text.



try.
clear send_request .

send_request = cl_bcs=>create_persistent( ).

clear document .
document = cl_document_bcs=>create_document(
i_type = 'HTM'
i_text = text
i_length = conlengths
i_subject = subject ).

call method document->add_attachment
exporting
i_attachment_type = 'zip'
i_attachment_subject = 'Price List' "Attachment name
i_att_content_hex = binary_content.


*You can add multiple attachments like below

* CALL METHOD document->add_attachment
* EXPORTING
* i_attachment_type = 'zip'
* i_attachment_subject = 'Subject testing 2' "atta_sub
* i_att_content_hex = binary_content.
*
* CALL METHOD document->add_attachment
* EXPORTING
* i_attachment_type = 'zip'
* i_attachment_subject = 'Subject 3' "atta_sub
* i_att_content_hex = binary_content.

* add document to send request
call method send_request->set_document( document ).

clear sender .
sender = cl_cam_address_bcs=>create_internet_address( sender_id ).
call method send_request->set_sender
exporting
i_sender = sender.

clear wa_rec .



* add recipient with its respective attributes to send request
* you can add multiple recipients in the same mail by calling
* this method repeatedly.

clear recipient .
wa_rec = email.
recipient = cl_cam_address_bcs=>create_internet_address(
wa_rec ).

* call method send_request->add_recipient
* exporting
* i_recipient = recipient1
* i_express = 'X'.

* call method send_request->add_recipient
* exporting
* i_recipient = recipient2
* i_express = 'X'.



call method send_request->add_recipient
exporting
i_recipient = recipient
i_express = 'X'.

call method send_request->set_status_attributes
exporting
i_requested_status = 'E'
i_status_mail = 'E'.

call method send_request->set_send_immediately( 'X' ).
* ---------- send document ---------------------------------------

call method send_request->send(
exporting
i_with_error_screen = 'X'
RECEIVING
result = sent_to_all ).
if sent_to_all = 'X'.
*APPEND 'Mail sent successfully ' TO return .
endif.
commit work.
catch cx_bcs into bcs_exception.
bcs_message = bcs_exception->get_text( ).
*APPEND bcs_message TO return .
exit.
endtry.