Objective
- The requirement was to send the Birthday Mails to Employee along with image in mail body. Generally, the images are send as attachment in mail but to send the image in the mail body different approach is to be used.The main objective of this document is to send the image in email body. I searched on portals and google to resolve it but couldn’t find proper solution , so decided to post the document on finding solution.
Challenges
- To use image in mail there are many options through direct HTML code and CL_BCS class but image appears as attachment and in body “X” icon appears, browsers and mail servers behaves differently for HTML code.
- There are four possible solutions according to me
- To upload image on any server open to Internet and use that image path in html code. It will work in all case except those were external links are blocked by mail server or the image is removed from the given path.
- To convert image to base64 code and append the whole string in image. It will work but string is very long and if not converted properly then image does not appears in mail body as required.
- To upload image in smart-form and convert that smart-form to HTML then create html document and embed image to the mail using BCS class. It will work but the limitation is that in smartforms only BMP image types are supported which reduces the quality of image. The styles applied in smartforms are available in html but browsers suppresses the CSS and styles. So no styles will be seen in mail.To use the style they are to be applied inline through html code using tags like <font>,<i>&<b>,etc.
- To upload image in MIME Repository. Get the Image from MIME Repository in report and convert it to xstring table form.Using class cl_gbt_multirelated_service combine HTML content and image. Create HTML content and attach image to mail body using BCS class and assign sender and receivers.
- The 4th Approach was more feasible and appropriate as it has high availabilty of image in mail body which fulfills the primary requirement.
Advantage
- It is quite simple and easy approach to embed image in mail body.
- It can be reused for sending images in mail body.
- We can give desired styling using inline HTML tags which avoids the usage of css as they are not rendered by many web browsers and mail severs.
- High availabilty of image in mail and no other objects like smartforms are required,use multiple images in mail body.
Disadvantage
- The web browsers and mail severs behave differently while rendering HTML page. We use CID (Context Id ) to display image in HTML body.It is possible that some browsers/mail servers don’t allow to render image in mail body using CID but atleast we would have the image as attachment.
- Tested for OUTLOOK, ZIMBRA works well on it.
Steps for sending the image in mail.
- Upload Image in MIME repository.
- Get image from mime reporsitory in form of xstring
- Convert the image from xstring to table form ( i.e. solix_tab )
- Attach Image in xstring form to HTML form.
- Create mail content.
- Create HTML form with HTML content.
- Create Mail using BCS class.
1. Upload Image in MIME repository.
- There are many options to upload image in Mime Repository. I have implemented using SE80 TCODE.
- It can also be implemented through report using
- Class : CL_MIME_REPOSITORY_API
- Method: IF_MR_API~PUT
1. Provide TCODE SE80.
2. Click on Mime Repository.
3. Select Directory where the image is to be uploaded , right click on directory and select “Import MIME Objects” ( Here the directory is “PUBLIC” ).
4. Browse the System and Select the required image to be uploaded from the system then following screen would appear,provide description and click on save button.
5. On Saving the image the system would prompt for object package , Save it in associated package or local object as per the requirement.
6. Now the image would appear in the folder and by clicking on image we can get the preview as shown below.
2. Get image from mime repository in form of xstring
- Create the report as per the logic required(requirement was to send birthday mails) and we can add the image part and html body to it
- Get the image stored in mime repository in the report using
- Class: cl_mime_repository_api
- Method: if_mr_api~get_api
- Here in parameter i_url the image path is to be given to obtain the image in xstring form.
- CODE:
3. Convert the image from xstring to table form ( i.e. solix_tab )
- In variable gv_content the image is in form of xstring.
- To attach that image in mail we have to convert it to table form of Xstring which contains row of type xstring and length 255 char
- The lt_solix table contains the image in xstring table form which would be attached to mail body.
- CODE
4. Attach Image in xstring
- In variable l_filename the name is given to the image that is created and in variable l_content_id the content id (CID) is assigned which would be later used in the html content.
- The variable lt_solix contains the image in xstring table form.
- The variable l_obj_len contains the length of image which was calculated earlier
- The content type is the mime type of the content in lt_solix
- For .jpg image it is image/jpg. Similarly for bmp – image/bmp, gif – image/gif , etc
- Attach image using
- Class : cl_gbt_multirelated_service
- Method: add_binary_part
- Object : lo_mime_helper
- CODE:
- If mimetype/content type is not available in system then we need to add it through following steps
TCODE : SMW0.
1. Execute TCODE : SMW0
2. Press F8
3. Press F8
4. In Menu , Goto Settings –> Maintain MIME Types.Click on Create button
- If the desired mime type is not present then the required mime type can be created using above steps
5. Create HTML mail content.
- The HTML content is prepared and filled in table lt_soli which would be later used to create HTML form.
- <font> tag is used to use to give the style to the content.
- <img> tag is used for using image in html body.
- In <img> the content id (CID) should be same as the content Id given in above step while attaching image.
- Code:
6.Create HTML form with HTML content.
- The created html content in table lt_soli is to be attached to create a HTML form using
- Class : cl_gbt_multirelated_service
- Method: set_main_html
- Object : lo_mime_helper
- Code
7. Create Mail using BCS class.
- Now using the CL_BCS class we can embed html body and image assigning sender and receivers.
- Code:
Output in SOST.
Output in Mail Client.
The Source Code is attached in a text file.
cheers!!!
Hi Purvang,
thank’s for this detailed blog. I will try it when my daily work allows it.
Go on with this thorough work.
Kind regards,
Dirk
Hi Purvang,
nice Blog.
You can replace the code in step 3 with a call to the class-method XSTRING_TO_SOLIX from class CL_BCS_CONVERT.
This makes your code cleaner, less error prone and saves you some work. Just don’t reinvent the wheel.
Regards Christian
Hi Christian,
Thanks for the suggestion !! 🙂
Thanks & Regards
purvang
Thank you for sharing! I especially liked how you outlined all the steps first and then went into details. Well done.
Maybe some kind of a web service to call Someecards would be a good alternative, er? 🙂
Hi Jelena,
I think that for using web service for calling e-cards would work if the mail servers allow to the access to other/private web services.
Thanks for the suggestion would try that way. 🙂
Hi Purvang,
Thanks for sharing this, just the couple of days back, i got the same requirement and i am working on it. Now you made my work easy.
Cheers for you…
Hi,
I have tried to send the logo to my gmail id and outlook still it is coming as attached file only not in the body of the mail can you please help me I have attached the screenshot here.
Even in SOST image is not showing me as per your screen shoot.Please help me.
Regards,
Ravi Shankar L
In Outlook even attachments itself not able to find
Just check the HTML part for the Image File.
confirm the same name throughout the program. that will solve this issue. please check the underlined part of your code.
CALL METHOD gv_mr_api->get
EXPORTING
i_url = ‘/SAP/PUBLIC/turtle.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.
l_filename = ‘turtle.jpg’.
l_content_id = ‘turtle.jpg’.
ls_soli = ‘<br><img src=”turtle.jpg” width = “150px” height = “150px”/><br>’.
Hi Jayakumar,
CALL METHOD gv_mr_api->get
EXPORTING
i_url = ‘/SAP/PUBLIC/ZSG_LOGOS/ZWeber_Logo.jpg’ “Image path
*Attach image to HTML body
l_filename = ‘ZSG_LOGOS/ZWeber_Logo.jpg’.
l_content_id = ‘ZSG_LOGOS/ZWeber_Logo.jpg’.
“For displaying Image
CLEAR ls_soli.
*ls_soli = ‘<lt;br><lt;img alt=”[image]” src=”cid:ZSG_LOGOS/ZWeber_Logo.jpg” /><lt;br>’.
ls_soli = ‘<br><img src=”ZSG_LOGOS/ZWeber_Logo.jpg” width = “150px” height = “150px”/><br>’.
APPEND ls_soli TO lt_soli.
I am getting frame allocated for that image but image is not placing there
Dear Ravi Shankar ,
Regards, Testing Team Sigma
Still mail is sending only in attachment please help me…… what mistake I am doing here…
Regards,
Ravi Shankar L
Hi Ravi,
Please check if file name & content id is added as below
l_filename = ‘ZSG_LOGOS/ZWeber_Logo.jpg’.
l_content_id = ‘ZSG_LOGOS/ZWeber_Logo.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 length = l_obj_len “length of image
content_id = l_content_id. “content id would be used in html part
If it’s fine then try inserting below line for image
‘<br><img alt=”[image]” src=”cid:ZSG_LOGOS/ZWeber_Logo.jpg” /><br>’
I think the problem is in HTML code.
If problem still persists you can mail me
that part of code if it’s fine for you so
that I can check code.
Regards
Purvang
Please tell your mail ID ….. I will send my program…… Thanks in advance for your help
Hello can u please send me as well
Nice one Dear!
Very useful. Can you please help me in the below scenario?
I want to convert some texts, tables into an image file dynamically and then use that image to make a PDF file so that my PDF file will not be editable even someone has opened it with any editor tool.
Can we do that? If yes, then how?
Thanks and Regards,
Virendra Kedia
use smartforms to create the formatted text and then convert it to PDF.
Hi Virendra,
If your primary requirement is to make PDF non editable then please go for digitally signed PDF or making it password protect or using SSF(provided by SAP) it would be more secured.
Even there are tools available which would read text from images in PDF.
I haven’t came across any such class or method to convert text/tables to an image will search on it and let you know if I find any.
You can use any third party tool (many are online) to convert text to image and save those images somewhere either in MIME repository or Application server.
You can upload this image to smart-form and then convert smart-form to PDF or other way is to convert image to binary content and then convert the binary content to PDF(OTF).
Hope this helps!!!! 🙂
Thanks & Regards
purvang
I got one more spark from this link….. It is possible to upload video files “*.avi,etc” in MIME repository if we click it from there means video running succesfully same way If I press some button I need to play that video….. If any one have idea about it please share it… Thanks in advance.
Regards,
Ravi Shankar L
Hi Ravi,
I tried uploading AVI file in MIME Repository and it works.
We just need to add a MIME Type(*.avi) as I mentioned in the document through transaction SMW0 the same way as we did for images.
On clicking the AVI file it will open on your PC’s default media player and start playing.
check the below link to have your media player in SAP it would be fun :
http://scn.sap.com/people/thomas.jung/blog/2005/05/11/using-classic-activex-controls-in-the-abap-control-framework
Thanks & Regards
purvang
I am trying to play this video file if I click one button in report it want to run it is possible? IN MIME it running successfully from program I need to call this If you have any idea please share me….
Does anybody knows if it is possible to save the email in outbox folder without sending?
Thank you.
Hi Purvang,
Have got same requirement where need to send image in the body of email with text (below image)
Pls check below code ,i m not getting image in the body it is showing as X mark
there is part of code mentioned above in the post which i have commented as it was showing junk character in Email
Thanks in advance.
REPORT ZTEST1.
INCLUDE ZTEST1I.*all data declaration part done inside include
*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/zlogo/xyz.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.
CALL FUNCTION ‘SSFC_BASE64_ENCODE’
EXPORTING
BINDATA = gv_content
IMPORTING
B64DATA = gv_content_64
EXCEPTIONS
SSF_KRN_ERROR = 1
SSF_KRN_NOOP = 2
SSF_KRN_NOMEMORY = 3
SSF_KRN_OPINV = 4
SSF_KRN_INPUT_DATA_ERROR = 5
SSF_KRN_INVALID_PAR = 6
SSF_KRN_INVALID_PARLEN = 7
OTHERS = 8
.
IF SY–SUBRC <> 0.
* Implement suitable error handling here
ENDIF.
*Attach image to HTML body
l_filename = ‘zlogo/xyz.jpg’.
l_content_id = ‘zlogo/xyz.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.
*****************************************************HTML body content start**************************************************************************
ls_soli = ‘<html xmlns=”http://www.w3.org/1999/xhtml” xmlns:xfa=”http://www.xfa.org/schema/xfa-template/2.1/“>’ .
APPEND ls_soli TO lt_soli.
CLEAR ls_soli.
ls_soli = ‘<html>’.
APPEND ls_soli TO lt_soli.
CLEAR ls_soli.
ls_soli = ‘<body>’.
APPEND ls_soli TO lt_soli.
CLEAR ls_soli.
*commented this part as was getting junk text in mail
*ls_soli = ‘< style=”font-family:verdana;font-size:15px;”>’ .
*APPEND ls_soli TO lt_soli.
*
*CLEAR ls_soli.
* lv_length = STRLEN( gv_content_64 ).
* lv_len2 = lv_length / 255.
* ls_soli = gv_content_64.
* APPEND ls_soli TO lt_soli.
*CLEAR ls_soli.
*
* DO lv_len2 TIMES.
*
* lv_len3 = 255 * sy-index.
* IF lv_len3 <= lv_length.
* ls_solixr-line = gv_content_64(lv_len3).
* IF ls_solixr IS NOT INITIAL.
* MOVE ls_solixr-line TO ls_soli-line.
* APPEND ls_soli TO lt_soli.
* CLEAR lt_soli.
* ELSE.
* EXIT.
* ENDIF.
*ELSEif lv_len3 > lv_length.
* EXIT.
*ENDIF.
*ENDDO.
*end of commented code
CLEAR ls_soli.
ls_soli = ‘<img src=”cid:zlogo/xyz.jpg”>’.
APPEND ls_soli TO lt_soli.
clear ls_soli .
ls_soli = ‘<BR> <BR> <BR> ‘ .
APPEND ls_soli TO lt_soli.
clear ls_soli .
ls_soli = ‘ <p> Dear Name1, <BR> <BR> the Following request requires your Attention: <BR> <BR> ‘ .
APPEND ls_soli TO lt_soli.
clear ls_soli .
ls_soli = ‘ Request Number : <U> Ticket number </U> <BR> Request Type : Category <BR> Description: Test <BR> Current Status: Status <BR> </p> ‘ .
APPEND ls_soli TO lt_soli.
clear ls_soli .
ls_Soli = ‘ <BR> <BR> <P1> <Strong> Action Team </Strong> </P1> <BR> <BR> ‘ .
APPEND ls_soli TO lt_soli.
clear ls_soli .
ls_soli = ‘<h2 style=”background-color:yellow;”>’ .
APPEND ls_soli TO lt_soli.
clear ls_soli .
ls_soli = ‘ <P2> <font color = “#0000FF” size=”+1″ face=”Arial,”Helvetica, sans-serif” <BR> This is an automatically generated message. Please do not reply to this address. <BR> ‘ .
APPEND ls_soli TO lt_soli.
clear ls_soli .
ls_soli = ‘ </body> ‘ .
APPEND ls_soli TO lt_soli.
clear ls_soli .
ls_soli = ‘ </html> ‘ .
APPEND ls_soli TO lt_soli.
clear ls_soli .
DATA : title type c LENGTH 100.
CONCATENATE ‘ticketnumber-status’ ‘requires your attention’ INTO title SEPARATED BY space.
*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 = ‘Ticket Alert’. “Title
“Create HTML using BCS class and attach html and image part to it.
l_subject = title. “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( ‘xyz@xyz.com‘ ).
“Create Recipient
lo_recipient = cl_cam_address_bcs=>create_internet_address( i_address_string = ‘xyz@xyz.com‘ ).
lo_bcs->add_recipient( i_recipient = lo_recipient ).
lo_bcs->send( ).
COMMIT WORK AND WAIT.
A very good solution. Thanks for sharing.
I am able to see the image in SOST but in outlook I am getting 2 attachments:-
i) sapwebform.htm
ii) photo.jpg.
But I need the image in mail content. Please help.
Im facing the same problem, could you please share the solution if you found one.
Thanks
Hi,
same problem, did everyone found a solution?
Thanks.
Hello.
Please let me know if anyone found the solution for the same. I took the help of the blog above. It works perfectly if I open my emails in Web Browser but when I open it in Outlook , I get two attachments .
Please let me know how to solve the issue.
Thanks
Hi Purvang,
Thanks for the documentation it is really good.
I have also same requirement but when i try to execute the program the image is attached in the mail and not showing in body but a icon with a cross sign is displayed in the body.
Please help if you can. I am pasting my code below.
Thanks in advance.
REPORT ZTEST_IT.
“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 by Purvang Gandhi
“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/Happy.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 lt 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 = ‘<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.
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=”red” fontStyle=”italic”><i><b>Dear’
‘Employee Name’ ‘, <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:happy.jpg” /><br>’.
APPEND ls_soli TO lt_soli.
CLEAR ls_soli.
CONCATENATE ‘<br>Regards,<br>’ ‘CEO Name’ ‘<br>Team GINGER</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( ‘xyz@abc.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 = ‘xyz@abc.com‘ ).
lo_bcs->add_recipient( i_recipient = lo_recipient ).
IF lo_bcs->send( ) IS INITIAL.
ELSE.
COMMIT WORK.
ENDIF.
WAIT UP TO 2 SECONDS.
SUBMIT rsconn01 WITH mode = ‘INT’
WITH output = ‘X’
AND RETURN.
*COMMIT WORK AND WAIT.
Im facing the same problem, could you please share the solution if you found one.
Thanks
Hi,
I am also getting the same issue, image is not being displayed in email body but a place holder with cross sign is being displayed. Also the image doesn’t comes as an attachment. It would be of great help if someone can guide me through the solution.
Thanks
It is a great information.
Thanks,
Useful information..
Thanks
Great example. Tested and worked. Thanks.
Hi Puvang,
Thank you so much for the post.
I was trying to fulfill a requirement where I need to send a Smartform ( Converted to PDF ) as attachment along with the mail body where there is logo in the signature section.
The approach mentioned by you works perfectly for email body part.
But the problem I face is the attachment handles by the below piece of code does not work.
lo_doc_bcs1->add_attachment(
i_attachment_type = ‘PDF’
i_attachment_subject = gv_title
i_attachment_size = lv_len
i_att_content_hex = li_content_hex ).
Can you suggest what could be done here.
Regards,
Rama Murthy
Hi Rama.
Did you change the bin file to binary tab on previous steps?
Generally below are the steps i made:
1. i used the CONVERT_OTF function
CALL FUNCTION ‘CONVERT_OTF’
EXPORTING
format = ‘PDF’
max_linewidth = 132
IMPORTING
bin_filesize = bin_size
bin_file = pdf_xstring
TABLES
otf = t_otf
lines = t_pdf_tab
EXCEPTIONS
err_max_linewidth = 1
err_format = 2
err_conv_not_possible = 3
OTHERS = 4.
2. Once i get the bin file, i changed the bin_file to binary tab with CMS_XSTRING_TO_BINARY
DATA: l_size TYPE sood–objlen,
CALL FUNCTION ‘SCMS_XSTRING_TO_BINARY’
EXPORTING
buffer = pdf_xstring
IMPORTING
output_length = len_size
TABLES
binary_tab = pdf_content.
MOVE len_size TO l_size.
3.Finally you should be able to attach you document in the email with the same function you are using
CALL METHOD l_document->add_attachment
EXPORTING
i_attachment_type = ‘PDF’
i_attachment_size = l_size
i_att_content_hex = pdf_content.
Hope this helps.
Have a nice weekend.
Hi Virgilio,
It worked thank you so much….!
Regards,
Murthy
Hi Murthy,
I have got same problem .
I saw your message when I check the sdn.
So, please tell me first of what did you do ?
I mean you when you use CONVERT OTF function you are saying PDF..
You mean are your grafic a PDF format ?
And did you do MIME Repository working using se80?
Wouıld you please write me how can I start before writing this code ?
I will appreciate …
Thanks
Regards
Tanju
Hi Purvang,
Great working, well done …
Please let me ask a question for you .
I am attaching PDF files and sending , there is no any problem.
But my problem is there is jpg files and PDF files sending together.
I am using the function ‘SO_DOCUMENT_SEND_API1’ ..
So My question, how can I send together ?
I mean how can start first of all ?
Am I use MIME Repository ?
May I use MIME Repository and SO_DOCUMENT_SEND_API1 together ?
Is this right ?
Or is this wrong how can I start ?
Well, in fact I opened a new incident in SDN, but unfortunately it was removed cause of rules.
I did not understand why ?
If you help me I am reallly appreciate
Regards
Tanju
Hi ,
it is working fine with one IMAGE,but it is not working with multiple IMAGES . Can some one help how to work with multiple images using MIME helper .
Regards,
Siva Krishna
Thank You Siva,
I got it,
Greetings from Istanbul
Tanju
Hi Tanju,
you got it means , you are able to get multiple images using MIME helper?
if yes
can you please help me to get it.
Regards,
Siva
I tried in different way to attach multiple images but those are working fine in outlook but not working properly in SOST and Webmail ( Mail Box in Internet explorer ) are not working fine.
Please open the below link for more details.
Email Preview in SOST
Dear Siva,
http://scn.sap.com/thread/3783877
Siva, This is my incident and one of my collegue Eitan helped me …
Please check it …
Is there any problem, we can speak …
Regards
Tanju
Many many thanks Tanju.
it worked as what I have expected.
Thank you very much appreciated your help.
Regards,
Siva Krishna
You are welcome Siva,
We are friend, we are collegue, …
Take care of yourself
Tanju
Hi Tanju,
I have one more last request , Email body is showing perfectly in English language where it it not showing correctly in other languages .
Example :
Text in Mail :-
Text in English : Thank you for your
Text in German : vielen Dank für Ihre
Text in SOST Display :-
Text in English : Thank you for your
Text in German : vielen Dank für Ihre
Regards,
Siva Krishna
Hi Siva,
It seems like a font problem …
Would you please try another font ?
Regards
Tanju
I am using Arial font for both English and German language . is there will be any notes from SAP for fonts ?
Hi Siva,
I foung this sdn note …
https://scn.sap.com/thread/369787
I hope it helps …
Regards
Tanju
I am unable to find Source code attachment. Could you please share or let know where i can see attachment in same thread.
Hi Gurus,
I have imported the image in MIME Repository and used in the email attachment. Everything is working fine, and image is displaying in Email (using this in CRM WebUI flow logic)when I execute the flow.
But when the Business users in CRM WebUI execute the logic, the image is not rendering for some users.
Is there any authorization issue or any other? Please help!!!
Thanks in advance.
Hi All,
I can see in the blog, it is mentioned that code is attached in text file. However i do not see the text attachment.
Can anyone please help me with the full code for this?
Anyone solved Multi-Image in Email Body using MIME helper? Please help Very Urgent!!!!
Hi Purvang & Shnakar,
I am also facing the problem where in image is not getting displayed in the outlook.
Can you please share the solution for this?
Thanks
Gopal
a long time has passed, but I saw that this is a common issue/question, so I decided to write down the solution:
The problem lies in method lo_mime_helper->set_main_html.
You must leave the filename empty.
In case filename isn’t empty, Microsoft Outlook will interrupt it as an attachment instead of e-mail body.
Hello
I tried many ways to send an image in body but it still doesn t appear , I applied all steps correctly but no result I send all html to email address but image still = X
my code is here please help
SPAN {
font-family: “Courier New”;
font-size: 10pt;
color: #000000;
background: #FFFFFF;
}
.L0S31 {
font-style: italic;
color: #808080;
}
.L0S32 {
color: #3399FF;
}
.L0S33 {
color: #4DA619;
}
.L0S52 {
color: #0000FF;
}
.L0S55 {
color: #800080;
}
.L0S70 {
color: #808080;
}
REPORT ZBW_HBDAY.
DATA:
*- Structure to hold Output Details in delimited format
gf_output_soli TYPE soli,
*- Structure to hold Imported Object Components
gf_objpack TYPE sopcklsti1,
*- Structure to hold data in single line format
gf_objhead TYPE solisti1,
*- Structure to hold data in single line format
gf_objtxt TYPE solisti1,
*- Structure to hold data for API Recipient List
gf_reclist TYPE somlreci1,
*- Structure to hold email data
gf_doc_chng TYPE sodocchgi1,
*- Internal Table to hold Output Details in delimited format
gt_output_soli TYPE TABLE OF soli,
*- Internal Table to hold Imported Object Components
gt_objpack TYPE TABLE OF sopcklsti1,
*- Internal Table to hold data in single line format
gt_objhead TYPE TABLE OF solisti1,
*- Internal Table to hold data in single line format
gt_objtxt TYPE TABLE OF solisti1,
*- Internal Table to hold data for API Recipient List
gt_reclist TYPE TABLE OF somlreci1.
* Data Declarations
DATA: LT_MAILSUBJECT TYPE SODOCCHGI1.
DATA: LT_MAILRECIPIENTS TYPE STANDARD TABLE OF SOMLREC90 WITH HEADER LINE.
DATA: LT_MAILTXT TYPE STANDARD TABLE OF SOLI WITH HEADER LINE.
DATA: LT_MAILSUBJECT_2 TYPE SODOCCHGI1.
DATA: LT_MAILRECIPIENTS_2 TYPE STANDARD TABLE OF SOMLREC90 WITH HEADER LINE.
DATA: LT_MAILTXT_2 TYPE STANDARD TABLE OF SOLI WITH HEADER LINE.
DATA: BEGIN OF GT_ITAB OCCURS 0,
usrid_long like pa0105–usrid_long.
DATA: END OF GT_ITAB,
LS_ITAB LIKE LINE OF GT_ITAB.
data : date(2) type c,
month(2) type c,
year(4) type c.
date = sy–datum+6(2).
month = sy–datum+4(2).
year = sy–datum+0(4).
SELECT
distinct
p05~usrid_long
FROM PA0002 as p2 INNER JOIN pa0000 as p0 on p2~pernr = p0~pernr
inner join pa0105 as p05 on p05~pernr = p0~pernr
into table gt_itab
where p2~gbmon = month and p2~gbtag = date and p05~usrid_long <> ” .
*loop at gt_itab.
* Recipients
*LT_MAILRECIPIENTS-REC_TYPE = ‘U’.
*LT_MAILRECIPIENTS-RECEIVER = gt_itab-usrid_long.
*APPEND LT_MAILRECIPIENTS .
*CLEAR LT_MAILRECIPIENTS .
LT_MAILRECIPIENTS–REC_TYPE = ‘U’.
LT_MAILRECIPIENTS–RECEIVER = ‘<private information removed by moderator>’.
LT_MAILRECIPIENTS–BLIND_COPY = ‘X’.
APPEND LT_MAILRECIPIENTS .
*ENDLOOP.
DATA: gv_mr_api TYPE REF TO if_mr_api.
DATA is_folder TYPE boole_d.
DATA l_current TYPE xstring.
DATA l_loio TYPE skwf_io.
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/dogum_gunu.jpg’
IMPORTING
e_is_folder = is_folder
e_content = l_current
e_loio = l_loio
EXCEPTIONS
parameter_missing = 1
error_occured = 2
not_found = 3
permission_failure = 4
OTHERS = 5.
*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/dogum_gunu.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
DATA : gv_content TYPE xstring.
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.
DATA: l_filename TYPE string,
l_content_id TYPE string,
gv_content_64 type string,
lv_length type string,
lv_len2 type i,
lv_len3 type i.
data : lt_soli TYPE soli_tab,
ls_soli TYPE soli.
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.
* CALL FUNCTION ‘SSFC_BASE64_ENCODE’
* EXPORTING
* bindata = l_current
* IMPORTING
* b64data = b64data.
* IF sy-subrc <> 0.
* ENDIF.
*CALL FUNCTION ‘SSFC_BASE64_ENCODE’
*EXPORTING
*BINDATA = gv_content
*IMPORTING
*B64DATA = gv_content_64
*EXCEPTIONS
*SSF_KRN_ERROR = 1
*SSF_KRN_NOOP = 2
*SSF_KRN_NOMEMORY = 3
*SSF_KRN_OPINV = 4
*SSF_KRN_INPUT_DATA_ERROR = 5
*SSF_KRN_INVALID_PAR = 6
*SSF_KRN_INVALID_PARLEN = 7
*OTHERS = 8
*.
*IF SY-SUBRC <> 0.
** Implement suitable error handling here
*ENDIF.
*Attach image to HTML body
l_filename = ‘dogum_gunu.jpg’.
l_content_id = ‘dogum_gunu.jpg’.
DATA : lo_mime_helper TYPE REF TO cl_gbt_multirelated_service.
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.
*****************************************************HTML body content start**************************************************************************
ls_soli = ‘<html xmlns=”http://www.w3.org/1999/xhtml” xmlns:xfa=”http://www.xfa.org/schema/xfa-template/2.1/”>’ .
APPEND ls_soli TO lt_soli.
CLEAR ls_soli.
ls_soli = ‘<html>’.
APPEND ls_soli TO lt_soli.
CLEAR ls_soli.
ls_soli = ‘<body>’.
APPEND ls_soli TO lt_soli.
CLEAR ls_soli.
*
*
*
*
*ls_soli = ‘<style=”font-family:verdana;font-size:15px;”>’ .
*APPEND ls_soli TO lt_soli.
*
*CLEAR ls_soli.
*
* lv_length = STRLEN( gv_content_64 ).
* lv_len2 = lv_length / 255.
* ls_soli = gv_content_64.
* APPEND ls_soli TO lt_soli.
*
*CLEAR ls_soli.
*
* DO lv_len2 TIMES.
*
* lv_len3 = 255 * sy-index.
* IF lv_len3 <= lv_length.
* ls_soli-line = gv_content_64(lv_len3).
* IF ls_soli IS NOT INITIAL.
* MOVE ls_soli-line TO ls_soli-line.
* APPEND ls_soli TO lt_soli.
* CLEAR lt_soli.
* ELSE.
* EXIT.
* ENDIF.
*ELSEif lv_len3 > lv_length.
* EXIT.
*ENDIF.
*ENDDO.
*
*
*
*CLEAR ls_soli.
ls_soli = ‘<br><img src=”cid:dogum_gunu.jpg” />’.
APPEND ls_soli TO lt_soli.
clear ls_soli .
ls_soli = ‘<BR> <BR> <BR> ‘ .
APPEND ls_soli TO lt_soli.
clear ls_soli .
ls_soli = ‘ <p> Dear Name1, <BR> <BR> the Following request requires your Attention: <BR> <BR> ‘ .
APPEND ls_soli TO lt_soli.
clear ls_soli .
ls_soli = ‘ Request Number : <U> Ticket number </U> <BR> Request Type : Category <BR> Description: Test <BR> Current Status: Status <BR> </p> ‘ .
APPEND ls_soli TO lt_soli.
clear ls_soli .
ls_Soli = ‘ <BR> <BR> <P1> <Strong> Action Team </Strong> </P1> <BR> <BR> ‘ .
APPEND ls_soli TO lt_soli.
clear ls_soli .
ls_soli = ‘<h2 style=”background-color:yellow;”>’ .
APPEND ls_soli TO lt_soli.
clear ls_soli .
ls_soli = ‘ <P2> <font color = “#0000FF” size=”+1″ face=”Arial,”Helvetica, sans-serif” <BR> This is an automatically generated message. Please do not reply to this address. <BR> ‘ .
APPEND ls_soli TO lt_soli.
clear ls_soli .
ls_soli = ‘ </body> ‘ .
APPEND ls_soli TO lt_soli.
clear ls_soli .
ls_soli = ‘ </html> ‘ .
APPEND ls_soli TO lt_soli.
clear ls_soli .
DATA : title type c LENGTH 100.
CONCATENATE ‘ticketnumber-status’ ‘requires your attention’ INTO title SEPARATED BY space.
*CREATE OBJECT lo_mime_helper.
CALL METHOD lo_mime_helper->set_main_html
EXPORTING
content = lt_soli
filename = ‘sapwebform.htm’
description = ‘Ticket Alert’.
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 HTML using BCS class and attach html and image part to it.
l_subject = title. “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( ‘<private information removed by moderator>’ ).
“Create Recipient
lo_recipient = cl_cam_address_bcs=>create_internet_address( i_address_string = ‘<private information removed by moderator>’ ).
lo_bcs->add_recipient( i_recipient = lo_recipient ).
lo_bcs->send( ).
COMMIT WORK AND WAIT.