0% found this document useful (0 votes)
178 views4 pages

Image Mail

This document contains code to generate an HTML email with an embedded image attachment. It creates a BCS mail object, attaches an image file as a binary part, generates HTML content with the image included, and sends the email. Key steps include importing the image file from a transaction, building the HTML content and embedding the image using its content ID, attaching the image and HTML parts to a mail document, and sending the email using BCS classes.

Uploaded by

g_honey_the1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
178 views4 pages

Image Mail

This document contains code to generate an HTML email with an embedded image attachment. It creates a BCS mail object, attaches an image file as a binary part, generates HTML content with the image included, and sends the email. Key steps include importing the image file from a transaction, building the HTML content and embedding the image using its content ID, attaching the image and HTML parts to a mail document, and sending the email using BCS classes.

Uploaded by

g_honey_the1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

*&---------------------------------------------------------------------*

*& Report Z_TESTMAIL_IMAGE


*&---------------------------------------------------------------------*

REPORT z_testmail_image.

DATA: l_obj_len TYPE so_obj_len,


lt_solix TYPE solix_tab,
ls_solix TYPE solix,
gr_xstr TYPE xstring,
lv_graphic_len TYPE i,
l_offset TYPE i,
l_diff TYPE i VALUE 0,
l_length TYPE i VALUE 255.

*Attach Image in xstring


DATA: l_filename TYPE mime_text VALUE 'ZLOGO.jpg',
l_content_id TYPE mime_cntid VALUE 'ZLOGO.jpg'.

*Create Mail using BCS class.


DATA: i_subject TYPE so_obj_des,
gv_sent_to_all TYPE os_boolean,
gv_email TYPE adr6-smtp_addr,
lo_bcs TYPE REF TO cl_bcs,
gr_sender TYPE REF TO cl_sapuser_bcs,
gr_bcs_exception TYPE REF TO cx_bcs,
gr_recipient TYPE REF TO if_recipient_bcs,
gr_document TYPE REF TO cl_document_bcs.

DATA: lc_cx_document_bcs TYPE REF TO cx_document_bcs,


lc_cx_bcom_mime TYPE REF TO cx_bcom_mime,
lc_cx_gbt_mime TYPE REF TO cx_gbt_mime.

DATA: lo_mime_helper TYPE REF TO cl_gbt_multirelated_service.

* Data Declaration
DATA: lfd_xstring TYPE xstring,
lv_obkey TYPE wwwdatatab,
lt_mime_raw TYPE STANDARD TABLE OF w3mime,
ls_mime_raw TYPE w3mime.

* Import the Image File, these are maintained in transaction SMW0


lv_obkey-objid = 'ZLOGO'.
lv_obkey-relid = 'MI'.

CALL FUNCTION 'WWWDATA_IMPORT'


EXPORTING
key = lv_obkey
TABLES
mime = lt_mime_raw
EXCEPTIONS
wrong_object_type = 1
import_error = 2
OTHERS = 3.

*... build a long xstring


IF sy-subrc EQ 0.
LOOP AT lt_mime_raw INTO ls_mime_raw.
CONCATENATE lfd_xstring ls_mime_raw-line INTO lfd_xstring IN BYTE MODE.
ENDLOOP.
ENDIF.

l_obj_len = xstrlen( lfd_xstring ).


lv_graphic_len = xstrlen( lfd_xstring ).
CLEAR gr_xstr.
gr_xstr = lfd_xstring(l_obj_len).

CLEAR lt_solix[].
WHILE l_offset < lv_graphic_len.
l_diff = lv_graphic_len - 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.
CLEAR ls_solix.
ADD l_length TO l_offset.
ENDWHILE.

*CALL METHOD cl_bcs_convert=>xstring_to_solix


* EXPORTING
* iv_xstring = l_current
* RECEIVING
* et_solix = lt_solix.

CREATE OBJECT lo_mime_helper.

CALL METHOD lo_mime_helper->add_binary_part


EXPORTING
content = lt_solix
filename = l_filename
extension = 'JPG'
description = 'Graphic in JPG Format'
content_type = 'image/jpeg'
length = l_obj_len
content_id = l_content_id.

*Create HTML mail content.


DATA: lt_soli TYPE TABLE OF solisti1,
ls_soli TYPE solisti1.

REFRESH lt_soli.

CLEAR ls_soli.
ls_soli = '<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:xfa="http://www.xfa.org/schema/xfa-template/2.1/"><head></head>'.
APPEND ls_soli TO lt_soli.

CLEAR ls_soli.
MOVE '<body>' TO ls_soli.
APPEND ls_soli TO lt_soli.

CLEAR ls_soli.
CONCATENATE '<p><font size="4" face="Gabriola, Lucida Calligraphy, Comic Sans MS"
color="red" fontStyle="italic"><i><b>Dear'
'Srivamsi' ', <br>' INTO ls_soli SEPARATED BY space.
APPEND ls_soli TO lt_soli.
CLEAR ls_soli.
ls_soli = '<br><img alt="logo" src="cid:ZLOGO.jpg" /><br>'.
APPEND ls_soli TO lt_soli.

CLEAR ls_soli.
CONCATENATE '<br>Regards,<br>' 'Sri Harsha' '<br> Team
HR</b></i></font></p></body></html>' INTO ls_soli SEPARATED BY space.
APPEND ls_soli TO lt_soli.

*Create HTML form with HTML content.


CALL METHOD lo_mime_helper->set_main_html
EXPORTING
content = lt_soli
filename = 'message.htm' "filename for HMTL form
description = 'Test Image Mail'. "Title

"Create HTML using BCS class and attach html and image part to it.
i_subject = 'Test Image Mail'.
TRY.
CALL METHOD cl_document_bcs=>create_from_multirelated
EXPORTING
i_subject = i_subject
i_multirel_service = lo_mime_helper
RECEIVING
result = gr_document.

CATCH cx_document_bcs INTO lc_cx_document_bcs. "#EC NO_HANDLER.


CATCH cx_bcom_mime INTO lc_cx_bcom_mime. "#EC NO_HANDLER.
CATCH cx_gbt_mime INTO lc_cx_gbt_mime. "#EC NO_HANDLER.
ENDTRY.

TRY. "Create send request


lo_bcs = cl_bcs=>create_persistent( ).

gr_sender = cl_sapuser_bcs=>create( sy-uname ). "Add sender to send


request
CALL METHOD lo_bcs->set_sender
EXPORTING
i_sender = gr_sender. "Email TO...

gv_email = '[email protected]'.
gr_recipient = cl_cam_address_bcs=>create_internet_address( gv_email ).
"Add recipient to send request

CALL METHOD lo_bcs->add_recipient


EXPORTING
i_recipient = gr_recipient
i_express = 'X'. "Email BODY

"Add document to send request


CALL METHOD lo_bcs->set_document( gr_document ). "Send email

CALL METHOD lo_bcs->send(


EXPORTING
i_with_error_screen = 'X'
RECEIVING
result = gv_sent_to_all ).
IF gv_sent_to_all = 'X'.
ENDIF. "Commit to send email
COMMIT WORK. "Exception handling

CATCH cx_bcs INTO gr_bcs_exception.


WRITE: 'Error!', 'Error type:',
gr_bcs_exception->error_type.
ENDTRY.

You might also like