Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Sending Mails to SAP inbox

Former Member
0 Kudos

Hi,

Please send me the code to send mails to SAP Inbox .

Regards,

Ram.

3 REPLIES 3

Former Member
0 Kudos

***********************************************************************

************************************************************************

    • Table Declarations

************************************************************************

TABLES: SOLI.

************************************************************************

    • Data Declarations

************************************************************************

SELECTION-SCREEN BEGIN OF BLOCK B1 WITH FRAME TITLE TEXT-001.

PARAMETERS: SAPID RADIOBUTTON GROUP ADDR,

EMAIL_ID RADIOBUTTON GROUP ADDR.

SELECTION-SCREEN END OF BLOCK B1.

SELECTION-SCREEN BEGIN OF BLOCK B2 WITH FRAME TITLE TEXT-002.

SELECT-OPTIONS: ID FOR SOLI-LINE NO INTERVALS.

SELECT-OPTIONS: CC FOR SOLI-LINE NO INTERVALS.

SELECT-OPTIONS: BCC FOR SOLI-LINE NO INTERVALS.

*PARAMETERS: SENDER LIKE SOUD-USRNAM.

SELECTION-SCREEN END OF BLOCK B2.

SELECTION-SCREEN BEGIN OF BLOCK B3 WITH FRAME TITLE TEXT-009.

PARAMETERS: SUB_LINE(60) TYPE C.

SELECTION-SCREEN END OF BLOCK B3.

SELECTION-SCREEN BEGIN OF BLOCK B4 WITH FRAME TITLE TEXT-008.

SELECT-OPTIONS: TEXT1 FOR SOLI-LINE NO INTERVALS.

SELECTION-SCREEN END OF BLOCK B4.

SELECTION-SCREEN BEGIN OF BLOCK B5 WITH FRAME TITLE TEXT-009.

SELECTION-SCREEN BEGIN OF LINE.

parameters: P_ATTACH as checkbox.

selection-screen comment 3(30) text-010.

SELECTION-SCREEN END OF LINE.

SELECTION-SCREEN END OF BLOCK B5.

DATA: MAIL_CONTENT LIKE SOLI OCCURS 0 WITH HEADER LINE,

SUBJECT_LINE LIKE SOOD1-OBJDES.

************************************************************************

    • Start of program processing

************************************************************************

START-OF-SELECTION.

    • Get the Body of the Message from the selection screen or from

    • calling program

LOOP AT TEXT1.

MOVE TEXT1-LOW TO MAIL_CONTENT-LINE.

APPEND MAIL_CONTENT.

ENDLOOP.

    • Subject of the Message

MOVE SUB_LINE TO SUBJECT_LINE.

    • call a routine to send the workflow message

PERFORM SEND_EMAIL

TABLES MAIL_CONTENT

USING SUBJECT_LINE.

&----


*& Form SEND_EMAIL

&----


  • Send Workflow message

----


FORM SEND_EMAIL TABLES OBJCONT STRUCTURE MAIL_CONTENT

USING TITLE LIKE SOOD-OBJDES.

DATA: RECEIVERS LIKE SOOS1 OCCURS 0 WITH HEADER LINE,

TSOOD1 LIKE SOOD1,

PACKING_LIST LIKE SOXPL OCCURS 0 WITH HEADER LINE,

OBJCONT1 LIKE MAIL_CONTENT OCCURS 0 WITH HEADER LINE.

DATA: BEGIN OF AT_HEADER OCCURS 1.

INCLUDE STRUCTURE SOLI.

DATA: END OF AT_HEADER.

CLEAR: TSOOD1,

RECEIVERS.

REFRESH RECEIVERS.

MOVE: SY-LANGU TO TSOOD1-OBJLA,

'Email Notice' TO TSOOD1-OBJNAM,

'C' TO TSOOD1-OBJSNS,

TITLE TO TSOOD1-OBJDES.

  • 'SCHIAVONIR' TO TSOOD1-OWNNAM.

    • loop through each ID and move them to recipient table

