Sending Image in Mail body using SAP ABAP + HTML Code
Image in Mail Body
Steps :
Enter Transaction : Se80Select/Choose : MIME repository option
Path to be used to upload image: /SAP/PUBLIC/Image.jpg (Image upload from PC)
Click on Import jpg picture
Sample picture name : img_happy.jpg (Example)
================================================================================= Write a below program(report with Mailer
functionality) to send an Image in Mail body .
Program :
report zimage_in_mail_body.
"For Mime Repository
data : gv_mr_api type ref to if_mr_api, "mime repository object
gv_content type xstring, "image in XSTRING
is_folder type boole_d,
l_loio type skwf_io.
*&------------------------------------*&source Code for image in mail body
*&------------------------------------
*&Image to Xstring Table form
data : l_obj_len type so_obj_len,
lv_graphic_length type tdlength,
gr_xstr type xstring,
l_offset type i,
l_length type i,
l_diff type i,
ls_solix type solix,
lt_solix type solix_tab.
*Attach image to HTML body
data: l_filename type string,
l_content_id type string.
"for HTML content
data : lt_soli type soli_tab,
ls_soli type soli.
*Class for cobining HMTL & Image
data : lo_mime_helper type ref to cl_gbt_multirelated_service.
*BCS class for sending mail
data: lo_bcs type ref to cl_bcs,
lo_doc_bcs type ref to cl_document_bcs,
lo_sender type ref to if_sender_bcs,
lo_recipient type ref to if_recipient_bcs,
l_subject type so_obj_des.
"Create image in xstring form
if gv_mr_api is initial.
gv_mr_api = cl_mime_repository_api=>if_mr_api~get_api( ).
endif.
call method gv_mr_api->get
exporting
i_url = '/SAP/PUBLIC/NAGA.JPG' "Image path
importing
e_is_folder = is_folder
e_content = gv_content
e_loio = l_loio
exceptions
parameter_missing = 1
error_occured = 2
not_found = 3
permission_failure = 4
others = 5.
*&Convert Image to Xstring table form
l_obj_len = xstrlen( gv_content ).
lv_graphic_length = xstrlen( gv_content ).
"get whole image
clear gr_xstr.
gr_xstr = gv_content(l_obj_len).
l_offset = 0.
l_length = 255.
clear lt_solix[].
while l_offset < lv_graphic_length.
l_diff = lv_graphic_length - l_offset.
if l_diff > l_length.
ls_solix-line = gr_xstr+l_offset(l_length).
else.
ls_solix-line = gr_xstr+l_offset(l_diff).
endif.
append ls_solix to lt_solix.
add l_length to l_offset.
endwhile.
*&-------------------------------
*Attach image to HTML body
*&-------------------------------
l_filename = 'img_happy.jpg'.
l_content_id = 'img_happy.jpg'.
create object lo_mime_helper.
call method lo_mime_helper->add_binary_part
exporting
content = lt_solix "Xstring in table form
filename = l_filename "file name to be given to image
extension = 'JPG' "type of file
description = 'Graphic in JPG format' "description
content_type = 'IMAGE/jpg' "content type / Mime type. If mime type not present in system then need to add through tcode : SMW0
length = l_obj_len "length of image
content_id = l_content_id. "content id would be used in html part
*&===============================================================================
*& Create HTML mail body Content
*&===============================================================================
refresh lt_soli[].
clear ls_soli.
ls_soli = '<body>'.
append ls_soli to lt_soli.
"to apply font to HTML body content
clear ls_soli.
concatenate '<p><font size="4" face="Gabriola,Lucida Calligraphy,Comic Sans MS" color="blue" fontStyle="italic"><i><b>Dear'
'Celebrity' ', <br>' into ls_soli separated by space.
append ls_soli to lt_soli.
"For displaying Image
clear ls_soli.
ls_soli = '<br><img alt="[image]" src="cid:img_happy.jpg" /><br>'.
append ls_soli to lt_soli.
clear ls_soli.
concatenate '<br>Regards,<br>' 'Developer Name' '<br>Nagaraju Adhikari</b></i></font></p></body></html>'
into ls_soli separated by space.
append ls_soli to lt_soli.
*&=======================================================================================================
"Create main HTML body
*CREATE OBJECT lo_mime_helper.
call method lo_mime_helper->set_main_html
exporting
content = lt_soli
filename = 'sapwebform.htm' "filename for HMTL form
description = 'Birthday Greeting'. "Title
"Create HTML using BCS class and attach html and image part to it.
l_subject = 'Birthday Greetings'(t01). "subject
lo_doc_bcs = cl_document_bcs=>create_from_multirelated(
i_subject = l_subject
i_multirel_service = lo_mime_helper ).
lo_bcs = cl_bcs=>create_persistent( ).
"Create Document
lo_bcs->set_document( i_document = lo_doc_bcs ).
"create Sender
lo_sender = cl_cam_address_bcs=>create_internet_address( 'nagaraju.adhikari@gmail.com' ).
* Set sender
lo_bcs->set_sender(
exporting
i_sender = lo_sender ).
"Create Recipient
lo_recipient = cl_cam_address_bcs=>create_internet_address( i_address_string =
'nagaraju.adhikari@gmail.com' ).
lo_bcs->add_recipient( i_recipient = lo_recipient ).
lo_bcs->send( ).
commit work and wait.
report zimage_in_mail_body.
"For Mime Repository
data : gv_mr_api type ref to if_mr_api, "mime repository object
gv_content type xstring, "image in XSTRING
is_folder type boole_d,
l_loio type skwf_io.
*&------------------------------------*&source Code for image in mail body
*&------------------------------------
*&Image to Xstring Table form
data : l_obj_len type so_obj_len,
lv_graphic_length type tdlength,
gr_xstr type xstring,
l_offset type i,
l_length type i,
l_diff type i,
ls_solix type solix,
lt_solix type solix_tab.
*Attach image to HTML body
data: l_filename type string,
l_content_id type string.
"for HTML content
data : lt_soli type soli_tab,
ls_soli type soli.
*Class for cobining HMTL & Image
data : lo_mime_helper type ref to cl_gbt_multirelated_service.
*BCS class for sending mail
data: lo_bcs type ref to cl_bcs,
lo_doc_bcs type ref to cl_document_bcs,
lo_sender type ref to if_sender_bcs,
lo_recipient type ref to if_recipient_bcs,
l_subject type so_obj_des.
"Create image in xstring form
if gv_mr_api is initial.
gv_mr_api = cl_mime_repository_api=>if_mr_api~get_api( ).
endif.
call method gv_mr_api->get
exporting
i_url = '/SAP/PUBLIC/NAGA.JPG' "Image path
importing
e_is_folder = is_folder
e_content = gv_content
e_loio = l_loio
exceptions
parameter_missing = 1
error_occured = 2
not_found = 3
permission_failure = 4
others = 5.
*&Convert Image to Xstring table form
l_obj_len = xstrlen( gv_content ).
lv_graphic_length = xstrlen( gv_content ).
"get whole image
clear gr_xstr.
gr_xstr = gv_content(l_obj_len).
l_offset = 0.
l_length = 255.
clear lt_solix[].
while l_offset < lv_graphic_length.
l_diff = lv_graphic_length - l_offset.
if l_diff > l_length.
ls_solix-line = gr_xstr+l_offset(l_length).
else.
ls_solix-line = gr_xstr+l_offset(l_diff).
endif.
append ls_solix to lt_solix.
add l_length to l_offset.
endwhile.
*&-------------------------------
*Attach image to HTML body
*&-------------------------------
l_filename = 'img_happy.jpg'.
l_content_id = 'img_happy.jpg'.
create object lo_mime_helper.
call method lo_mime_helper->add_binary_part
exporting
content = lt_solix "Xstring in table form
filename = l_filename "file name to be given to image
extension = 'JPG' "type of file
description = 'Graphic in JPG format' "description
content_type = 'IMAGE/jpg' "content type / Mime type. If mime type not present in system then need to add through tcode : SMW0
length = l_obj_len "length of image
content_id = l_content_id. "content id would be used in html part
*&===============================================================================
*& Create HTML mail body Content
*&===============================================================================
refresh lt_soli[].
clear ls_soli.
ls_soli = '<body>'.
append ls_soli to lt_soli.
"to apply font to HTML body content
clear ls_soli.
concatenate '<p><font size="4" face="Gabriola,Lucida Calligraphy,Comic Sans MS" color="blue" fontStyle="italic"><i><b>Dear'
'Celebrity' ', <br>' into ls_soli separated by space.
append ls_soli to lt_soli.
"For displaying Image
clear ls_soli.
ls_soli = '<br><img alt="[image]" src="cid:img_happy.jpg" /><br>'.
append ls_soli to lt_soli.
clear ls_soli.
concatenate '<br>Regards,<br>' 'Developer Name' '<br>Nagaraju Adhikari</b></i></font></p></body></html>'
into ls_soli separated by space.
append ls_soli to lt_soli.
*&=======================================================================================================
"Create main HTML body
*CREATE OBJECT lo_mime_helper.
call method lo_mime_helper->set_main_html
exporting
content = lt_soli
filename = 'sapwebform.htm' "filename for HMTL form
description = 'Birthday Greeting'. "Title
"Create HTML using BCS class and attach html and image part to it.
l_subject = 'Birthday Greetings'(t01). "subject
lo_doc_bcs = cl_document_bcs=>create_from_multirelated(
i_subject = l_subject
i_multirel_service = lo_mime_helper ).
lo_bcs = cl_bcs=>create_persistent( ).
"Create Document
lo_bcs->set_document( i_document = lo_doc_bcs ).
"create Sender
lo_sender = cl_cam_address_bcs=>create_internet_address( 'nagaraju.adhikari@gmail.com' ).
* Set sender
lo_bcs->set_sender(
exporting
i_sender = lo_sender ).
"Create Recipient
lo_recipient = cl_cam_address_bcs=>create_internet_address( i_address_string =
'nagaraju.adhikari@gmail.com' ).
lo_bcs->add_recipient( i_recipient = lo_recipient ).
lo_bcs->send( ).
commit work and wait.
Comments
Post a Comment