Send PDF As Email Attachment
Send PDF As Email Attachment
Attachment
abaplearner / April 18, 2019
Berikut adalah contoh program untuk kirim PDF melalui email. Prosesnya adalah
sebagai berikut:
1 REPORT zsendpdf.
2
3 PARAMETERS: p_fname TYPE tdsfname OBLIGATORY,
4 p_email TYPE text100 OBLIGATORY.
5
6 DATA: wa_ssfctrlop TYPE ssfctrlop,
7 wa_ssfcompop TYPE ssfcompop,
8 wa_output TYPE ssfcrescl.
DATA: d_fmname TYPE rs38l_fnam,
9
d_binsize TYPE i,
10
d_binfile TYPE xstring,
11
d_subject TYPE so_obj_des,
12 d_flname TYPE so_obj_des,
13 d_email TYPE ad_smtpadr.
14 DATA: t_otf TYPE TABLE OF itcoo,
15 t_lines TYPE TABLE OF tline,
16 t_bintab TYPE solix_tab,
17 t_bodymail TYPE bcsy_text.
18 DATA: lo_send_reqst TYPE REF TO cl_bcs,
19 lo_document TYPE REF TO cl_document_bcs,
20 lo_sender TYPE REF TO if_sender_bcs,
21 lo_recipient TYPE REF TO if_recipient_bcs.
22
23 *supress the dialog box for printer
24 wa_ssfcompop-tddest = 'LOCL'.
25 wa_ssfctrlop-no_dialog = 'X'.
26 wa_ssfctrlop-preview = space.
wa_ssfctrlop-getotf = 'X'.
27
28
*get function of smartforms
29
CALL FUNCTION 'SSF_FUNCTION_MODULE_NAME'
30
EXPORTING
31 formname = p_fname
32 IMPORTING
33 fm_name = d_fmname.
34
35 *get otf of smartforms
36 CALL FUNCTION d_fmname
37 EXPORTING
38 control_parameters = wa_ssfctrlop
output_options = wa_ssfcompop
39
user_settings = space
40
IMPORTING
41
job_output_info = wa_output.
42
43 *convert otf to pdf file
44 t_otf = wa_output-otfdata[].
45 CALL FUNCTION 'CONVERT_OTF'
46 EXPORTING
47 format = 'PDF'
48 IMPORTING
49 bin_filesize = d_binsize
50 bin_file = d_binfile
51 TABLES
52 otf = t_otf
53 lines = t_lines.
54
55 *convert xstring to binary
56 CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
57 EXPORTING
buffer = d_binfile
58
TABLES
59
binary_tab = t_bintab.
60
61
*subject email
62 d_subject = 'Hallo'.
63
64 *write body mail
65 APPEND 'Dengan hormat,' TO t_bodymail.
66 APPEND 'Berikut adalah lampiran file dalam bentuk PDF.' TO t_bodymail.
67 APPEND 'Terima kasih' TO t_bodymail.
68
69 lo_send_reqst = cl_bcs=>create_persistent( ).
70 lo_document = cl_document_bcs=>create_document( i_type = 'RAW'
71 i_text = t_bodymail
72 i_subject = d_subject ).
73
74 *attchment doc & pass the document to send request
75 d_flname = 'FILE'.
76 CALL METHOD lo_document->add_attachment
77 EXPORTING
i_attachment_type = 'PDF'
78
i_attachment_subject = d_flname
79
i_att_content_hex = t_bintab.
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94 CALL METHOD lo_send_reqst->set_document( lo_document ).
95
96 *set sap username as sender
97 lo_sender = cl_sapuser_bcs=>create( sy-uname ).
98 CALL METHOD lo_send_reqst->set_sender
99 EXPORTING
10 i_sender = lo_sender.
0
10 *set recipient
1 d_email = p_email.
10 lo_recipient = cl_cam_address_bcs=>create_internet_address( d_email ).
CALL METHOD lo_send_reqst->add_recipient
2
EXPORTING
10
i_recipient = lo_recipient
3
i_express = 'X'
10 i_copy = ' '
4 i_blind_copy = ' '
10 i_no_forward = ' '.
5
10 *send email
6 lo_send_reqst->set_send_immediately( 'X' ).
10 CALL METHOD lo_send_reqst->send( ).
7 COMMIT WORK.