LOOP AT ID.

TRANSLATE ID-LOW TO UPPER CASE.

IF SAPID = 'X'.

MOVE: SY-DATUM TO RECEIVERS-RCDAT,

SY-UZEIT TO RECEIVERS-RCTIM,

' ' TO RECEIVERS-RECESC,

ID-LOW TO RECEIVERS-RECNAM,

'X' TO RECEIVERS-SNDEX.

ELSE.

MOVE: SY-DATUM TO RECEIVERS-RCDAT,

SY-UZEIT TO RECEIVERS-RCTIM,

'U' TO RECEIVERS-RECESC,

'U-' TO RECEIVERS-RECNAM,

ID-LOW TO RECEIVERS-RECEXTNAM.

ENDIF.

APPEND RECEIVERS.

CLEAR RECEIVERS.

ENDLOOP.

    • loop through each CC and move them to recipient table

LOOP AT CC.

TRANSLATE CC-LOW TO UPPER CASE.

IF SAPID = 'X'.

MOVE: SY-DATUM TO RECEIVERS-RCDAT,

SY-UZEIT TO RECEIVERS-RCTIM,

' ' TO RECEIVERS-RECESC,

CC-LOW TO RECEIVERS-RECNAM,

'X' TO RECEIVERS-SNDEX,

'X' TO RECEIVERS-SNDCP.

ELSE.

MOVE: SY-DATUM TO RECEIVERS-RCDAT,

SY-UZEIT TO RECEIVERS-RCTIM,

'U' TO RECEIVERS-RECESC,

'U-' TO RECEIVERS-RECNAM,

CC-LOW TO RECEIVERS-RECEXTNAM,

'X' TO RECEIVERS-SNDCP.

ENDIF.

APPEND RECEIVERS.

CLEAR RECEIVERS.

ENDLOOP.

    • loop through each BCC and move them to recipient table

LOOP AT BCC.

TRANSLATE BCC-LOW TO UPPER CASE.

IF SAPID = 'X'.

MOVE: SY-DATUM TO RECEIVERS-RCDAT,

SY-UZEIT TO RECEIVERS-RCTIM,

' ' TO RECEIVERS-RECESC,

BCC-LOW TO RECEIVERS-RECNAM,

'X' TO RECEIVERS-SNDEX,

'X' TO RECEIVERS-SNDBC.

ELSE.

MOVE: SY-DATUM TO RECEIVERS-RCDAT,

SY-UZEIT TO RECEIVERS-RCTIM,

'U' TO RECEIVERS-RECESC,

'U-' TO RECEIVERS-RECNAM,

BCC-LOW TO RECEIVERS-RECEXTNAM,

'X' TO RECEIVERS-SNDBC.

ENDIF.

APPEND RECEIVERS.

CLEAR RECEIVERS.

ENDLOOP.

AT_HEADER = SY-DATUM.

APPEND AT_HEADER.

AT_HEADER = SY-UZEIT.

APPEND AT_HEADER.

  • IF SENDER EQ SPACE.

  • SENDER = SY-UNAME.

  • ENDIF.

IF P_ATTACH EQ 'X'.

PACKING_LIST-HEAD_START = 1.

PACKING_LIST-HEAD_NUM = 2.

PACKING_LIST-BODY_START = 1.

PACKING_LIST-BODY_NUM = 9999.

PACKING_LIST-FILE_EXT = 'TXT'.

APPEND PACKING_LIST.

CLEAR PACKING_LIST.

APPEND LINES OF OBJCONT TO OBJCONT1.

REFRESH OBJCONT.

ENDIF.

CALL FUNCTION 'SO_OBJECT_SEND'

EXPORTING

OBJECT_HD_CHANGE = TSOOD1

OBJECT_TYPE = 'RAW'

TABLES

OBJCONT = OBJCONT

RECEIVERS = RECEIVERS

ATT_HEAD = AT_HEADER

ATT_CONT = OBJCONT1

PACKING_LIST = PACKING_LIST

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.

ENDFORM. " SEND_EMAIL

