Application Development and Automation 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: 
Read only

Email body logo embedding using cl_bcs_message

bob_w67
Explorer
7,942

We are sending emails from SAP for invoices, PO's and such. Some older programs still use the SO_NEW_DOCUMENT_ATT_SEND_API1 module, some are using the CL_BCS class. The first one should not be used anymore so new developments will use the class.

Bow for a certain functionality I ran into the COMMIT issue (you need the commit to send the mail via cl_bcs, but if you trigger it within an exit, commit is not allowed or even causes dumps). This can be solved by a wrapper FM and some calling mechanism, but there is an easier way, use cl_bcs_message instead of cl_bcs.

This works great in most cases (and I find it cleaner than cl_bcs, but that is just me), however.....

Now we run into the next issue: The user wants the company logo as part of the email body text. I can add everything as attachment to the mails, works like a charm, also company logos, but embedding of the logo in the (html) email body does not seem to work.

I'm now up to the point that I take the next steps (in a dummy test-program so I can fiddle around), in this order:

- get the html body text from a SO10 text

- convert the SO10 text into a string field.

- get the logo (BMP format as stored in SE78) via cl_ssf_xsf_utilities=>get_bds_graphic_as_bmp (we now have the logo in xstring format)

- convert the xstring to xtring table (solix) via cl_bcs_convert=>xstring_to_solix

- to be able to use the logo we need to add the binary part stored so we can reference it in the html tag (via obj_mime_helper->add_binary_part)

- create the email, with a subject, attachment (the same logo, from the xstring field, totally works fine), sender, recipient and the email body text from the SO10 string field created earlier.

In SOST it looks like this:

and the email looks like this:

As you can see, attachments work like a charm, but embedded images do not.

The source of the email shows this:


As you can see the CID used is "DITISHEM.bmp". I have tried without the extension (in the CID), used both lowercase and uppercase in the CID name, but to no avail.

I have the feeling that the preparation of the CID does magic for cl_bcs, but does nothing for cl_bcs_message.

Any ideas are more than appreciated....


REPORT zzzzz.

PARAMETERS: field TYPE c LENGTH 1.

DATA main_text_line TYPE soli.
DATA main_text TYPE soli_tab.
DATA body_text TYPE string.
DATA xstr_logo TYPE xstring.
DATA text_lines TYPE tlinet.
DATA image_id TYPE tdidgr VALUE 'BMAP'.
DATA text_line TYPE tline.

*
DATA: obj_mime_helper TYPE REF TO cl_gbt_multirelated_service.
DATA: lv_obj_len TYPE so_obj_len.
DATA: lt_solix TYPE solix_tab.
*

CLEAR text_lines.
CALL FUNCTION 'READ_TEXT'
EXPORTING
id = 'ST'
language = syst-langu
name = 'Zzzzzzzz'
object = 'TEXT'
TABLES
lines = text_lines
EXCEPTIONS
id = 1
language = 2
name = 3
not_found = 4
object = 5
reference_check = 6
wrong_access_to_archive = 7
OTHERS = 8.


LOOP AT text_lines INTO text_line.
body_text = |{ body_text }{ text_line-tdline }|.
ENDLOOP.

*---------------------------------------------------------------*
" Get the logo
CLEAR xstr_logo.
CALL METHOD cl_ssf_xsf_utilities=>get_bds_graphic_as_bmp
EXPORTING
p_object = 'GRAPHICS'
p_name = 'GGGGGG HHHHHH'
p_id = image_id
p_btype = 'BCOL'
RECEIVING
p_bmp = xstr_logo
EXCEPTIONS
not_found = 1
internal_error = 2
OTHERS = 3.
IF sy-subrc <> 0.
* Implement suitable error handling here
ELSE.
*
CLEAR lt_solix.
CALL METHOD cl_bcs_convert=>xstring_to_solix
EXPORTING
iv_xstring = xstr_logo
RECEIVING
et_solix = lt_solix.
*
lv_obj_len = xstrlen( xstr_logo ).
*

" check tx SMW0 -> Settings -> maintain MIME types

