Sending Emails and SAP Mail
来源:互联网 发布:最新网络诈骗案例 编辑:程序博客网 时间:2024/06/07 07:18
SAP is a robust system, which gives many facilities in the form of Function Modules (FMs) for connecting to external systems or for use within the system. With a clever use of these FMs we can achieve a lot of things through ABAP code.
This article focuses on ways to send E-mails and SAP Mails using ABAP code.
Firstly SAP Mail
A SAP mail is a mail internal to the SAP system. It is a very good forum to exchange information with other users. Using a SAP mail in ABAP code facilitates exchange of automatic messages at various stages of the business process. It is easy to use and saves many hassles involved in using workflows for exchanging messages.
The ABAP code to send a sap mail is built around the FM SO_OBJECT_SEND which has the following pattern.
call function 'SO_OBJECT_SEND'
exporting
* EXTERN_ADDRESS = ' '
* FOLDER_ID = ' '
* FORWARDER = ' '
* OBJECT_FL_CHANGE = ' '
* OBJECT_HD_CHANGE = ' '
* OBJECT_ID = ' '
* OBJECT_TYPE = ' '
* OUTBOX_FLAG = ' '
* OWNER = ' '
* STORE_FLAG = ' '
* DELETE_FLAG = ' '
* SENDER = ' '
* CHECK_ALREADY_SENT = ' '
importing
object_id_new =
sent_to_all =
tables
* OBJCONT =
* OBJHEAD =
* OBJPARA =
* OBJPARB =
receivers =
* PACKING_LIST =
* ATT_CONT =
* ATT_HEAD =
* NOTE_TEXT =
exceptions
active_user_not_exist = 1
communication_failure = 2
component_not_available = 3
folder_not_exist = 4
folder_no_authorization = 5
forwarder_not_exist = 6
note_not_exist = 7
object_not_exist = 8
object_not_sent = 9
object_no_authorization = 10
object_type_not_exist = 11
operation_no_authorization = 12
owner_not_exist = 13
parameter_error = 14
substitute_not_active = 15
substitute_not_defined = 16
system_failure = 17
too_much_receivers = 18
user_not_exist = 19
x_error = 20
others = 21.
In the next article we will understand the main parameters and table interfaces of the FM.
Import Parameters Name and Description Field name Field function Object_hd_change (structure)Contains the process to be done when SAP mail is marked for execution.
When the Execute Icon in the SAP mail is clicked the corresponding object is executed
vmtyp ‘D’ for dialog module‘F’ for function module
‘R’ for report
‘T’ for transaction
‘U’ for transaction with export
Acnam Name of the object which is to be executed, like name of transaction/report Skips ‘X’ to execute first screen in background Objsns Indicates sensitivity of the object.‘P’ for private object
‘F’ for functional object
‘O’ for confidential object
‘C’ for company confidential object
Sensitivity level is restricted to ‘O’ for documents in shared folders
Objla Language of the document‘E’ for English
sy-langu for system language
Objnam Name of the document Objdes Short description (Title) of the document Objsrt Name of the sort field. This is used to group documents based on certain criteria Object_typeType of document to be sent with mail
‘RAW’ for raw text (default)‘DOC’ for word file
‘XLS’ for excel file
All classes can be used except for folders (‘FOL’) and distribution lists (‘DLI’)
Outbox_flag ‘X’ to indicate that mail should also be stored in outbox of the user after sending (default ‘ ‘) Owner Sap login of the user responsible for transmission Table Parameters Name and Description Field Name Field function ObjcontTable to hold the body of the mail
Line Text lines up to 255 characters ObjheadTable to hold number of lines in the body of the mail, i.e size of the table in lines
Line Text lines up to 255 characters ObjparaTable to hold the set/get parameters to be transferred to the processing element
Name Name of the parameter. Only first three characters are used Option Not used Low Value of the parameter in name High Not used ObjparbTable to hold information for mails to which a certain processing type is assigned. For a report or transaction with transfer of values to global memory, the table has to contain the parameters with relevant values. The memory id is taken from the first row. For a FM or dialog module, data in table will be transferred as table parameter msgdial
Name For report or transaction with transfer of values to global memory, the field for first row should hold the name of the memory id used for export and the other rows should hold the parameter names. For FM or dialog module this field should hold the values as per the usage of the module Value For report or transaction with transfer of values to global memory, the field for first row should remain empty and the other rows should hold values of the parameters. For FM or dialog module this field should hold the values as per the usage of the module ReceiversTable to hold recipient details
Recnam SAP login of the recipient. Append all the recipients to this table Sndcp X for sending mail as a copy Sndex X for sending as express document. This will prompt a logged on recipient saying that he or she has received an express mail Recesc ‘B’ for SAP user‘E’ for external email address
‘U’ for unix address
Export Parameters Name and Description Field name Field function Object_id_newContains object id of document created during send process
Sent_to_allX indicates that document was sent to all recipients. Flag is not activated if sending fails in one or more cases
Main Exceptions Name and Description Description Too_much_receivers Number of recipients is greater than number for which sender is authorised to send Object_not_sent Document was not delivered to any of the recipients Object_not_exist Document class specified does not exist or cannot be sent Object_no_authorisation Document could not be sent as one of the required authorisations does not exist Parameter_error Invalid combination of parameter values transferred to FM X_error Internal error occurredOnce all the parameters and table interfaces are properly filled, call the function module to send the SAP mail to the recipient inbox.
Care should be taken to report the status of execution of the function module to the user using one of the exceptions defined (refer function module pattern above)
Though SAP mail is a very robust method of interacting with users within SAP system, it is always good to receive email in Microsoft inbox. This also works like an additional notification to users in case they do not check SAP mail regularly. The next article covers sending E-mails using ABAP.
Sending Email to a non-SAP system:
Though SAP mail is a very robust method of interacting with users within SAP system, it is always good to receive email in Microsoft inbox. This also works like an additional notification to users in case they do not check SAP mail regularly.
Sending an email to the Microsoft Inbox is a way of interacting with a non SAP system through ABAP code and hence is very interesting. A fair knowledge of UNIX shell scripting is assumed here.
The ABAP code to send an email to Microsoft inbox revolves around following UNIX script
Echo "From:" "<"$1">" > <unix file path name>
Echo "To:" "<"$2">" >> <unix file path name>
Echo "Subject:" "<"$3">" >> <unix file path name>
Cat $4 >> <unix file path name>
Uuencode $5 $6 >> <unix file path name>
Cat <unix file path name> | /usr/sbin/sendmail –f $fraddr $toaddr
(Note : the commands in the above script can be case sensitive. Check the actual case on the unix installation in question)
call function 'SXPG_CALL_SYSTEM'
exporting
commandname =
PARAMETERS = ' '
importing
status =
tables
exec_protocol =
exceptions
no_permission = 1
command_not_found = 2
parameters_too_long = 3
security_risk = 4
wrong_check_call_interface = 5
program_start_error = 6
program_termination_error = 7
x_error = 8
parameter_expected = 9
too_many_parameters = 10
illegal_command = 11
others = 12.
In the next article we will understand the main parameters and table interfaces of the FM.
Once the above FM is invoked with the necessary parameters the unix program sendmail will send a mail to the inbox of the recipient in Microsoft.
The restriction on the parameter string length of 128 characters can be removed to make way for very long email addresses and subjects. Build the parameters into an internal table and download it as a file on the unix server. Instead of passing each parameter individually, pass the unix server path of this file as the parameter to the unix shell script (sndmail mentioned above). Modify the shell script to read every line of this parameter file as variables to be used in the shell script.
In the next article we will see an example of the ABAP code you can use to send SAP Mails.
REPORT ZTSAPMAIL.
DATA: X_OBJECT_TYPE LIKE SOOD-OBJTP.
DATA: BEGIN OF X_OBJECT_HD_CHANGE.
INCLUDE STRUCTURE SOOD1.
DATA: END OF X_OBJECT_HD_CHANGE.
DATA: BEGIN OF X_OBJCONT OCCURS 10.
INCLUDE STRUCTURE SOLI.
DATA: END OF X_OBJCONT.
DATA: BEGIN OF X_OBJHEAD OCCURS 0.
INCLUDE STRUCTURE SOLI.
DATA: END OF X_OBJHEAD.
DATA: BEGIN OF RAW_HEAD.
INCLUDE STRUCTURE SORH.
DATA: END OF RAW_HEAD.
DATA: BEGIN OF X_RECEIVERS OCCURS 0.
INCLUDE STRUCTURE SOOS1.
DATA: END OF X_RECEIVERS.
PARAMETERS: RECEIVER LIKE X_RECEIVERS-RECNAM. " Name
*BUILD MESSAGE HEADER
MOVE 'Sort field goes here' TO X_OBJECT_HD_CHANGE-OBJSRT. " Sort field
MOVE 'Name of the object goes here' TO X_OBJECT_HD_CHANGE-OBJNAM. " Name
MOVE 'Document title goes here' TO X_OBJECT_HD_CHANGE-OBJDES. " Title
MOVE 'F' TO X_OBJECT_HD_CHANGE-OBJSNS. " Functional OBJECT
MOVE 'E' TO X_OBJECT_HD_CHANGE-OBJLA. " Language
* Object type of the new document
MOVE 'RAW' TO X_OBJECT_TYPE.
CLEAR X_OBJCONT.
MOVE 'Contents of mail' TO X_OBJCONT-LINE.
APPEND X_OBJCONT.
CLEAR X_OBJCONT-LINE. APPEND X_OBJCONT.
MOVE 'More contents' TO X_OBJCONT-LINE.
APPEND X_OBJCONT.
MOVE 'Still more contents'
to x_objcont-line.
APPEND X_OBJCONT.
MOVE ' ' TO X_OBJCONT-LINE.
APPEND X_OBJCONT.
* Specific header (Dependent on the object type, here RAW)
REFRESH X_OBJHEAD.
DESCRIBE TABLE X_OBJCONT LINES RAW_HEAD-RAWSIZ.
MOVE RAW_HEAD TO X_OBJHEAD.
APPEND X_OBJHEAD.
*RECEIVERS table
CLEAR X_RECEIVERS.
REFRESH X_RECEIVERS.
MOVE RECEIVER TO X_RECEIVERS-RECNAM. " Name
MOVE 'B' TO X_RECEIVERS-RECESC. " Receiver type
MOVE 'X' TO X_RECEIVERS-SNDCP. " Send as a copy
MOVE 'X' TO X_RECEIVERS-SNDEX. " EXPRESS DOCUMENT
APPEND X_RECEIVERS.
CALL FUNCTION 'SO_OBJECT_SEND'
EXPORTING
* folder_id = 'OUTBOX'
* forwarder = x_forwarder
* object_fl_change = x_object_fl_change
OBJECT_HD_CHANGE = X_OBJECT_HD_CHANGE
* object_id = x_object_id
OBJECT_TYPE = X_OBJECT_TYPE
OUTBOX_FLAG = 'X'
OWNER = SY-UNAME
* store_flag = x_store_flag
* importing
* object_id_new = x_object_id_new
* sent_to_all = x_sent_to_all "May need to use
TABLES
OBJCONT = X_OBJCONT
OBJHEAD = X_OBJHEAD
* objpara = x_objpara
* objparb = x_objparb
RECEIVERS = X_RECEIVERS.
In the next article we will see example ABAP code for sending Emails to a non-SAP system.
REPORT ZTUNIXMAIL.
*
DATA : TO_ADDRESS LIKE SY_LISEL,
FROM_ADDRESS LIKE SY-LISEL,
SUBJECT LIKE SY-LISEL,
ATTACHMENT_NAME LIKE SY_LISEL,
DATA_FILE_PATH LIKE SXPGCOLIST-PARAMETERS,
BODY_FILE_PATH LIKE SXPGCOLIST-PARAMETERS.
DATA : BEGIN OF INT_EMAIL_ATTACH OCCURS 0,
TXTLINE CHAR(255),
END OF INT_EMAIL_ATTACH.
DATA : BEGIN OF INT_EMAIL_BODY OCCURS 0,
TXTLINE CHAR(255),
END OF INT_EMAIL_BODY.
CLEAR : INT_EXEC_PROTOCOL,INT_EMAIL_ATTACH,INT_EMAIL_BODY.
REFRESH : INT_EXEC_PROTOCOL,INT_EMAIL_ATTACH,INT_EMAIL_BODY.
*
INT_EMAIL_ATTACH-TXTLINE = 'Put all attachment text in this table'.
APPEND INT_EMAIL_ATTACH. CLEAR INT_EMAIL_ATTACH.
INT_EMAIL_BODY-TXTLINE = 'Put all attachment text in this table'.
APPEND INT_EMAIL_BODY. CLEAR INT_EMAIL_BODY.
*
CONCATENATE TO_ADDRESS
FROM_ADDRESS
SUBJECT
BODY_FILE_PATH
DATA_FILE_PATH
ATTACHMENT_NAME
INTO V_PARAMETERS.
*
IF NOT INT_EMAIL_ATTACH[] IS INITIAL.
OPEN DATASET DATA_FILE_PATH FOR OUTPUT IN TEXT MODE.
LOOP AT INT_EMAIL_ATTACH.
TRANSFER INT_EMAIL_ATTACH-TXTLINE TO DATA_FILE_PATH.
ENDLOOP.
CLOSE DATASET DATA_FILE_PATH.
ENDIF.
*
IF NOT INT_EMAIL_BODY[] IS INITIAL.
OPEN DATASET BODY_FILE_PATH FOR OUTPUT IN TEXT MODE.
LOOP AT INT_EMAIL_BODY.
TRANSFER INT_EMAIL_BODY-TXTLINE TO BODY_FILE_PATH.
ENDLOOP.
CLOSE DATASET BODY_FILE_PATH.
ENDIF.
*
CALL FUNCTION 'SXPG_CALL_SYSTEM'
EXPORTING
COMMANDNAME = 'Z_EMAIL' - Command calling unix script
PARAMETERS = V_PARAMETERS
* importing
* status = ''
TABLES
EXEC_PROTOCOL = INT_EXEC_PROTOCOL
EXCEPTIONS
NO_PERMISSION = 1
COMMAND_NOT_FOUND = 2
PARAMETERS_TOO_LONG = 3
SECURITY_RISK = 4
WRONG_CHECK_CALL_INTERFACE = 5
PROGRAM_START_ERROR = 6
PROGRAM_TERMINATION_ERROR = 7
X_ERROR = 8
PARAMETER_EXPECTED = 9
TOO_MANY_PARAMETERS = 10
ILLEGAL_COMMAND = 11
OTHERS = 12.
We hope you have enjoyed reading this tutorial. If you are interested in learning more, check out the online SAP training courses offered by
Import Parameters Name and Description Field name Field function Commandname
Name of unix shell script or command to be executed by SAP
Name of unix shell script to be invoked Parameters The parameters to be sent to unix shell script ($1, $2 etc). Send parameters as a concatenated string separated by space and length not exceeding 128 charactersFor eg in UNIX environment the above shell script would be executed as
Sndmail
We will be simulating this command using the FM from SAP system
sender@a.com receiver@b.com Trial /home/test.doc testmail. Table Parameters Name and Description Field Name Field function Exec_protocol (structure)Table to get messages from unix server after shell script is executed
Length Length of the message from external program i.e unix Message Log message from external program i.e unix Export Parameters Name and Description Field name Field function StatusContains the status of execution of external program
Scheduling status of external program i.e unix
Let us understand the various parts of the above script.
$1 = Sender email address
$2 = Recipient email address
$3 = Subject of the email
$4 = Path of unix server file having email body
Form email body as an internal table in ABAP program, download it to a unix server file
$5 = Path of unix server file to be sent as email attachment
$6 = Name to be given to the attachment (like test1.doc, test1.xls). The corresponding Microsoft icon
will be shown in the email for the type of file attached ( Word document, excel document etc)
The script builds a temporary file and pipes the file to the sendmail command to achieve the mission.
This script can be invoked from SAP to send the mail to the intended recipient. Store this small script on the unix server. (Assume script name is sndmail )
To do this we should define a link in the SAP system between a customized command and this unix script.
The FM to define a customized command in SAP system has the following pattern.
- Sending Emails and SAP Mail
- Sending Emails and SAP Mail
- Sending Multiple Emails At Once
- Sending Emails with the Zend Framework
- Sending Emails with Django CSRF Protection
- Sending e-mail from Oracle
- 发送邮件Failure sending mail
- Sending E-mail using C#
- Sending HTML emails with embedded images in BizTalk
- Sending and Receiving Packets
- Sending mail from AX using .NET Framework.
- Ruby1.8.7 encode! undefined when mail sending!
- Test Mail Sending Mechanism via GreenMail
- C# 发送邮件 Failure sending mail
- 3-6. Sending an E-mail
- sap mail express
- 5 Sending and Receiving Messages
- Write Great Emails - Dos and Don'ts
- 自定义标签的开发----内嵌标签的开发
- eclipse.ini内存设置
- Asp.Net中获取Castle容器中的服务的另一方法
- 内存对齐
- 利用Session防止表单重复提交
- Sending Emails and SAP Mail
- AVI文件格式
- Castle在“新.NET时代”将何去何从
- MySQL参数说明
- JSP URL中文亂碼問題~~
- 根据类别显示不同的页面元素
- 关于人事,户口等问题,我需要记录下来
- ASP.NET MVC+LINQ开发一个图书销售站点
- Say hello to everyone