Reward Points if it is helpful

Thanks

Seshu

Former Member
0 Kudos

Hi,

Here is the detail code of sending email with an attachment of excel sheet . Please note it depends upon the type of email address you have maintained for sending it to SAP INBOX, in case of SAP Inbox, you should have email as internal email address.

Please award points in case you feel the code if of any importance to you..

Thanks

  • Structure to hold email address

BEGIN OF ty_email,

zemail TYPE AD_SMTPADR, "email address

END OF ty_email.

  • Table to hold email address

DATA: t_email TYPE STANDARD TABLE OF ty_email INITIAL SIZE 0,

*Work Area

w_email TYPE ty_email.

  • Populate email receiver table to send the Error log report

PERFORM sub_get_email.

&----


*& Form sub_get_email

&----


  • Retrieve the email addresses to send the Error Log report

----


FORM sub_get_email .

  • Retrieve the email addresses to send the Error Log report

SELECT zemail FROM zcap_email_addr INTO TABLE t_email

WHERE zdevid = 'IDD0468'.

ENDFORM. " sub_get_email

  • To email the report

PERFORM sub_send_email TABLES t_error_first.

&----


*& Form sub_send_email

&----


  • Routine to send e-mail

----


  • -->P_T_ERROR_FIRST text

----


FORM sub_send_email TABLES p_error_log TYPE STANDARD TABLE.

  • Data Declaration:

DATA:

  • Internal Tables:

  • To store the receivers details for email

lt_receivers TYPE STANDARD TABLE OF somlreci1

INITIAL SIZE 0,

  • To store information to be sent as the e-mail body.

lt_body TYPE STANDARD TABLE OF solisti1

INITIAL SIZE 0,

  • To store the attachment records

lt_attachmnt TYPE STANDARD TABLE OF solisti1

INITIAL SIZE 0,

  • Constants:

  • Attachment format

c_att_ext(3) TYPE c VALUE 'XLS',

  • Local variables:

  • To store sy-subrc value

l_sysubrc TYPE sy-subrc,

  • Attachment description(name)

l_file_desc(50) TYPE c,

  • Subject of the email

l_email_sub(50) TYPE c.

  • Populate email subject

l_email_sub = 'Error Log report for IDD0468'(050).

  • Populate attachment name

l_file_desc = 'Error Log Report'(051).

  • Populate message body text with appropriate text

PERFORM populate_email_message_body TABLES lt_body.

  • Populate recipient table

PERFORM populate_email_receivers TABLES lt_receivers.

  • Convert internal table record in XLS format.

************************************************************************

  • Here we have sent the record stored in an internal table as an

  • excel attachment for that we need to do some modification in the

  • records to convert in excel format. For different attachment format

  • different modifications will be needed. The general procedure is

  • to store records in appropriate format in table lt_attachmnt and pass

  • it to the under mentioned subroutine.

************************************************************************

PERFORM sub_process_it_records TABLES p_error_log

CHANGING lt_attachmnt.

  • Send file by email as .xls spreadsheet

PERFORM sub_send_file_as_email_atcmnt

TABLES lt_body

lt_attachmnt

lt_receivers

USING l_email_sub

c_att_ext " 'XLS' etc.

l_file_desc

CHANGING l_sysubrc.

  • Check if email is ready for sending

IF l_sysubrc <> 0.

  • Display information message

MESSAGE s108. "Sending of email failed

ENDIF.

ENDFORM. " sub_send_email

&----


*& Form populate_email_message_body

&----


  • Populating the body of email *

----


  • P_LT_BODY: Structure of body

----


FORM populate_email_message_body

TABLES p_t_body STRUCTURE solisti1. "#EC *

DATA lw_body TYPE solisti1.

lw_body-line = 'This is the Error Log file for Object ID IDD0468'(052).

APPEND lw_body TO p_t_body.

lw_body-line = 'Please Refer the attachment below.'(053).

APPEND lw_body TO p_t_body.

ENDFORM. " populate_email_message_body

&----