CREATE OBJECT obj_mime_helper.
CALL METHOD obj_mime_helper->add_binary_part
EXPORTING
content = lt_solix
filename = 'GGGGGG HHHHHH'
extension = 'BMP'
description = 'Graphic in BMP format'
content_type = 'image/bmp'
length = lv_obj_len
content_id = 'DITISHEM.bmp'.

*
ENDIF.
*---------------------------------------------------------------*

* Send the email -------------------------------------------------*
TRY.

* -------- create persistent send request --------------------
DATA(msg) = NEW cl_bcs_message( ).

* -------- set the mail subject ------------------------------
msg->set_subject( 'subject regel' ).


* -------- add the attachment file to the mail ---------------
msg->add_attachment( EXPORTING iv_contents_bin = xstr_logo
iv_doctype = 'bmp'
iv_filename = 'Gggggg.bmp' ).

* -------- add sender ----------------------------------------
msg->set_sender(
EXPORTING
iv_address = '[email protected]'
).

" Also add sender as recipient:
msg->add_recipient(
EXPORTING
iv_address = '[email protected]'
iv_copy = ' '
).


* -------- email body text ------------------------------------
msg->set_main_doc(
EXPORTING
iv_contents_txt = body_text
iv_doctype = 'htm' " Document Category
).


* -------- send the email -------------------------------------
msg->set_send_immediately( abap_false ).
msg->send( ).

* -------- errors? --------------------------------------------
CATCH cx_bcs_send INTO DATA(ex).
MESSAGE ex->get_text( ) TYPE 'E'.

ENDTRY.


1 ACCEPTED SOLUTION
Read only

Sandra_Rossi
Active Contributor
6,543

I'm not sure to understand what is the problem of not doing the commit in the exit. You don't need to commit in a user exit, because sending an email is usually done during the update task, and the actual database commit (not talking about COMMIT WORK which starts the update task) is done by SAP at the end of the task.

Concerning your code, I don't understand why you instantiate the MIME helper because you don't use it afterwards.

Concerning the image that you attach, I think it's where you forget to assign the Content-id (DITISHEM.bmp) via the parameter IV_CONTENT_ID. In the MIME multipart which will be generated, this image will be one part with "content-id: DISTISHEM.bmp" (when the email client finds <img src="cid:DITISHEM.bmp"> in the main document, it will look for one part of the MIME multipart whose content-id = DISTISHEM.bmp).

4 REPLIES 4
Read only

FredericGirod
Active Contributor
6,543

long time ago, I do this https://blogs.sap.com/2013/05/30/sending-mail-using-oo-2-smartforms/ it is using smartforms, but you see how to store picture

Read only

Sandra_Rossi
Active Contributor
6,544

I'm not sure to understand what is the problem of not doing the commit in the exit. You don't need to commit in a user exit, because sending an email is usually done during the update task, and the actual database commit (not talking about COMMIT WORK which starts the update task) is done by SAP at the end of the task.

Concerning your code, I don't understand why you instantiate the MIME helper because you don't use it afterwards.

Concerning the image that you attach, I think it's where you forget to assign the Content-id (DITISHEM.bmp) via the parameter IV_CONTENT_ID. In the MIME multipart which will be generated, this image will be one part with "content-id: DISTISHEM.bmp" (when the email client finds <img src="cid:DITISHEM.bmp"> in the main document, it will look for one part of the MIME multipart whose content-id = DISTISHEM.bmp).

Read only

6,543

Hi sandra.rossi , thanks for answering. The problem with the COMMIT had to do with sending mail from BADi and/or user-exit (see blog https://abapblog.com/articles/tricks/104-send-mail-in-badi-or-user-exit-without-commiting), but for now this can be ignored. I think I overdone it with information in my original question.

I use the MIME helper to get/set a content-id so I can refer to it in the html code in the SO10 text. But now I see that that does not work like that.

In the add_attachment method I need to add the content ID as you suggested. That works. I removed the MIME helper because that is not needed indeed.

Thanks!!!

Read only

bob_w67
Explorer
0 Likes
6,543

Hi frdric.girod . Yes! I use that post of yours (and others) so that is why I've tried the add_binary_part. The missing link is described in Sandra's post which I will investigate later.

Thanks!