More SAP mailing… the easy one

I had to send again a mail through SAP. I used my older code and found it a lot heavy for my needs. I managed to create a Z function that allows me to send a simple mail without having to fill all those nasty tables.

Maybe it exists in SAP standard, but I’ve been not able to find it.

FUNCTION z_xx01_simple_mail_sending.
*"----------------------------------------------------------------------
*"*"Local interface:
*" IMPORTING
*" REFERENCE(I_SINGLE_URL) TYPE SO_RECNAME OPTIONAL
*" REFERENCE(I_RECEIVER) TYPE SOMLRECI1 OPTIONAL
*" REFERENCE(I_SUBJECT) TYPE SO_OBJ_DES OPTIONAL
*" REFERENCE(I_BODY) TYPE SO_TEXT255 OPTIONAL
*" EXPORTING
*" VALUE(SENT_TO_ALL) LIKE SONV-FLAG
*" VALUE(NEW_OBJECT_ID) LIKE SOFOLENTI1-OBJECT_ID
*" TABLES
*" PACKING_LIST STRUCTURE SOPCKLSTI1 OPTIONAL
*" OBJECT_HEADER STRUCTURE SOLISTI1 OPTIONAL
*" CONTENTS_BIN STRUCTURE SOLISTI1 OPTIONAL
*" CONTENTS_TXT STRUCTURE SOLISTI1 OPTIONAL
*" CONTENTS_HEX STRUCTURE SOLIX OPTIONAL
*" OBJECT_PARA STRUCTURE SOPARAI1 OPTIONAL
*" OBJECT_PARB STRUCTURE SOPARBI1 OPTIONAL
*" RECEIVERS STRUCTURE SOMLRECI1 OPTIONAL
*" EXCEPTIONS
*" TOO_MANY_RECEIVERS
*" DOCUMENT_NOT_SENT
*" DOCUMENT_TYPE_NOT_EXIST
*" OPERATION_NO_AUTHORIZATION
*" PARAMETER_ERROR
*" X_ERROR
*" ENQUEUE_ERROR
*"----------------------------------------------------------------------
*---------------------------------------------------------------------*
* Header:
* -------
* Program Name: Z_XX01_SIMPLE_MAIL_SENDING
* Description : SO_NEW_DOCUMENT_ATT_SEND_API1 interface
*---------------------------------------------------------------------*
* IT Change History
* -----------------
* 1.
* Remark: 1).
*----------------------------------------------------------------------*
DATA: document_data TYPE sodocchgi1,
packing_line TYPE sopcklsti1,
receiver TYPE somlreci1,
header TYPE solisti1.
DATA: content_txt TYPE solisti1,
lines TYPE i.

* Receiver -----------------------------------------------
IF NOT i_single_url IS INITIAL.
receiver-receiver = i_single_url.
receiver-rec_type = 'U'. "URL-internet
APPEND receiver TO receivers.
ENDIF.
IF NOT i_receiver IS INITIAL.
APPEND i_receiver TO receivers.
ENDIF.

* Body of the message ------------------------------------
IF NOT i_body IS INITIAL.
content_txt-line = i_body.
APPEND content_txt TO contents_txt.
ENDIF.
DESCRIBE TABLE contents_txt.
lines = sy-tfill.

* Content -------------------------------------------------
document_data-obj_name = 'MAIL'.
document_data-obj_descr = i_subject. "subject
document_data-sensitivty = 'P'.
document_data-doc_size = lines * 255.

* Subject -------------------------------------------------
header-line = i_subject.
APPEND header TO object_header.

* Packing list --------------------------------------------
CLEAR packing_line-transf_bin.
packing_line-head_start = 1.
packing_line-head_num = 0.
packing_line-body_start = 1.
packing_line-body_num = lines.
packing_line-doc_type = 'RAW'.
APPEND packing_line TO packing_list.

CALL FUNCTION 'SO_NEW_DOCUMENT_ATT_SEND_API1'
EXPORTING
document_data = document_data
* PUT_IN_OUTBOX = ' '
IMPORTING
sent_to_all = sent_to_all
* NEW_OBJECT_ID =
TABLES
packing_list = packing_list
object_header = object_header
contents_bin = contents_bin
contents_txt = contents_txt
contents_hex = contents_hex
object_para = object_para
object_parb = object_parb
receivers = receivers
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.
CASE sy-subrc.
WHEN 0.
* Woooohoooooo!!
WHEN 1.
RAISE too_many_receivers.
WHEN 2.
RAISE document_not_sent.
WHEN 3.
RAISE document_type_not_exist.
WHEN 4.
RAISE operation_no_authorization.
WHEN 5.
RAISE parameter_error.
WHEN 6.
RAISE x_error.
WHEN 7.
RAISE equeue_error.
WHEN 8.
MESSAGE e158(zppes00) WITH 'Error sending mail'.
ENDCASE.
ENDFUNCTION.

It’s use is easy:
– if you want to send the mail to a single address, just fill the I_SINGLE_URL field
– if you want to send the mail to a non-URL address, fill the I_RECEIVER structure
– the subject of your mail? I_SUBJECT
– a short body? I_BODY

Update: Remember to call a COMMIT after the function call. If you want not, you can use the COMMIT_WORK parameter of the SO* function module called here.

I kept the original parameters and exceptions of the original function module, to allow people to make the mail as complicated as needed.

Enjoy it!

Advertisements

One thought on “More SAP mailing… the easy one

  1. Pingback: How to send a mail through SAP and not to die trying it « 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