*& Form populate_email_receivers

&----


  • Populating the receiver email address *

----


  • -->P_LT_RECEIVERS: email address of receiver *

----


FORM populate_email_receivers TABLES p_t_receivers STRUCTURE

somlreci1.

  • populate receiver type

p_t_receivers-rec_type = 'U'. " ( Internet ) / 'B' (SAP Inbox)

  • populate communication type

p_t_receivers-com_type = 'INT'. " (Internet)

  • mail delivered notification needed?

p_t_receivers-notif_del = 'X'.

  • mail not delivered notification needed?

p_t_receivers-notif_ndel = 'X'.

  • Express delivery needed?

p_t_receivers-express = 'X'.

LOOP AT t_email INTO w_email.

  • Insert each receiver from select option table into receivers table

p_t_receivers-receiver = w_email-zemail. " email address to send

APPEND p_t_receivers.

CLEAR p_t_receivers-receiver.

ENDLOOP.

ENDFORM. " populate_email_receivers

&----


*& Form sub_process_it_records

&----


  • Creating attachment format *

----


  • P_ERROR_LOG: Error log to be sent *

  • P_T_ATTACHMNT: Attachment format being created *

----


FORM sub_process_it_records TABLES p_error_log TYPE STANDARD TABLE

CHANGING p_t_attachmnt TYPE soli_tab.

DATA:

  • Internal table to hold the tab delimited main records

  • to be sent as excel attachment

l_t_line TYPE STANDARD TABLE OF ty_line INITIAL SIZE 0,

  • Work area to hold the line data

lw_line TYPE ty_line,

l_str TYPE string,

c_var_tab TYPE c

VALUE cl_abap_char_utilities=>cr_lf .

  • Get tab-delimited records row wise

PERFORM sub_get_delimiter_data TABLES p_error_log

l_t_line.

  • Get vertical tab delimited record in one string so that the records

  • will be displayed in row wise

LOOP AT l_t_line INTO lw_line.

CONCATENATE l_str lw_line c_var_tab INTO l_str.

CLEAR lw_line.

ENDLOOP.

  • Split the string at 255 character and append into attachment table

REFRESH p_t_attachmnt.

CALL FUNCTION 'SCMS_STRING_TO_FTEXT'

EXPORTING

text = l_str

TABLES

ftext_tab = p_t_attachmnt.

ENDFORM. " sub_process_it_records

&----


*& Form sub_get_delimiter_data

&----


  • Setting delimiter for the attachment file *

----


  • P_ERROR_LOG: Input error log file to be attached *

  • P_T_LINE: File with delimiter *

----


FORM sub_get_delimiter_data TABLES p_error_log TYPE STANDARD TABLE

p_t_line TYPE STANDARD TABLE."#EC *

FIELD-SYMBOLS: <f>.

DATA: l_len TYPE i,

l_num TYPE i,

l_len_length TYPE string,

l_delimit_len TYPE i,

l_for_len TYPE i,

lw_line TYPE ty_line.

  • get the length of the c_con_tab

l_delimit_len = STRLEN( c_con_tab ).

  • here p_main_tab contains records to be sent as Excel attachment.

LOOP AT p_error_log.

CLEAR lw_line.

l_num = 0.

l_for_len = 0.

DO.

  • Get structure field into <f>

ASSIGN COMPONENT sy-index OF STRUCTURE p_error_log TO <f>.

IF sy-subrc <> 0.

EXIT.

ENDIF. " No more fields in structure

  • get the length of the field

l_len = STRLEN( <f> ).

  • Get the no of digit of the field length

l_len_length = l_len.

************************************************************************

  • A major problem in excel spreadsheet is that it automatically removes

  • the leading zeroes. But for most of the records we need the leading

  • zeros. To keep leading zero there is an excel command = REPLACE(“old

  • text”,1,<text length>,”New text”). We have used this command to keep

  • the leading zeroes. Here we concatenate c_for1,<f>,c_for2, l_len,<f>,

  • c_for4,c_con_tab so that *each record <f> will be placed in a new

  • cell in the excel sheet with the above mentioned *command and

  • will keep the leading zeros. The code is of generic kind and can be

  • used for any internal *table (t_output_xls)

