How to send a mail through SAP and not to die trying it

Update: I’ve wrote a function module that allows you to send VERY SIMPLE mails (no attachments, no weird things) here.

Long time ago, before becoming a tuly idiot, I’ve had to face a little trouble: I had to send a mail with an attachment through SAP, and was not able to find by myself how to do it.

I’ve searched around the net (basically SAPFans :P) and managed how to do it.

I’ve posted there (in SAPFans, I mean) how to do it, but from time to time, someone re-asks the same question, and I’m tired of use the search button in SF. Today I did it again (the search, I mean… I’m not enough lucky) and though “why not to put it in the club’s page? I will save some time in the future…” or something like that.

The link here: http://sapfans.com/forums/viewtopic.php?p=543631
And the post itself after the jump 😛

I was only 6 months old in the SAP world, ignore my blatant mistakes in the code, if you find them.

The post:

This sample will send a pdf and a tif files as attachments… modify as you need, but don’t ask me for more, I had some headhaches (Rich could confirm it) trying to reach it.

I don’t use the sy-uname to get the sender (it seems was too obvious, and the user wanted a customized list of available senders… these options are in a z table z_destmail.

The body text is also saved in that z table, but I left the option to not find the sender nor the text, gettin g sy-uname and some fixed text.

The commented lines are in catalan (not in castillian 😉 ) look for a translator if you are not able to understand the code (I normally use comments for people who will need to understand my code, not for foreigners… in any case, I think any natural ABAP speaker could understand that code easily).

The standard function to send the mail is commented, because it doesn’t allow you to change the sender. If you debug it, you will find the one I’m using at the end of the process, with some parameters fixed. I used it changing the parameters I needed.

Pagina1 is the pdf path, and pagina2 the tif path…

FORM ENVIAMENT_CORREU
USING PAGINA1
PAGINA2.

DATA: FRONTEND_SERVICES TYPE REF TO CL_GUI_FRONTEND_SERVICES.
DATA: T_PDF TYPE TABLE OF SOLI, ".pdf contents
T_TIF TYPE TABLE OF SOLI, ".tif contents
T_ANN TYPE TABLE OF SOLI. "All attachments
DATA: L_ANN TYPE SOLI. "attachment standard line
DATA: TAM_PDF TYPE I, ".pdf line
TAM_TIF TYPE I, ".tif lie
TAM_ANN TYPE I. "attachment line

DATA: ELPATH TYPE STRING. "type conversion
DATA: NOMFITXER(255) TYPE C, "to split path and filename
EXTENSIO(3) TYPE C, "filename extension
ESBORRAR(128) TYPE C. "to delete

DATA: TEXTS LIKE SOLISTI1 OCCURS 10 WITH HEADER LINE,
LIN_TEXT TYPE I.

DATA: OBJPACK LIKE SOPCKLSTI1 OCCURS 0 WITH HEADER LINE.
DATA: DOC_CHNG LIKE SODOCCHGI1.

DATA: ENVIAT LIKE SONV-FLAG.

DATA: REMITENT LIKE SOEXTRECI1-RECEIVER,
DADESUSER TYPE TABLE OF BAPIADSMTP WITH HEADER LINE,
BAPIRETUR TYPE TABLE OF BAPIRET2 WITH HEADER LINE.

* --------------------ANNEXOS---------------------
* 1 - .pdf
ELPATH = PAGINA1. "type change

* We use objects because GUI_UPLOAD
* is marked as obsolete, although the
* "new" code itself calls it
IF FRONTEND_SERVICES IS INITIAL.
CREATE OBJECT FRONTEND_SERVICES.
ENDIF.
CALL METHOD FRONTEND_SERVICES->GUI_UPLOAD
EXPORTING
FILENAME = ELPATH
FILETYPE = 'BIN'
READ_BY_LINE = ' '
IMPORTING
FILELENGTH = TAM_PDF
CHANGING
DATA_TAB = T_PDF
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
NOT_SUPPORTED_BY_GUI = 17
OTHERS = 18.

IF SY-SUBRC 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.

* 2 - .tif (o any kind of file, really)
ELPATH = PAGINA2. "type change

* We use objects because GUI_UPLOAD
* is marked as obsolete, although the
* "new" code itself calls it
IF FRONTEND_SERVICES IS INITIAL.
CREATE OBJECT FRONTEND_SERVICES.
ENDIF.
CALL METHOD FRONTEND_SERVICES->GUI_UPLOAD
EXPORTING
FILENAME = ELPATH
FILETYPE = 'BIN'
READ_BY_LINE = ' '
IMPORTING
FILELENGTH = TAM_TIF
CHANGING
DATA_TAB = T_TIF
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
NOT_SUPPORTED_BY_GUI = 17
OTHERS = 18.

IF SY-SUBRC 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.

*-------------------------------------------------------------
* Attachment sum
LOOP AT T_PDF INTO L_ANN.
APPEND L_ANN TO T_ANN.
ENDLOOP.
LOOP AT T_TIF INTO L_ANN.
APPEND L_ANN TO T_ANN.
ENDLOOP.

* Sizes
DESCRIBE TABLE T_PDF LINES TAM_PDF.
DESCRIBE TABLE T_TIF LINES TAM_TIF.
DESCRIBE TABLE T_ANN LINES TAM_ANN.

* From field and mail text
SELECT SINGLE *
INTO T_DESTI
FROM ZMAILDEST
WHERE TCODE = SY-TCODE
AND ZCOMPT = 0.

IF SY-SUBRC = 0.
REMITENT = T_DESTI-ZDESTI.
IF T_DESTI-ZTEXT1 IS NOT INITIAL.
TEXTS = T_DESTI-ZTEXT1.
ELSE.
TEXTS = 'Adjunt trameto l''expedient indicat.'.
ENDIF.
APPEND TEXTS.
IF T_DESTI-ZTEXT2 IS NOT INITIAL.
TEXTS = T_DESTI-ZTEXT2.
APPEND TEXTS.
ENDIF.
IF T_DESTI-ZTEXT3 IS NOT INITIAL.
TEXTS = T_DESTI-ZTEXT3.
APPEND TEXTS.
ENDIF.
ELSE.
* User address search
CALL FUNCTION 'BAPI_USER_GET_DETAIL'
EXPORTING
USERNAME = SY-UNAME
TABLES
RETURN = BAPIRETUR
ADDSMTP = DADESUSER.

READ TABLE BAPIRETUR WITH KEY TYPE = 'E'.
IF SY-SUBRC NE 4.
MESSAGE E039 WITH
'No es pot determinar remitent. Enviament avortat.'.
ELSE.
* found
READ TABLE DADESUSER INDEX 1.
REMITENT = DADESUSER-E_MAIL.
ENDIF.
* If we are here, we have no specified text
TEXTS = 'Adjunt trameto l''expedient indicat.'.
APPEND TEXTS.
ENDIF.
DESCRIBE TABLE TEXTS LINES LIN_TEXT.

* Subject
CONCATENATE 'TB Num. Expedient:'
ZACEXPEDI-NUMEXPED
INTO TEXTS SEPARATED BY SPACE.

DOC_CHNG-OBJ_NAME = 'URGENT'.
DOC_CHNG-EXPIRY_DAT = SY-DATUM + 10.
DOC_CHNG-OBJ_DESCR = TEXTS.
DOC_CHNG-SENSITIVTY = 'O'.
DOC_CHNG-DOC_SIZE = LIN_TEXT * 255.

* Header
CLEAR OBJPACK-TRANSF_BIN.
OBJPACK-HEAD_START = 1.
OBJPACK-HEAD_NUM = 0.
OBJPACK-BODY_START = 1.
OBJPACK-BODY_NUM = LIN_TEXT.
OBJPACK-DOC_TYPE = 'RAW'.
APPEND OBJPACK.

* 1st attachmet
* Splitting file and path to get the file type
NOMFITXER = PAGINA1.
CALL FUNCTION 'SPLIT_FILENAME'
EXPORTING
LONG_FILENAME = NOMFITXER
IMPORTING
PURE_EXTENSION = EXTENSIO.

OBJPACK-TRANSF_BIN = 'X'.
OBJPACK-HEAD_START = 1.
OBJPACK-HEAD_NUM = 1.
OBJPACK-BODY_START = 1.
OBJPACK-BODY_NUM = TAM_PDF.
OBJPACK-DOC_TYPE = EXTENSIO.
OBJPACK-OBJ_NAME = 'pagina1'.
CONCATENATE 'pagina1' EXTENSIO
INTO NOMFITXER SEPARATED BY '.'. "toguether again
OBJPACK-OBJ_DESCR = NOMFITXER.
OBJPACK-DOC_SIZE = TAM_PDF * 255.
APPEND OBJPACK.

* 2nd attachment
* Splitting file and path to get the file type
NOMFITXER = PAGINA2.
CALL FUNCTION 'SPLIT_FILENAME'
EXPORTING
LONG_FILENAME = NOMFITXER
IMPORTING
PURE_EXTENSION = EXTENSIO.

OBJPACK-TRANSF_BIN = 'X'.
OBJPACK-HEAD_START = 2.
OBJPACK-HEAD_NUM = 2.
OBJPACK-BODY_START = TAM_PDF + 1.
OBJPACK-BODY_NUM = TAM_TIF.
OBJPACK-DOC_TYPE = EXTENSIO.
OBJPACK-OBJ_NAME = 'pagina2'.
CONCATENATE 'pagina2' EXTENSIO
INTO NOMFITXER SEPARATED BY '.'. "toguether again
OBJPACK-OBJ_DESCR = NOMFITXER.
OBJPACK-DOC_SIZE = TAM_TIF * 255.
APPEND OBJPACK.

* VLR - 13/04/2005
* Original function SO_DOCUMENT_SEND_API1
* Although it's obsolete, we need to change the sender
*-------------------------------------------------------
CALL FUNCTION 'SO_DOCUMENT_SEND_API1'
EXPORTING
DOCUMENT_DATA = DOC_CHNG
PUT_IN_OUTBOX = 'X'
SENDER_ADDRESS = REMITENT
SENDER_ADDRESS_TYPE = 'SMTP'
COMMIT_WORK = ' '
IMPORTING
SENT_TO_ALL = ENVIAT
* NEW_OBJECT_ID =
* SENDER_ID =
TABLES
PACKING_LIST = OBJPACK
* OBJECT_HEADER =
CONTENTS_BIN = T_ANN
CONTENTS_TXT = TEXTS
* CONTENTS_HEX =
* OBJECT_PARA =
* OBJECT_PARB =
RECEIVERS = DESTINATARIS
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.
**** CALL FUNCTION 'SO_NEW_DOCUMENT_ATT_SEND_API1'
**** EXPORTING
**** DOCUMENT_DATA = DOC_CHNG
**** PUT_IN_OUTBOX = 'X'
**** COMMIT_WORK = 'X'
**** IMPORTING
**** SENT_TO_ALL = ENVIAT
***** NEW_OBJECT_ID =
**** TABLES
**** PACKING_LIST = OBJPACK
***** OBJECT_HEADER =
**** CONTENTS_BIN = T_ANN
**** CONTENTS_TXT = TEXTS
***** CONTENTS_HEX =
***** OBJECT_PARA =
***** OBJECT_PARB =
**** RECEIVERS = DESTINATARIS
**** 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 E041
WITH '(' SY-SUBRC ') No s''ha pogut enviar el correu.'.
ENDIF.

* Attachment deletion
ESBORRAR = PAGINA1(128).
CALL FUNCTION 'GUI_DELETE_FILE'
EXPORTING
FILE_NAME = ESBORRAR
EXCEPTIONS
FAILED = 1
OTHERS = 2.

ESBORRAR = PAGINA2(128).
CALL FUNCTION 'GUI_DELETE_FILE'
EXPORTING
FILE_NAME = ESBORRAR
EXCEPTIONS
FAILED = 1
OTHERS = 2.

ENDFORM. "enviament_correu

It costed me a severe headhache and, of course, some hours of work. But I must recognize the fountain of any knowledge needed to do that thing provides from here:
http://sapfans.com/forums/viewtopic.php?t=86370
(of course, if you used the search button, you had to found it, as I did)

Posting my code here is a form to say Thanks to anyone involved in that thread, and for all these ones who posted useful information in related threads I will not link because you must be tired of read them if you used the search button.
Additional thanks must be given to Rich, who had to read some useless PMs because I needed someone to talk with (it’s a sad work, you know).

Wish it helps,
Vic

P.S.: I know that code is able to be modified for a better use and performance. But I want/cannot to do it now. Any additional comments will be appreciated. Hints for another ways will be appreciated too… maybe the next time I need to send a mail I’ll can use these hints.

Advertisements

One thought on “How to send a mail through SAP and not to die trying it

  1. Pingback: The Google’d ABAP questions « The Tuly Idiots’ Club

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s