HTML E-Mail inklusive Bild versenden

In diesem Beitrag möchte ich zeigen, wie einfach es ist ein Bild aus dem MIME-Repository in eine HTML E-Mail einzubinden.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
REPORT zstkoes_send_html_mail.
 
DATA(lo_mime_helper) = NEW cl_gbt_multirelated_service( ).
 
" Get MIME-Repository Object
DATA(gv_filename) = |ABDOC_SOC_bad_EN.jpg|.
cl_mime_repository_api=>get_api( )->get(
  EXPORTING
    i_url                  = |SAP/PUBLIC/BC/ABAP/{ gv_filename }|
    i_check_authority      = abap_true
  IMPORTING
    e_content              = DATA(gv_content)
    e_mime_type            = DATA(gv_mime_type)
  EXCEPTIONS
    parameter_missing      = 1
    error_occured          = 2
    not_found              = 3
    permission_failure     = 4
    OTHERS                 = 5 ).
 
IF sy-subrc EQ 0 AND gv_content IS NOT INITIAL.
*... split xstring into lt_solix table (which is need for email)
  DATA(lt_solix) = VALUE solix_tab( LET lv_total_length = xstrlen( gv_content ) lv_max_off = lv_total_length - 255 IN
                                    FOR lv_off = 0 THEN lv_off + 255  UNTIL lv_off GT lv_total_length
                                    ( line = COND #( WHEN lv_off LE lv_max_off
                                                     THEN gv_content+lv_off(255)
                                                     ELSE gv_content+lv_off ) ) ).
*Attach image to HTML body
  DATA(gv_obj_len) = xstrlen( gv_content ).
 
  lo_mime_helper->add_binary_part( content      = lt_solix
                                   content_type = CONV #( gv_mime_type )
                                   length       = CONV #( gv_obj_len )
                                   content_id   = gv_filename ).
ENDIF.
 
*... create the HTML content for the body of the email
DATA(lt_soli) = VALUE soli_tab(
  ( |<html xmlns="http://www.w3.org/1999/xhtml" xmlns:xfa="http://www.xfa.org/schema/xfa-template/2.1/"><head></head>| )
  ( |<body>| )
  ( |<p>Manchmal ist es notwendig ein Bild in eine E-Mail einzubinden.</p>| )
  ( |{ COND #( WHEN lt_solix IS NOT INITIAL THEN |<p><img alt="logo" src="cid:{ gv_filename }" /></p>| ) }| )
  ( |<p>Und hier ist, wie es funktioniert.</p>| )
  ( |</body></html>| ) ).
 
*... set the HTML with description
lo_mime_helper->set_main_html( content     = lt_soli
                               filename    = 'message.htm'      " filename for HMTL form
                               description = 'Email message' ). " Title
 
"Create HTML using BCS class and attach html and image part to it.
DATA(lo_doc_bcs) = cl_document_bcs=>create_from_multirelated( i_subject          = 'E-Mail mit MIME-Repository Bild'
                                                              i_multirel_service = lo_mime_helper ).
 
DATA(lo_bcs) = cl_bcs=>create_persistent( ).
 
"Create Document
lo_bcs->set_document( i_document = lo_doc_bcs ).
 
* Set sender
lo_bcs->set_sender( cl_cam_address_bcs=>create_internet_address( 'abap@koester-consulting.eu' ) ).
 
"Set Recipient
lo_bcs->add_recipient( cl_cam_address_bcs=>create_internet_address( 'abap@koester-consulting.eu' ) ).
 
lo_bcs->send( ).
COMMIT WORK.