************************************************************************

  • for example let <f> = 000011234 so l_len = 9

  • write ' =REPLACE(" '

l_for_len = STRLEN( c_for1 ).

WRITE: c_for1 TO lw_line-line+l_num(l_for_len).

l_num = l_num + l_for_len.

  • write ' 000011234 '

WRITE: <f> TO lw_line-line+l_num(l_len).

l_num = l_num + l_len.

  • write ‘ ”,1, ’

l_for_len = STRLEN( c_for2 ).

WRITE: c_for2 TO lw_line-line+l_num(l_for_len).

l_num = l_num + l_for_len.

  • write ‘ 9 ’

l_for_len = STRLEN( l_len_length ).

WRITE: l_len TO lw_line-line+l_num(l_for_len).

l_num = l_num + l_for_len.

  • write ’ ,” ’

l_for_len = STRLEN( c_for3 ).

WRITE: c_for3 TO lw_line-line+l_num(l_for_len).

l_num = l_num + l_for_len.

  • write ‘ 000011234 ’

WRITE: <f> TO lw_line-line+l_num(l_len).

l_num = l_num + l_len.

  • write ‘”)’

l_for_len = STRLEN( c_for4 ).

WRITE: c_for4 TO lw_line-line+l_num(l_for_len).

l_num = l_num + l_for_len.

  • write ‘ # ‘

WRITE: c_con_tab TO lw_line-line+l_num(l_delimit_len).

l_num = l_num + l_delimit_len.

  • now lw_line = ‘ = REPLACE(“000011234”,1,9,”000011234”)# ‘

ENDDO.

APPEND lw_line TO p_t_line.

ENDLOOP.

ENDFORM. " sub_get_delimiter_data

&----


*& Form sub_send_file_as_email_atcmnt

&----


  • Attachnign attachment to email and send *

----


  • P_T_BODY: Body of email *

  • P_T_ATTACHMNT: Attachment of email *

  • P_T_RECEIVERS: Receiver email address *

  • P_EMAIL_SUB: Subject of email *

  • P_C_ATT_EXT: Extension of attachment *

  • P_FILE_DESC: Attachment description *

  • P_SYSUBRC: Success value *

----


FORM sub_send_file_as_email_atcmnt TABLES p_t_body STRUCTURE solisti1

p_t_attachmnt STRUCTURE

solisti1

p_t_receivers STRUCTURE

somlreci1

USING p_title TYPE any

p_format TYPE any

p_attdescription TYPE any

CHANGING p_sysubrc TYPE sysubrc.

DATA:

l_t_packing_list TYPE STANDARD TABLE OF sopcklsti1

INITIAL SIZE 0,

lw_packing_list TYPE sopcklsti1,

lw_doc_data TYPE sodocchgi1,

lw_body TYPE solisti1,

lw_attachmnt TYPE solisti1,

l_sent TYPE so_text001, "#EC NEEDED

l_tablines TYPE i.

************************************************************************

  • To send the mail we require three steps.

  • 1. Populate document information

  • 2. Populate packing list with document and attachment

  • information

  • 3. Send email with function module

************************************************************************

  • Populate document information

  • This structure must contain the attributes of the document to be sent

DESCRIBE TABLE p_t_body LINES l_tablines.

READ TABLE p_t_body INTO lw_body INDEX l_tablines.

  • Size of the documents in bytes. The size of each document should be

  • entered here for PC documents, and the size of RAW and SCR

  • documents is calculated by "length of the last line" + "number of

  • remaining lines” multiplied by 255".

lw_doc_data-doc_size = ( l_tablines - 1 ) * 255 + STRLEN( lw_body ).

  • Document language

lw_doc_data-obj_langu = sy-langu.

  • Name of the document

lw_doc_data-obj_name = 'Email of ERROR LOG FOR Invalid Data for Payment amount interface'(054).

  • Title (short description) of the document

