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: 

Send HTML mail with image

steffen_weber
Employee
Employee
0 Kudos

Hi experts and gurus,

how can I send an HTML email which is displaying an image in the HTML context (table)?

I'm using the 'SO_NEW_DOCUMENT_SEND_API1' like below:

CALL FUNCTION 'SO_NEW_DOCUMENT_SEND_API1'
    EXPORTING
      document_data              = ls_doc_data
      document_type              = 'HTM'
      put_in_outbox                = 'X'
      commit_work                 = 'X'
    TABLES
      object_content             = gt_mail_body
      receivers                  = gt_receiver .

I think that might switch to function module 'SO_NEW_DOCUMENT_ATT_SEND_API1', but I have no clue how to set up the PACKING_LIST properly and to incorporate the image into the HTML code afterwards.

Can anybody help me?

Regards, Steffen

1 ACCEPTED SOLUTION

Former Member
0 Kudos
21 REPLIES 21

Former Member
0 Kudos

0 Kudos

Hi AD,

the link leads to your profile page...

Regards, Steffen

0 Kudos

Sorry Steffen ,

check this:

FORM docu_send_email USING pv_otfdata TYPE tsfotf pv_emailid TYPE any pv_formname TYPE any. DATA: lv_filesize TYPE i, lv_buffer TYPE string, lv_attachment TYPE i, lv_testo TYPE i. DATA: li_pdfdata TYPE STANDARD TABLE OF tline, li_mess_att TYPE STANDARD TABLE OF solisti1, li_mtab_pdf TYPE STANDARD TABLE OF tline, li_objpack TYPE STANDARD TABLE OF sopcklsti1, li_objtxt TYPE STANDARD TABLE OF solisti1, li_objbin TYPE STANDARD TABLE OF solisti1, li_reclist TYPE STANDARD TABLE OF somlreci1, li_objhead TYPE soli_tab. DATA: lwa_pdfdata TYPE tline, lwa_objpack TYPE sopcklsti1, lwa_mess_att TYPE solisti1, lwa_objtxt TYPE solisti1, lwa_objbin TYPE solisti1, lwa_reclist TYPE somlreci1, lwa_doc_chng TYPE sodocchgi1. CONSTANTS: lc_u TYPE char1 VALUE 'U', lc_0 TYPE char1 VALUE '0', lc_1 TYPE char1 VALUE '1', lc_pdf TYPE char3 VALUE 'PDF', lc_raw TYPE char3 VALUE 'RAW', lc_ordform TYPE char15 VALUE 'ZORDCONFIRM_01', lc_attachment TYPE char10 VALUE 'ATTACHMENT'. CALL FUNCTION 'CONVERT_OTF' EXPORTING format = lc_pdf max_linewidth = 132 IMPORTING bin_filesize = lv_filesize TABLES otf = pv_otfdata lines = li_pdfdata EXCEPTIONS err_max_linewidth = 1 err_format = 2 err_conv_not_possible = 3 err_bad_otf = 4 OTHERS = 5. IF sy-subrc 0. * MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO * WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4. ENDIF. LOOP AT li_pdfdata INTO lwa_pdfdata. TRANSLATE lwa_pdfdata USING ' '. CONCATENATE lv_buffer lwa_pdfdata INTO lv_buffer. CLEAR lwa_pdfdata. ENDLOOP. TRANSLATE lv_buffer USING ' '. DO. lwa_mess_att = lv_buffer. APPEND lwa_mess_att TO li_mess_att. CLEAR lwa_mess_att. SHIFT lv_buffer LEFT BY 255 PLACES. IF lv_buffer IS INITIAL. EXIT. ENDIF. ENDDO. * Object with PDF. REFRESH li_objbin. li_objbin] = li_mess_att[. DESCRIBE TABLE li_objbin LINES lv_attachment. * Object with main text of the mail. lwa_objtxt = space. APPEND lwa_objtxt TO li_objtxt. CLEAR lwa_objtxt. DESCRIBE TABLE li_objtxt LINES lv_testo. * Create the document which is to be sent lwa_doc_chng-obj_name = text-008. lwa_doc_chng-obj_descr = text-008. lwa_doc_chng-sensitivty = lc_0. lwa_doc_chng-obj_prio = lc_1. lwa_doc_chng-doc_size = lv_testo * 225. * Pack to main body. CLEAR lwa_objpack-transf_bin. * header lwa_objpack-head_start = 1. * The document needs no header (head_num = 0) lwa_objpack-head_num = 0. * body lwa_objpack-body_start = 1. lwa_objpack-body_num = lv_testo. lwa_objpack-doc_type = lc_raw. APPEND lwa_objpack TO li_objpack. CLEAR lwa_objpack. * Create the attachment. * Fill the fields of the packing_list for the attachment: lwa_objpack-transf_bin = gc_x . * header lwa_objpack-head_start = 1. lwa_objpack-head_num = 1. * body lwa_objpack-body_start = 1. lwa_objpack-body_num = lv_attachment. lwa_objpack-doc_type = lc_pdf. lwa_objpack-obj_name = lc_attachment. lwa_objpack-obj_descr = text-008. lwa_objpack-doc_size = lv_attachment * 255. APPEND lwa_objpack TO li_objpack. CLEAR lwa_objpack. lwa_reclist-receiver = pv_emailid. lwa_reclist-rec_type = lc_u. lwa_reclist-notif_del = gc_x. lwa_reclist-notif_ndel = gc_x. APPEND lwa_reclist TO li_reclist. IF li_reclist IS NOT INITIAL. CALL FUNCTION 'SO_NEW_DOCUMENT_ATT_SEND_API1' EXPORTING document_data = lwa_doc_chng put_in_outbox = gc_x TABLES packing_list = li_objpack object_header = li_objhead contents_bin = li_objbin contents_txt = li_objtxt receivers = li_reclist EXCEPTIONS too_many_receivers = 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. IF sy-subrc 0. * MESSAGE ID sy-msgid TYPE 'I' NUMBER sy-msgno * WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4. ENDIF. ENDIF. ENDFORM. " DOCU_SEND_EMAIL

0 Kudos

Hi AD,

Thanks a l lot for your posting!!!

could you please also provide the code how you call this form, especially how the parameter pv_otfdata (TYPE tsfotf) is filled?

I didn't get your code working, and by the way, I need to send an image (JPG, GIF), and not a PDF.

regards, Steffen

0 Kudos

Hi Steffen,

Frankly I have never send an image with your FM, but maybe you will be interesed in using CL_BCS class instead, which offers such posibility.

Please refer


"in point 3. of the above link, this is full coding which dispatches an email

  DATA: lr_email TYPE REF TO cl_bcs,
            lr_email_body TYPE REF TO cl_document_bcs,
            l_send_result TYPE c,
            lr_receiver TYPE REF TO if_recipient_bcs,
            l_subject TYPE so_obj_des,
           l_mail_address LIKE mail_address.

        lr_email_body = cl_document_bcs=>create_document(
                                         i_type = 'HTM'
                                         i_text = lt_message
                                         i_subject = l_subject ).
 
lr_email->set_document( lr_email_body ).  "your picture is already in the body 

lr_receiver = cl_cam_address_bcs=>create_internet_address( 'someone at somewhere . com' ). 
lr_email->add_recipient( i_recipient = lr_receiver ).

lr_email->set_send_immediately( 'X' ).
l_send_result = lr_email->send( i_with_error_screen = 'X' ).

COMMIT WORK.

Regards

Marcin

0 Kudos

Hi Marcin,

thanks for your reply, I have tried this code as well. But I have also no clue (and no authorizations) how to handle the MIME repository. And I also prefer the function modules instead the BCS class.

But if have some sample code how to upload images to the MIME repository, you can also convince me to use the BCS class...

KR Steffen

0 Kudos

You must have authorizaiton to OAER tcode, then follow these steps:

- run OAER

- in Class name type PICTURES

- in Class type type OT

- in Object key type MYPICTURE

- run it

- from bottom left part of screen choose Screen

- pick your image

- as Description put MYPICTURE too

- in code I provided replace


wa_query-value = 'ENJOYSAP_LOGO'.

"with
wa_query-value = 'MYPICTURE'.

That's all you need

Regards

Marcin

0 Kudos

Hi Marcin,

we are coming closer and closer, thanks a lot!

I thought I have uploaded the image in TA OAER correctly, but unfortunately function WWW_GET_MIME_OBJECT can't find my picture.

When I debug this function, I can see that the call "import mime from database wwwdata(mi) id key."

fails with SUBRC 4.

I have uploaded the picture in OAER with these steps

- in Class name type PICTURES

- in Class type type OT

- in Object key type ZSACO_SAP_LOGO_MAIL

- run it

- from bottom left part of screen choose Screen

- pick your image

- as Description put ZSACO_SAP_LOGO_MAIL too

and before function WWW_GET_MIME_OBJECT is called I build the query like this

wa_query-name = '_OBJECT_ID'.
  wa_query-value = 'ZSACO_SAP_LOGO_MAIL'. 

The function then fails wit SUBRC 3 (=others).

Maybe I have to provide more parameters for thsi functions? At the moment only the query_string is filled, but there are a lot more:

CALL FUNCTION 'WWW_GET_MIME_OBJECT'
    TABLES
      query_string        = it_query
      html                = it_html
      mime                = pick_data
    CHANGING
      return_code         = return_code
      content_type        = content_type
      content_length      = content_length

Would be really helpful if you help me with this, I think this is the last step, as the mail is already send out properly, just without the picture...

Kind regards, Steffen

0 Kudos

Hi Steffen,

Unfortunatelly I was wrong. Using tcode OAER you can upload pictures only used in ALV background (top-of-page).

When using fm WWW_GET_MIME_OBJECT you will have to use SAP Web Repository (tcode SNWO), but this seems you are missing authorization for.

Anyhow if you are able to access it please check this blog for details how to upload your picture so it will be visible as MIME object

[How to display picture in SAP screen|http://abap-explorer.blogspot.com/2008/10/how-to-display-picture-in-screen.html]

Once you upload it, the code should work and import from table wwwdata will not fail.

Regards

Marcin

0 Kudos

Hi Marcin,

hm, I managed it to upload the image into the Web Repistory and this is the URL which is builded:

"SAPR3://DE4BBB2FAA4798F197D8001438C4EA66". Unfortunately, when putting this into the HTML image tag, I still get a broken image (red cross) in the HTML mail. I have to state that I had the receive the same ID for the image all the time, I mean before I've uploaded it with SMW0. Most likely because I have uploaded it with OAER and SE78.

As I said, FM DP_CREATE_URL returns URL "SAPR3://DE4BBB2FAA4798F197D8001438C4EA66", which seems to be not valid for an image tag. Any idea what might be the issue now?

Could it be that I have to attach the image to the mail object instead of simply use it in the image tag?

And, your help is highly appreciated!!! Thanks a lot!

Regards, Steffen

Edited by: Steffen Weber on May 28, 2009 9:21 PM

0 Kudos

Success!!!!

As I assumed, when I attach the Pic to the mail and use the attachment name in the image tag, it is working!!!!

After the creation of the email document and I have just inserted the ADD_ATTACHMENT method like below:

lr_email_body = cl_document_bcs=>create_document(
                                   i_type = 'HTM'
                                   i_text = lt_message
                                   i_subject = l_subject ).

  "add attachment to mail 
  lr_email_body->add_attachment(
            *i_attachment_type* = 'GIF'
            *i_attachment_subject* = 'My_GIF'
            i_att_content_hex = pick_data ).

If I then create the image tag like below, the pic is displayed correctly in the mail:

ls_message = '<<img src="My_GIF.GIF">>'.
APPEND ls_message TO lt_message.

You simply have to use the name provided in i_attachment_subject and the document extenstion coming from i_attachment_type to build the image source.

Perfect! Thanks a lot for all your help!!!!!!

It was worth the full points!

Kind regards, Steffen

Edited by: Steffen Weber on May 28, 2009 9:48 PM

0 Kudos

hi...could you please tell in brief, the steps tht u followed to send an image in a mail? I tried going through all the posts, but stll couldn't get the result still..

0 Kudos

Hi there,

1. Upload your pic via transaction SWM0

2. Retrieve image

wa_query-name = '_OBJECT_ID'.
  wa_query-value = YOUR_NAME. "name in swm0 where your picture was placed

  append wa_query to it_query.


  call function 'WWW_GET_MIME_OBJECT'
    tables
      query_string        = it_query
      html                = it_html
      mime                = pick_data
    changing
      return_code         = return_code
      content_type        = content_type
      content_length      = content_length
    exceptions
      invalid_table       = 1
      parameter_not_found = 2
      others              = 3.
  if sy-subrc = 0.
    pic_size = content_length.
  endif.

  clear url.
  call function 'DP_CREATE_URL'
    exporting
      type     = 'image'
      subtype  = 'X-UNKNOWN'
      size     = pic_size
      lifetime = 'T'
    tables
      data     = pick_data
    changing
      url      = url
    exceptions
      others   = 1.

3. Prepare your HTML mail document. Include the image tag to your pic:

concatenate '<img_src="' your_image '">' into gv_mail_body.

In my case

your_image = 'sap_logo.gif'.

Because of: (see code below)

i_attachment_type = 'GIF'
i_attachment_subject = 'sap_logo'

4. Then BCS class to send your mail, image will be attached

data: lr_email type ref to cl_bcs,
        lr_email_body type ref to cl_document_bcs,
        l_send_result type c,
        lr_receiver type ref to if_recipient_bcs,
        l_subject type so_obj_des,
        l_sender type ref to if_sender_bcs,
        l_mail_address type adr6-smtp_addr.



  "l_mail_address LIKE mail_address.
  l_subject = 'SAPCon EM - Inactivity Reminder'.
  lr_email_body = cl_document_bcs=>create_document(
                                   i_type = 'HTM'
                                   i_text = gt_mail_body
                                   i_subject = l_subject ).


  "add attachment to mail (not from Marcin) WORKS!!!!
  lr_email_body->add_attachment(
            i_attachment_type = 'GIF'
            i_attachment_subject = 'sap_logo'
            i_att_content_hex = pick_data ).



  lr_email = cl_bcs=>create_persistent( ).
  lr_email->set_document( lr_email_body ).

5. Then add all required recipients and send the mail

"Add all recipients
  loop at gt_receiver into gs_receiver.
    l_mail_address = gs_receiver-receiver.

    lr_receiver = cl_cam_address_bcs=>create_internet_address( l_mail_address ).
    lr_email->add_recipient( i_recipient = lr_receiver ).

  endloop.

  "Set Sender and send mail
  l_sender = cl_sapuser_bcs=>create( sy-uname ).
  lr_email->set_sender( l_sender ).
  lr_email->set_send_immediately( 'X' ).  "Sed email directly
  l_send_result = lr_email->send( i_with_error_screen = 'X' ).

  commit work.

Does this help???

Can't tell you what's wrong with the format, it is totally corrupted. If you want, post your email, and I will mail you the code.

Regards, Steffen

Edited by: Steffen Weber on Aug 11, 2009 1:35 PM

Edited by: Steffen Weber on Aug 11, 2009 1:36 PM

Edited by: Steffen Weber on Aug 11, 2009 1:37 PM

Edited by: Steffen Weber on Aug 11, 2009 2:04 PM

Edited by: Steffen Weber on Aug 11, 2009 2:06 PM

0 Kudos

Hi Ms. Weber,

You said:

After the creation of the email document and I have just inserted the ADD_ATTACHMENT method like below:

lr_email_body = cl_document_bcs=>create_document(

i_type = 'HTM'

i_text = lt_message

i_subject = l_subject ).

"add attachment to mail

lr_email_body->add_attachment(

i_attachment_type = 'GIF'

i_attachment_subject = 'My_GIF'

i_att_content_hex = pick_data ).

If I then create the image tag like below, the pic is displayed correctly in the mail:

ls_message = '<<img src="My_GIF.GIF">>'.

APPEND ls_message TO lt_message.

I tried this to my requirement but it did not work.

The image is not still displayed, though it is properly attached.

I want it to be displayed on the body of the email.

Can you please tell me what further steps you made for this?

Thank you so much.

Regards,

Ronnie

0 Kudos

Thank you for your post.. it helped my problem

0 Kudos

Hi!

I have the same problem: how to send an image via email but in the BODY not like an attach file.

I read this post and I tried the steps. Yes, you can do this but like an attach file. If you want to put the image in the body of the email, first the image should be located on a server which can be hit threw the internet(if this email is to go outside your network). Next, use HTML to build your email body.

I also found the Note 416340 - Personalized e-mails: No graphics sent

Mihaela

0 Kudos

Yes the image should be loaded on the server which can be accessed outside as well.

You can load the image through web repository SWM0 or SE78 as you like. and accordingly create the graphic_table for that image through the FMs mentioned in the post and pass it as an attached file(internal table) in the email FM (Graphic_table is passed as attachement but the image in the email will be in BODY only along with Image attachment). I have tried and it works for me on gmail , my company mail etc.

Hope this is helpful.

Regards,

Varsha

Edited by: Varsha Agarwal on Jul 6, 2011 6:59 PM

0 Kudos

Hi Varsha,

I also have the same requirement. But, when I tried the steps mentioned in the post, I can only see the image in attachment but not in the body of the mail.

I am using the cl_bcs class only to send the mail.

If possible, please post your code, so that it could be very helpful for me.

Thank you in advance.

Regards,

Kiran.

0 Kudos

Hi,

I am also trying to send the image in the mail body but the image is not being displayed in the mail.

I am using cl_bcs class to send the image as the attachment in the mail and the body content but the

image is not being displayed instead all I can see a box with red cross however, the attached gets open.

But it is not able to find the image source I guess.

I am using Lotus notes to check the mail.

Please help me ......

I am uploading the image file from desktop into my code then I am attaching it in the mail.

Please let me know the solution

The code is as follows :

*----


- - - - -- - - - - - - -

REPORT ztest_mail_544.

DATA: l_send_request TYPE REF TO cl_bcs,

l_document TYPE REF TO cl_document_bcs,

l_sender TYPE REF TO cl_sapuser_bcs,

l_sub TYPE char50,

l_recipient TYPE REF TO if_recipient_bcs,

tl_contents TYPE STANDARD TABLE OF soli,

tl_reciev TYPE STANDARD TABLE OF somlreci1,

lp_encrypt TYPE bcsd_encr,

wa_contents TYPE solisti1,

l_doc_len TYPE so_obj_len,

l_cnt TYPE sy-tabix,

l_rcv_email TYPE adr6-smtp_addr,

l_result TYPE sy-binpt,

l_bcs_exception TYPE REF TO cx_bcs,

l_subj TYPE string,

l_doc TYPE REF TO cl_document_bcs,

e_result TYPE sr_state,

ls_receivers TYPE somlreci1,

ll_text TYPE uname.

DATA filename TYPE string .

DATA : it_tab TYPE solix_tab.

filename = 'C:\Documents and Settings\Desktop\otter.jpg'.

CALL FUNCTION 'GUI_UPLOAD'

EXPORTING

filename = filename

filetype = 'BIN'

TABLES

data_tab = it_tab

EXCEPTIONS

file_open_error = 1

file_read_error = 2

no_batch = 3

gui_refuse_filetransfer = 4

invalid_type = 5

no_authority = 6

unknown_error = 7

bad_data_format = 8

header_not_allowed = 9

separator_not_allowed = 10

header_too_long = 11

unknown_dp_error = 12

access_denied = 13

dp_out_of_memory = 14

disk_full = 15

dp_timeout = 16

OTHERS = 17

.

wa_contents-line = 'Mail containing pics <br/>'.

APPEND wa_contents TO tl_contents.

CLEAR wa_contents.

wa_contents-line = '<p><img src="otter.jpg" width="304" height="228"></img > </p><br/>'.

APPEND wa_contents TO tl_contents.

TRY.

*-- Create persistent send request

l_send_request = cl_bcs=>create_persistent( ).

  • tl_contents[] = it_mail_body[].

*-- Get the length of the Document

DESCRIBE TABLE tl_contents LINES l_cnt.

READ TABLE tl_contents INTO wa_contents INDEX l_cnt.

l_doc_len = ( l_cnt - 1 ) * 255 + STRLEN( wa_contents ).

*-- Subject of the mail

CLEAR l_sub.

l_sub = 'test mail for image'.

l_document = cl_document_bcs=>create_document(

i_type = 'HTM'

i_text = tl_contents

i_length = l_doc_len

i_subject = l_sub

i_language = sy-langu

i_importance = '9' ).

*-- Add document to send request

CALL METHOD l_send_request->set_document( l_document ).

*-- Don't send delivery info for successful mails

CALL METHOD l_send_request->set_status_attributes

EXPORTING

i_requested_status = 'E'

i_status_mail = 'A'.

*-- Set sender

l_sender = cl_sapuser_bcs=>create( 'HR_SUPPORT' ).

CALL METHOD l_send_request->set_sender

EXPORTING

i_sender = l_sender.

DATA : i_attachment_type TYPE soodk-objtp,

i_attachment_subject TYPE sood-objdes .

DATA : t_attachment TYPE STANDARD TABLE OF solix.

TRY.

  • l_att_type = i_attachment_type.

  • l_subject = i_attachment_subject.

i_attachment_type = 'jpg'.

t_attachment[] = it_tab[].

l_doc = l_document .

i_attachment_type = space.

i_attachment_subject = 'otter.jpg'.

CALL METHOD l_doc->add_attachment

EXPORTING

i_attachment_type = i_attachment_type

i_attachment_subject = i_attachment_subject

i_att_content_hex = t_attachment.

CATCH cx_document_bcs.

*

*

ENDTRY.

l_recipient = cl_cam_address_bcs=>create_internet_address('xyz @abc.com' ).

CALL METHOD l_send_request->add_recipient

EXPORTING

i_recipient = l_recipient

i_express = 'X'

i_copy = ''

i_blind_copy = ''.

IF sy-subrc = 0.

ENDIF.

CALL METHOD l_send_request->set_send_immediately( 'X' ).

*-- Send Email

CALL METHOD l_send_request->send(

EXPORTING

i_with_error_screen = 'X'

RECEIVING

result = l_result ).

COMMIT WORK.

CATCH cx_bcs INTO l_bcs_exception.

write : 'mail not sent '.

0 Kudos

Hi Ronnie,

did you got a break thorugh on this...I also have the same issue : Image displayed as X(red mark)

0 Kudos

Hi varsha,

did you got a break thorugh on this...I also have the same issue : Image displayed as X(red mark)