Send Emails with Attachments of any Format
Skip to end of metadata
Created by V S BHARGAV MYLAVARAPU, last modified by Former Member on Oct 24, 2013
Go to start of metadata
Hi All,
Happy SDN..!
Recently I got a chance to do some R&D in sending emails using ABAP. I searched all the SCN and other Technical forums to find a easy solution to perform this operation. But unsucessfull :( . We have several options given by SAP to send mails to several people with attachments. Everyone knows how to perform this operation. The new thing here which I want to explain to all via Wiki SDN is Sending those mails in a bulk format. For Example, I have kept 100 documents in a Folder on my desktop. The folder contains different types of documents like, PDF, EXCEL, WORD, JPEG, Etc... Now I need to send all those documents one by one via Email to a specific Mail ID with attachments. Since the folder can contain any type of document, The below program will solve this kind of requirement. Please find the below screen shots of the program for a easy go-thru of what exactly this program does.
Now a Email will be generated to the given Mail ID as shown in the below screen shots.
Similarly you will get different mails with each attachment in a seperate mail.
The explaination of the program is as follows.
Error rendering macro 'code': Invalid value specified for parameter 'com.atlassian.confluence.ext.code.render.InvalidValueException'
REPORT YSEND_BULK_DOCUMENTS_FROM_A_FOLDER.PARAMETERS : P_ATTACH TYPE CHAR200 OBLIGATORY, " Select the Folder on the Desktop from F4 help
P_EMAIL TYPE CHAR50 OBLIGATORY, " Email ID
P_SUBJ TYPE CHAR50 OBLIGATORY. " Subject of the Email
DATA : T_HEXTAB TYPE TABLE OF STRING,
L_FILE TYPE STRING,
T_MAILHEX TYPE SOLIX_TAB,
LV_COUNT TYPE I,
GV_EMAIL TYPE ADR6-SMTP_ADDR,
T_BODY TYPE TABLE OF SOLISTI1,
GR_RECIPIENT TYPE REF TO IF_RECIPIENT_BCS,
TL_TEXTPOOL TYPE TABLE OF TEXTPOOL,
WA_TEXTPOOL LIKE LINE OF TL_TEXTPOOL,
IT_CONTENTS TYPE SOLI_TAB,
LV_ATTACH_NAME TYPE SOOD-OBJDES,
L_SUBJECT TYPE SO_OBJ_DES,
L_DOCUMENT TYPE REF TO CL_DOCUMENT_BCS,
W_DOCUMENT TYPE REF TO CL_BCS,
WA_CONTENTS LIKE LINE OF IT_CONTENTS,
LV_FILENAME TYPE STRING,
L_RESULT TYPE OS_BOOLEAN,
LV_STRING TYPE STRING.
DATA : FILE_TABLE LIKE TABLE OF SDOKPATH WITH HEADER LINE .
DATA : DIR_TABLE LIKE TABLE OF SDOKPATH WITH HEADER LINE .
DATA : FILE_COUNT TYPE I ,
DIRCOUNT TYPE I .
AT SELECTION-SCREEN ON VALUE-REQUEST FOR P_ATTACH.
CALL METHOD CL_GUI_FRONTEND_SERVICES=>DIRECTORY_BROWSECHANGING
SELECTED_FOLDER = LV_STRINGEXCEPTIONS
CNTL_ERROR = 1
ERROR_NO_GUI = 2
NOT_SUPPORTED_BY_GUI = 3
OTHERS = 4.
IF SY-SUBRC <> 0.MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ELSE.
P_ATTACH = LV_STRING.
ENDIF.START-OF-SELECTION.
• CHECK IF THE FOLDER PATH IS INITIAL, IF INITIAL LEAVE THE PROGRAM, ELSE COPY THE SUBJECT. ---> IT ALL DEPENDS ON HOW YOU VALIDATE.
IF P_ATTACH IS INITIAL.LEAVE LIST-PROCESSING.
ELSE.
L_SUBJECT = P_SUBJ.
ENDIF.• GETS ALL THE FILES NAMES IN THE SELECTED FOLDER
CALL FUNCTION 'TMP_GUI_DIRECTORY_LIST_FILES'EXPORTING
DIRECTORY = P_ATTACH
IMPORTING
FILE_COUNT = FILE_COUNT
DIR_COUNT = DIRCOUNT
TABLES
FILE_TABLE = FILE_TABLE
DIR_TABLE = DIR_TABLE
EXCEPTIONS
CNTL_ERROR = 1
OTHERS = 2.
• TELL HOW MANY FIELDS EXISTS
WRITE:/ 'no of files in the floder is : '(005), FILE_COUNT.SKIP 1.
WRITE:/ SY-ULINE.WRITE:/ SY-ULINE.• PROCESS THE DATA TO SEND MAIL WITH ATTACHMENT.
LOOP AT FILE_TABLE.LV_COUNT = SY-TABIX.
CONCATENATE P_ATTACH '\' FILE_TABLE INTO LV_FILENAME.
MOVE LV_FILENAME TO L_FILE.
CALL FUNCTION 'GUI_UPLOAD'EXPORTING
FILENAME = L_FILE
FILETYPE = 'BIN'
TABLES
DATA_TAB = T_MAILHEX
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.
IF SY-SUBRC <> 0.MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.• YOU NEED TO MAINTAIN TEXT SYMBOLS AND WANT TO READ THEM IN THE PROGRAM
READ TEXTPOOL SY-REPID INTO TL_TEXTPOOL LANGUAGE SY-LANGU.
• MY TEXT SYMBOL IS " T04: You can Add your Body here"
READ TABLE TL_TEXTPOOL INTO WA_TEXTPOOL WITH KEY ID = 'I'KEY = 'T04' BINARY SEARCH.
IF SY-SUBRC = 0.WA_CONTENTS-LINE = WA_TEXTPOOL-ENTRY.
APPEND WA_CONTENTS TO IT_CONTENTS.ENDIF.• CREATE THE DOCUMENT WITH CONTENTS
CREATE OBJECT L_DOCUMENT.L_DOCUMENT = CL_DOCUMENT_BCS=>CREATE_DOCUMENT(
I_TYPE = 'HTM'
I_SUBJECT = L_SUBJECT
I_LENGTH = '1000'
I_LANGUAGE = SY-LANGU
I_IMPORTANCE = '1'
I_TEXT = IT_CONTENTS ).
• ATTACH THE FILE, THE ATTACHMENT TYPE SHOULD BE BIN TO ACCEPT ANY KIND OF ATTACHMENT, INCLUDING VIDEOS, AUDIO FILES ETC...
LV_ATTACH_NAME = FILE_TABLE.
CALL METHOD L_DOCUMENT->ADD_ATTACHMENTEXPORTING
I_ATTACHMENT_TYPE = 'BIN'
I_ATTACHMENT_SUBJECT = LV_ATTACH_NAME
I_ATT_CONTENT_HEX = T_MAILHEX.
• CREATING PERSISTENT OBJECT WILL ALLOW YOU TO SET THE DOCUMENT IN THE MAIL
W_DOCUMENT = CL_BCS=>CREATE_PERSISTENT( ).
CALL METHOD W_DOCUMENT->SET_DOCUMENT( L_DOCUMENT ).• EMAIL AS GIVEN IN THE SELECTION SCREEN.
GV_EMAIL = P_EMAIL.
GR_RECIPIENT = CL_CAM_ADDRESS_BCS=>CREATE_INTERNET_ADDRESS( GV_EMAIL ).
"Add recipient to send request
CALL METHOD W_DOCUMENT->ADD_RECIPIENTEXPORTING
I_RECIPIENT = GR_RECIPIENT
I_EXPRESS = 'X'.
• SEND THE MAIL
CALL METHOD W_DOCUMENT->SEND(EXPORTING
I_WITH_ERROR_SCREEN = 'X'
RECEIVING
RESULT = L_RESULT ).
• YOU CAN VERIFY THE STATUS IN THE LIST, YOU CAN ALSO SUBMIT THIS AS A BACKGROUND JOB.
IF L_RESULT = 'X'.WRITE :/ , 1(9) LV_COUNT, 10(55) FILE_TABLE-PATHNAME.
WRITE : 56(1) '-', 58(20) 'Mail sent'(003).
COMMIT WORK.
ELSE.
WRITE :/ , 1(9) LV_COUNT, 10(55) FILE_TABLE-PATHNAME.
WRITE : 56(1) '-', 58(30) 'Error in sending Mail'(004).
ROLLBACK WORK.
ENDIF.REFRESH IT_CONTENTS[].
ENDLOOP.You can Verify the Status of the Mail in SOST and SCOT as shown below respectively.
I Hope this WIKI would help many SAPians in sending mails with any type of attachment using ABAP.
Thanks & Regards,
Venkat Sesha
emails
sost
scot
attachments
attachment
formats
3 Comments
Former Member
Hi Venkat Sesha,
Thank you for the detailed explanation with screen shots. It is very helpful.
We tried using this and it worked perfectly. But , we had an issue with the sender email . By default this program is taking the SAP logon as the email id.
Is there a way we can specify the sender email id?
Thanks
Sujata Kolla
Aug 06, 2014
Former Member
Hi Venkat,
I really appreciate your effort. Wonder full..Keep it up..
Regards,
Abu Fazi
Feb 04, 2015
V S BHARGAV MYLAVARAPU
Thanks for your complements...
Working with files
Skip to end of metadata
Created by Sandra Rossi, last modified on Nov 04, 2009
Go to start of metadata
Table of contents
Introduction
Working with files on the application server
How to create or write to a file
How to read a file
Checking file existence
How to get list of files in a directory
Is there a search help for selecting a file
Best practice
Miscellaneous
Working with files on presentation server
How to create or write a file
How to read a file
Checking file existence
How to get list of files in a directory
Is there a search help for selecting a file or directory
Is it possible to work in background?
Miscellaneous
Copying from server to frontend or vice versa
File compression (zip)
Working with special files
Excel file types
OLE/DOI for Office native files (Excel, Word, etc.)
Introduction
In this article, only general directions are given. You may find snippets here
Files are handled differently when they are on the application server, or on the presentation server
Files may be processed in 2 modes, either text (in a given encoding), or binary (generated by a software like Microsoft Office, Adobe Acrobat, etc.) From ABAP, you may directly write and read text files, while binary files can only be written and read by the software. The mode is chosen when opening the file.
Working with files on the application server
Transaction code AL11 takes you to the application server in which you find the sap directories and the files inside the directories.
The configure button on the main screen can be used to create and manage links to existing directories.
Files may be processed in 2 ways, decided in OPEN DATASET statement: either as text files with a given encoding (IN TEXT MODE, ENCODING and CODE PAGE additions), or as binary files (IN BINARY MODE addition).
How to create or write to a file
Use ABAP statements OPEN DATASET with FOR OUTPUT keyword, TRANSFER, CLOSE DATASET.
How to read a file
Use ABAP statements OPEN DATASET with FOR INPUT keyword, READ DATASET and CLOSE DATASET.
Checking file existence
either OCS_GET_FILE_INFO function module
or "OPEN DATASET" ABAP statement
How to get list of files in a directory
To read the names of files present in a particular path on the application server we may use SUBST_GET_FILE_LIST or EPS_GET_DIRECTORY_LISTING function modules.
Is there a search help for selecting a file
F4_DXFILENAME_TOPRECURSION function module may be used to display a search help to choose a file
Snippet
Best practice
...