01-12-2018 12:41 PM
Dear Experts,
I have a requirement where I have to generate email for plant maintenance notification, In the email body (content) i need to display an image(jpeg) .
I have written the code as specified in the blog
https://blogs.sap.com/2013/06/11/image-in-mail-body-in-abap/
Email is getting triggered however image is not displayed.
I have debugged my code and there is no issue. even the HTML source code from outlook email is also same as coded.
What else could be wrong? Please help as this is urgent.
See the attached image for more details.
My code snippet is as below
"Get image from mime repository in form of xstring
lr_mime_rep = cl_mime_repository_api=>if_mr_api~get_api( ).
CALL METHOD lr_mime_rep->get
EXPORTING
i_url = '/SAP/PUBLIC/PM/notify.jpg'
IMPORTING
e_is_folder = lv_folder
e_content = lv_content
e_loio = lv_loio
EXCEPTIONS
parameter_missing = 1
error_occured = 2
not_found = 3
permission_failure = 4
others = 5
.
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.
"Convert the image from xstring to table form
call method cl_bcs_convert=>xstring_to_solix
exporting
iv_xstring = lv_content
receiving
et_solix = i_solix.
lv_len = xstrlen( lv_content ).
"Attach Image in xstring
lv_filename = 'PM/notify.jpg'.
lv_content_id = 'PM/notify.jpg'.
CREATE OBJECT lo_mime_helper.
CALL METHOD lo_mime_helper->add_binary_part
EXPORTING
content = i_solix "Xstring in table form
filename = lv_filename "file name to be given to image
extension = 'JPG' "type of file
description = 'notify' "description
content_type = 'image/jpg' "content type / Mime type. If mime type not present in system
length = lv_len "length of image
content_id = lv_content_id. "content id would be used in html part
* Prepare email body
CONCATENATE '<html>'
'<body>'
INTO lv_line1.
CONCATENATE '<p>'
'<img src="cid:PM/notify.jpg" width="668" height="191" />'
------ Skipping some lines of code
CALL METHOD cl_document_bcs=>create_document
EXPORTING
i_text = lt_mail_body
i_subject = lv_subject
i_type = 'HTM'
i_language = sy-langu
RECEIVING
result = lr_document.
Thanks & Regards
Gopal
01-12-2018 1:08 PM
Check the html source for the <img src=cid:filename.jpg> tag, look at the attachment(s) of the mail, is the picture attached, does it use the same filename, are you able to download the attached image and display it?
01-12-2018 12:51 PM
It would help if you display the entire message. It would also help if you shared your actual code. Otherwise, we can't help.
Michelle
01-12-2018 1:08 PM
Check the html source for the <img src=cid:filename.jpg> tag, look at the attachment(s) of the mail, is the picture attached, does it use the same filename, are you able to download the attached image and display it?
01-12-2018 1:19 PM
Hi Raymond
I have checked the HTML source and it is fine. image is not coming as attachment. I cannot download the image as well. i have updated my text above with more details like code snippets. See above.
Thanks
Gopal
01-12-2018 1:25 PM
So correct the code that send the mail to attach the image file.
Hint: Look for the add_binary_part method in original blog.
01-12-2018 1:34 PM
In your first comment you asked me to check if there is any attachment in the email.
And my answer is "No".
I am also not expecting any attachment.
I want image in email body. The code I have written is correct but still image is not displayed in the email output.
Hope you are getting my question.
Thanks
Gopal
01-12-2018 1:47 PM
If your html code contains a cif: tag your mail must carry an attachment, I was expecting one (else why would I ask you to download and display it?) 😉
Hint: Seems you created wrongly the document, use cl_document_bcs=>create_from_multirelated (as in the original blog) and not cl_document_bcs=>create_document which doesn't add the attachment.
01-13-2018 5:17 AM
So you are saying that even if I just want the image to be embedded in the email body I have to use cl_document_bcs=>create_from_multirelated?
Anyway I try that too.
01-13-2018 12:29 PM
01-12-2018 2:10 PM
It amazes me when I see code like this and people wonder why it doesn't work
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.
01-13-2018 5:10 AM
Dear Sir,
If you have read my question fully then You will find that i have clearly mentioned that I have done the debugging and there is no error. So its not even going to any exception block. To be more clear sy-subrc is always 0.
Moreover please understand that this is not a final code that I can share. Once i get the basic functionality up and working then i will add all the exception/error handling.
Regards
Gopal
01-14-2018 9:21 AM
If you had read my response fully, you would have found that it was not proposing that this is the cause of your problem, but an observation on code quality. Code quality from the start is a very important part of solutions with fewer errors. "I will add error handling later" is something many developers say/think/hope, but sadly the majority of such code stays as is.
01-12-2018 5:39 PM
Please format your code, it's difficult to read it. There's a button "CODE".