Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

How to send image as a email body with dynamic data?

sourabhmaybhate
Explorer
0 Kudos

I tried with MIME repository image upload and sending email but requirement is to send Image as mail body which may contains sales quotation number and URL's.

1 ACCEPTED SOLUTION

AmanSaxena
Participant
0 Kudos

To send an image as the body of an email with dynamic data in SAP, you can follow these steps:

1. Upload the image to the MIME repository: In SAP, go to transaction `SE78` and upload the image to the MIME repository. This step will make the image accessible through a URL.

2. Create an HTML email template: In SAP, you can use the `SO_NEW_DOCUMENT_ATT_SEND_API1` function module to send emails. Create an HTML email template that includes the image URL and any dynamic data placeholders. Replace the dynamic data placeholders with the actual values when generating the email.

3. Replace dynamic data: Replace the dynamic data placeholders in the HTML template with the actual values. You can do this using string manipulation or by using SAP's text substitution function.

4. Attach the image to the email: Before sending the email, you need to attach the image to it. Use the `SO_DOCUMENT_INSERT_API1` function module to add the image as an attachment to the email. Pass the URL of the image in the `OBJECT_CONTENT` parameter and set the `FILE_EXTENSION` parameter to the appropriate file extension (e.g., JPG, PNG).

5. Send the email: Finally, use the `SO_NEW_DOCUMENT_ATT_SEND_API1` function module to send the email. Set the `DOCUMENT_DATA` parameter to the modified HTML template, including the replaced dynamic data. Also, include the attachment information in the `DOCUMENT_ATTACHMENT` parameter.

Here's an example code snippet in ABAP:

DATA: lv_email_subject TYPE so_obj_des,
lv_email_body TYPE solisti1,
lt_attachments TYPE STANDARD TABLE OF solisti1,
ls_attachment TYPE solisti1.
DATA: lv_quotation_number TYPE string,
lv_image_url TYPE string.
lv_quotation_number = '12345'.
lv_image_url = 'https://example.com/sales_image.jpg'.
lv_email_subject = 'Sales Quotation'.
lv_email_body = |<html>
<body>
<h1>Sales Quotation: { lv_quotation_number }</h1>
<img src="{ lv_image_url }" alt="Sales Image">
</body>
</html>|.
REPLACE '{ lv_quotation_number }' IN lv_email_body WITH lv_quotation_number.
REPLACE '{ lv_image_url }' IN lv_email_body WITH lv_image_url.
ls_attachment-doc_type = 'HTM'.
ls_attachment-obj_descr = 'Sales Quotation HTML'.
ls_attachment-obj_langu = sy-langu.
ls_attachment-obj_name = 'sales_quotation.htm'.
ls_attachment-obj_descr = 'Sales Quotation HTML'.
ls_attachment-obj_descr = 'Sales Quotation HTML'.
ls_attachment-obj_size = strlen( lv_email_body ).
ls_attachment-doc_size = strlen( lv_email_body ).
ls_attachment-doc_size = strlen( lv_email_body ).
ls_attachment-obj_descr = 'Sales Quotation HTML'.
APPEND lv_email_body TO lt_attachments.
CALL FUNCTION 'SO_NEW_DOCUMENT_ATT_SEND_API1'
EXPORTING
document_data = lv_email_body
document_type = 'HTM'
commit_work = 'X'
TABLES
packing_list = lt_attachments
EXCEPTIONS
too_many_recipients = 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.

Make sure to adjust the code according to your specific SAP system and requirements.

Alternative Method 2-

If you need an alternative method to send an image as the email body with dynamic data in SAP, you can consider using the SAP Business Communication Services (BCS) functionality. BCS provides a higher level of abstraction for sending emails and allows you to send HTML emails with dynamic content and embedded images.

Here's an example using BCS in ABAP:

DATA: lo_send_request TYPE REF TO cl_bcs,
lo_document TYPE REF TO cl_document_bcs,
lo_sender TYPE REF TO cl_sapuser_bcs,
lo_recipient TYPE REF TO if_recipient_bcs,
lo_image TYPE REF TO cl_document_bcs,
lt_attachments TYPE STANDARD TABLE OF object TYPE REF TO if_bcs_attachment_bcs,
lo_attachment TYPE REF TO cl_document_bcs.
DATA: lv_quotation_number TYPE string,
lv_image_url TYPE string,
lv_email_subject TYPE string,
lv_email_body TYPE string.
lv_quotation_number = '12345'.
lv_image_url = 'https://example.com/sales_image.jpg'.
lv_email_subject = 'Sales Quotation'.
lv_email_body = |<html>
<body>
<h1>Sales Quotation: { lv_quotation_number }</h1>
<img src="{ lv_image_url }" alt="Sales Image">
</body>
</html>|.
REPLACE '{ lv_quotation_number }' IN lv_email_body WITH lv_quotation_number.
REPLACE '{ lv_image_url }' IN lv_email_body WITH lv_image_url.
CREATE OBJECT lo_send_request.
CREATE OBJECT lo_document.
lo_send_request->set_document( lo_document ).
lo_document->add_attachment(
EXPORTING
i_attachment_type = 'HTM'
i_attachment_subject = 'Sales Quotation HTML'
i_att_content_text = lv_email_body ).
CREATE OBJECT lo_sender.
lo_sender->set_property( 'SMTP_ADDR', 'sender@example.com' ).
lo_sender->set_property( 'SMTP_MAIL', 'sender@example.com' ).
CREATE OBJECT lo_recipient.
lo_recipient->set_property( 'SMTP_ADDR', 'recipient@example.com' ).
lo_recipient->set_property( 'SMTP_MAIL', 'recipient@example.com' ).
lo_send_request->add_recipient( lo_recipient ).
lo_send_request->set_sender( lo_sender ).
lo_send_request->set_subject( lv_email_subject ).
lo_send_request->set_send_immediately( 'X' ).
CALL METHOD lo_send_request->send(
EXPORTING
i_with_error_screen = 'X'
i_sapoffice_inbox = 'X'
i_save_sent_messages = 'X'
RECEIVING
result = lv_result ).

In this example, we create a BCS send request and set the email subject, sender, and recipient. We create a document object and attach the HTML email body as an attachment with the type 'HTM'. Finally, we send the email using the `send` method of the send request object.

Please note that you may need to configure your SAP system with the necessary email settings for BCS to work correctly.

4 REPLIES 4

raymond_giuseppi
Active Contributor

Basically attach the image and use it in the main body with a <img src="cid:some-image-cid" alt="img" />

FredericGirod
Active Contributor
0 Kudos

very old blog, but you have attachment explain somewhere

Sending Mail using Oo (2) – Smartforms | SAP Blogs

AmanSaxena
Participant
0 Kudos

To send an image as the body of an email with dynamic data in SAP, you can follow these steps:

1. Upload the image to the MIME repository: In SAP, go to transaction `SE78` and upload the image to the MIME repository. This step will make the image accessible through a URL.

2. Create an HTML email template: In SAP, you can use the `SO_NEW_DOCUMENT_ATT_SEND_API1` function module to send emails. Create an HTML email template that includes the image URL and any dynamic data placeholders. Replace the dynamic data placeholders with the actual values when generating the email.

3. Replace dynamic data: Replace the dynamic data placeholders in the HTML template with the actual values. You can do this using string manipulation or by using SAP's text substitution function.

4. Attach the image to the email: Before sending the email, you need to attach the image to it. Use the `SO_DOCUMENT_INSERT_API1` function module to add the image as an attachment to the email. Pass the URL of the image in the `OBJECT_CONTENT` parameter and set the `FILE_EXTENSION` parameter to the appropriate file extension (e.g., JPG, PNG).

5. Send the email: Finally, use the `SO_NEW_DOCUMENT_ATT_SEND_API1` function module to send the email. Set the `DOCUMENT_DATA` parameter to the modified HTML template, including the replaced dynamic data. Also, include the attachment information in the `DOCUMENT_ATTACHMENT` parameter.

Here's an example code snippet in ABAP:

DATA: lv_email_subject TYPE so_obj_des,
lv_email_body TYPE solisti1,
lt_attachments TYPE STANDARD TABLE OF solisti1,
ls_attachment TYPE solisti1.
DATA: lv_quotation_number TYPE string,
lv_image_url TYPE string.
lv_quotation_number = '12345'.
lv_image_url = 'https://example.com/sales_image.jpg'.
lv_email_subject = 'Sales Quotation'.
lv_email_body = |<html>
<body>
<h1>Sales Quotation: { lv_quotation_number }</h1>
<img src="{ lv_image_url }" alt="Sales Image">
</body>
</html>|.
REPLACE '{ lv_quotation_number }' IN lv_email_body WITH lv_quotation_number.
REPLACE '{ lv_image_url }' IN lv_email_body WITH lv_image_url.
ls_attachment-doc_type = 'HTM'.
ls_attachment-obj_descr = 'Sales Quotation HTML'.
ls_attachment-obj_langu = sy-langu.
ls_attachment-obj_name = 'sales_quotation.htm'.
ls_attachment-obj_descr = 'Sales Quotation HTML'.
ls_attachment-obj_descr = 'Sales Quotation HTML'.
ls_attachment-obj_size = strlen( lv_email_body ).
ls_attachment-doc_size = strlen( lv_email_body ).
ls_attachment-doc_size = strlen( lv_email_body ).
ls_attachment-obj_descr = 'Sales Quotation HTML'.
APPEND lv_email_body TO lt_attachments.
CALL FUNCTION 'SO_NEW_DOCUMENT_ATT_SEND_API1'
EXPORTING
document_data = lv_email_body
document_type = 'HTM'
commit_work = 'X'
TABLES
packing_list = lt_attachments
EXCEPTIONS
too_many_recipients = 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.

Make sure to adjust the code according to your specific SAP system and requirements.

Alternative Method 2-

If you need an alternative method to send an image as the email body with dynamic data in SAP, you can consider using the SAP Business Communication Services (BCS) functionality. BCS provides a higher level of abstraction for sending emails and allows you to send HTML emails with dynamic content and embedded images.

Here's an example using BCS in ABAP:

DATA: lo_send_request TYPE REF TO cl_bcs,
lo_document TYPE REF TO cl_document_bcs,
lo_sender TYPE REF TO cl_sapuser_bcs,
lo_recipient TYPE REF TO if_recipient_bcs,
lo_image TYPE REF TO cl_document_bcs,
lt_attachments TYPE STANDARD TABLE OF object TYPE REF TO if_bcs_attachment_bcs,
lo_attachment TYPE REF TO cl_document_bcs.
DATA: lv_quotation_number TYPE string,
lv_image_url TYPE string,
lv_email_subject TYPE string,
lv_email_body TYPE string.
lv_quotation_number = '12345'.
lv_image_url = 'https://example.com/sales_image.jpg'.
lv_email_subject = 'Sales Quotation'.
lv_email_body = |<html>
<body>
<h1>Sales Quotation: { lv_quotation_number }</h1>
<img src="{ lv_image_url }" alt="Sales Image">
</body>
</html>|.
REPLACE '{ lv_quotation_number }' IN lv_email_body WITH lv_quotation_number.
REPLACE '{ lv_image_url }' IN lv_email_body WITH lv_image_url.
CREATE OBJECT lo_send_request.
CREATE OBJECT lo_document.
lo_send_request->set_document( lo_document ).
lo_document->add_attachment(
EXPORTING
i_attachment_type = 'HTM'
i_attachment_subject = 'Sales Quotation HTML'
i_att_content_text = lv_email_body ).
CREATE OBJECT lo_sender.
lo_sender->set_property( 'SMTP_ADDR', 'sender@example.com' ).
lo_sender->set_property( 'SMTP_MAIL', 'sender@example.com' ).
CREATE OBJECT lo_recipient.
lo_recipient->set_property( 'SMTP_ADDR', 'recipient@example.com' ).
lo_recipient->set_property( 'SMTP_MAIL', 'recipient@example.com' ).
lo_send_request->add_recipient( lo_recipient ).
lo_send_request->set_sender( lo_sender ).
lo_send_request->set_subject( lv_email_subject ).
lo_send_request->set_send_immediately( 'X' ).
CALL METHOD lo_send_request->send(
EXPORTING
i_with_error_screen = 'X'
i_sapoffice_inbox = 'X'
i_save_sent_messages = 'X'
RECEIVING
result = lv_result ).

In this example, we create a BCS send request and set the email subject, sender, and recipient. We create a document object and attach the HTML email body as an attachment with the type 'HTM'. Finally, we send the email using the `send` method of the send request object.

Please note that you may need to configure your SAP system with the necessary email settings for BCS to work correctly.

iamamanrx this function module should not be used since more than 10 years !