lw_doc_data-obj_descr = p_title .

  • Document sensitivity.

  • ’A’ : Private document can have the following sensitivity:

  • 'O' : Standard, normal sensitivity

  • 'F' : Functional, can be forwarded functionally

  • 'P' : Confidential, cannot be displayed by substitutes

  • Documents in shared folders can only have the sensitivity 'O'

lw_doc_data-sensitivty = 'F'.

  • Populate packing list

************************************************************************

  • This table requires information on how the data from tables

  • OBJECT_HEADER, CONTENTS_BIN and CONTENTS_TXT should be distributed to

  • the document and its attachments. The first line stands for the

  • document and each of the following lines for one attachment. In the

  • present case we have only one attachment stored in p_t_attachmnt

  • (CONTENTS_BIN) so there will be two lines in l_t_packing_list , one

  • for the document and other for attachment. See details below

************************************************************************

  • Populate Document information for packing list

  • If transf_bin is set ('X'), the table entry indicates an object stored

  • in binary format. The *corresponding *object contents are located in

  • table CONTENTS_BIN. If the flag is not set, *the object contents are

  • located in table CONTENTS_TXT in ASCII format.

lw_packing_list-transf_bin = ' '.

  • Requires the starting line of table OBJECT_HEADER, starting from which

  • the specific header

  • data of the object is stored

lw_packing_list-head_start = 1.

  • Requires the number of lines in table OBJECT_HEADER which are used for

  • the specific header

  • data of the object. The lines involved are arranged in a block and

  • uniquely defined *together with

  • the specification in HEAD_START

lw_packing_list-head_num = 0.

  • Requires the starting lines of the tables dependent on the flag

  • TRANSF_BIN, starting from *which

  • the object contents are stored

lw_packing_list-body_start = 1.

  • Requires the number of lines of the table dependent on flag TRANSF_BIN

  • which are used to store the object contents. The lines involved are

  • arranged in a block and uniquely defined together with the

  • specification in HEAD_START

lw_packing_list-body_num = l_tablines.

  • Attachment type. This field is not used in the first line of the table

lw_packing_list-doc_type = 'RAW'.

APPEND lw_packing_list TO l_t_packing_list.

CLEAR: lw_packing_list,

l_tablines.

  • Similarly populate attachment information for packing list

DESCRIBE TABLE p_t_attachmnt LINES l_tablines.

READ TABLE p_t_attachmnt INTO lw_attachmnt INDEX l_tablines.

lw_packing_list-transf_bin = 'X'.

lw_packing_list-head_start = 1.

lw_packing_list-head_num = 0.

lw_packing_list-body_start = 1.

lw_packing_list-body_num = l_tablines.

lw_packing_list-doc_type = p_format.

lw_packing_list-obj_descr = p_attdescription.

lw_packing_list-obj_name = 'ATTACHMNT'. "Name of the Object

lw_packing_list-doc_size = ( l_tablines - 1 ) * 255 + STRLEN(

lw_attachmnt ).

APPEND lw_packing_list TO l_t_packing_list.

  • Send email

CALL FUNCTION 'SO_NEW_DOCUMENT_ATT_SEND_API1'

EXPORTING

document_data = lw_doc_data

put_in_outbox = 'X' "save the mail to outbox

commit_work = 'X' "Commit work

IMPORTING

sent_to_all = l_sent

TABLES

packing_list = l_t_packing_list

contents_bin = p_t_attachmnt

contents_txt = p_t_body

receivers = p_t_receivers

EXCEPTIONS

too_many_receivers = 1 "will return X if mail sent to all

document_not_sent = 2

document_type_not_exist = 3

operation_no_authorization = 4

parameter_error = 5

x_error = 6

enqueue_error = 7

OTHERS = 8.

  • Return sy-subrc value

p_sysubrc = sy-subrc.

ENDFORM. " sub_send_file_as_email_atcmnt

former_member189631
Active Contributor
0 Kudos

Thank you !!!