Application Development and Automation 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: 
Read only

emailing from a program

Former Member
0 Likes
800

Hi Guys,

I need to email the contents of a string internal table. Can someone please let me know how this can be achived.

Thanks

Lilan

7 REPLIES 7
Read only

Former Member
0 Likes
772

See the below code :

Text elements :

BCC Blind CC

CC Copy to

EMAIL_ID Email ID

ID ?...

P_ATTACH Send as Attachment?

SAPID SAP ID

SUB_LINE Subject

TEXT1 Message

________ ______________________________

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

    • 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 useful

Thanks

Seshu

Read only

Former Member
0 Likes
772

hi,

use the fnction module <b>SO_OBJECT_SEND</b>

With Regards,

S.BArani

Read only

Former Member
0 Likes
772

Hi Lilan,

Check this link, It will help you.

https://forums.sdn.sap.com/click.jspa?searchID=2803853&messageID=2020983

Thanks.

Read only

Former Member
0 Likes
772

Hi,

Check the following link:

http://www.sap-img.com/abap/sending-email-with-attachment.htm

Regards,

Bhaskar

Read only

0 Likes
772

there is no attachment to be sent. I just need to add some text to the body of the message.

Read only

Former Member
0 Likes
772

Hi Lilan,

Check this program...

PARAMETERS: psubject(40) type c default 'Hello',

p_email(40) type c default 'test@yahoo.com' .

data: it_packing_list like sopcklsti1 occurs 0 with header line,

it_contents like solisti1 occurs 0 with header line,

it_receivers like somlreci1 occurs 0 with header line,

it_attachment like solisti1 occurs 0 with header line,

gd_cnt type i,

gd_sent_all(1) type c,

gd_doc_data like sodocchgi1,

gd_error type sy-subrc.

data: it_message type standard table of SOLISTI1 initial size 0

with header line.

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

*START-OF-SELECTION.

START-OF-SELECTION.

Perform populate_message_table.

*Send email message, although is not sent from SAP until mail send

*program has been executed(rsconn01)

PERFORM send_email_message.

*Instructs mail send program for SAPCONNECT to send email(rsconn01)

perform initiate_mail_execute_program.

&----


*& Form POPULATE_MESSAGE_TABLE

&----


  • Adds text to email text table

----


form populate_message_table.

Append 'Email line 1' to it_message.

Append 'Email line 2' to it_message.

Append 'Email line 3' to it_message.

Append 'Email line 4' to it_message.

endform. " POPULATE_MESSAGE_TABLE

&----


*& Form SEND_EMAIL_MESSAGE

&----


  • Send email message

----


form send_email_message.

  • Fill the document data.

gd_doc_data-doc_size = 1.

  • Populate the subject/generic message attributes

gd_doc_data-obj_langu = sy-langu.

gd_doc_data-obj_name = 'SAPRPT'.

gd_doc_data-obj_descr = psubject.

gd_doc_data-sensitivty = 'F'.

  • Describe the body of the message

clear it_packing_list.

refresh it_packing_list.

it_packing_list-transf_bin = space.

it_packing_list-head_start = 1.

it_packing_list-head_num = 0.

it_packing_list-body_start = 1.

describe table it_message lines it_packing_list-body_num.

it_packing_list-doc_type = 'RAW'.

append it_packing_list.

  • Add the recipients email address

clear it_receivers.

refresh it_receivers.

it_receivers-receiver = p_email.

it_receivers-rec_type = 'U'.

it_receivers-com_type = 'INT'.

it_receivers-notif_del = 'X'.

it_receivers-notif_ndel = 'X'.

append it_receivers.

  • Call the FM to post the message to SAPMAIL

call function 'SO_NEW_DOCUMENT_ATT_SEND_API1'

exporting

document_data = gd_doc_data

put_in_outbox = 'X'

importing

sent_to_all = gd_sent_all

tables

packing_list = it_packing_list

contents_txt = it_message

receivers = it_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.

  • Store function module return code

gd_error = sy-subrc.

  • Get it_receivers return code

loop at it_receivers.

endloop.

endform. " SEND_EMAIL_MESSAGE

&----


*& Form INITIATE_MAIL_EXECUTE_PROGRAM

&----


  • Instructs mail send program for SAPCONNECT to send email.

----


form initiate_mail_execute_program.

wait up to 2 seconds.

if gd_error eq 0.

submit rsconn01 with mode = 'INT'

with output = 'X'

and return.

endif.

endform. " INITIATE_MAIL_EXECUTE_PROGRAM

Check this Link for more….

https://forums.sdn.sap.com/click.jspa?searchID=2797505&messageID=2347687

http://www.sapdevelopment.co.uk/reporting/email/email_sapmail.htm

https://forums.sdn.sap.com/click.jspa?searchID=2797505&messageID=1890889

Regards,

Younus

<b>Reward Helpful Answers:-)</b>

Read only

Former Member
0 Likes
772


REPORT  ZMBJ_EMAIL_ATTACH.

TABLES: ekko.

PARAMETERS: p_email TYPE somlreci1-receiver DEFAULT 'jogdand.machindra@arteriatech.com'.


TYPES: BEGIN OF t_ekpo,
ebeln TYPE ekpo-ebeln,
ebelp TYPE ekpo-ebelp,
aedat TYPE ekpo-aedat,
matnr TYPE ekpo-matnr,
END OF t_ekpo.
DATA: it_ekpo TYPE STANDARD TABLE OF t_ekpo INITIAL SIZE 0,
wa_ekpo TYPE t_ekpo.

TYPES: BEGIN OF t_charekpo,
ebeln(10) TYPE c,
ebelp(5) TYPE c,
aedat(8) TYPE c,
matnr(18) TYPE c,
END OF t_charekpo.
DATA: wa_charekpo TYPE t_charekpo.

DATA: it_message TYPE STANDARD TABLE OF solisti1 INITIAL SIZE 0
WITH HEADER LINE.
DATA: it_attach TYPE STANDARD TABLE OF solisti1 INITIAL SIZE 0
WITH HEADER LINE.

DATA: t_packing_list LIKE sopcklsti1 OCCURS 0 WITH HEADER LINE,
t_contents LIKE solisti1 OCCURS 0 WITH HEADER LINE,
t_receivers LIKE somlreci1 OCCURS 0 WITH HEADER LINE,
t_attachment LIKE solisti1 OCCURS 0 WITH HEADER LINE,
t_object_header LIKE solisti1 OCCURS 0 WITH HEADER LINE,

w_cnt TYPE i,
w_sent_all(1) TYPE c,
w_doc_data LIKE sodocchgi1,
gd_error TYPE sy-subrc,
gd_reciever TYPE sy-subrc.
t_object_header = 'Text.xls'. APPEND t_object_header.

*START_OF_SELECTION
START-OF-SELECTION.
* Retrieve sample data from table ekpo
PERFORM data_retrieval.

* Populate table with detaisl to be entered into .xls file
PERFORM build_xls_data_table.


************************************************************************
*END-OF-SELECTION
END-OF-SELECTION.
* Populate message body text
perform populate_email_message_body.

* Send file by email as .xls speadsheet
PERFORM send_file_as_email_attachment
tables it_message
it_attach
using p_email
'Example .xls documnet attachment'
'XLS'
'filename'
' '
' '
' '
changing gd_error
gd_reciever.

* Instructs mail send program for SAPCONNECT to send email(rsconn01)
PERFORM initiate_mail_execute_program.


*&---------------------------------------------------------------------*
*& Form DATA_RETRIEVAL
*&---------------------------------------------------------------------*
* Retrieve data form EKPO table and populate itab it_ekko
*----------------------------------------------------------------------*
FORM data_retrieval.
SELECT ebeln ebelp aedat matnr
UP TO 10 ROWS
FROM ekpo
INTO TABLE it_ekpo.
ENDFORM. " DATA_RETRIEVAL


*&---------------------------------------------------------------------*
*& Form BUILD_XLS_DATA_TABLE
*&---------------------------------------------------------------------*
* Build data table for .xls document
*----------------------------------------------------------------------*
FORM build_xls_data_table.
*CONSTANTS: con_cret TYPE x VALUE '0D', "OK for non Unicode
*con_tab TYPE x VALUE '09'. "OK for non Unicode

*If you have Unicode check active in program attributes thnen you will
*need to declare constants as follows
*class cl_abap_char_utilities definition load.
constants:
 con_tab type c value cl_abap_char_utilities=>HORIZONTAL_TAB,
 con_cret type c value cl_abap_char_utilities=>CR_LF.


CONCATENATE 'EBELN' 'EBELP' 'AEDAT' 'MATNR'
INTO it_attach SEPARATED BY con_tab.
CONCATENATE con_cret it_attach INTO it_attach.
APPEND it_attach.

LOOP AT it_ekpo INTO wa_charekpo.
CONCATENATE wa_charekpo-ebeln wa_charekpo-ebelp
wa_charekpo-aedat wa_charekpo-matnr
INTO it_attach SEPARATED BY con_tab.
CONCATENATE con_cret it_attach INTO it_attach.
APPEND it_attach.
ENDLOOP.
ENDFORM. " BUILD_XLS_DATA_TABLE


*&---------------------------------------------------------------------*
*& Form SEND_FILE_AS_EMAIL_ATTACHMENT
*&---------------------------------------------------------------------*
* Send email
*----------------------------------------------------------------------*
FORM send_file_as_email_attachment tables pit_message
pit_attach
using p_email
p_mtitle
p_format
p_filename
p_attdescription
p_sender_address
p_sender_addres_type
changing p_error
p_reciever.


DATA: ld_error TYPE sy-subrc,
ld_reciever TYPE sy-subrc,
ld_mtitle LIKE sodocchgi1-obj_descr,
ld_email LIKE somlreci1-receiver,
ld_format TYPE so_obj_tp ,
ld_attdescription TYPE so_obj_nam ,
ld_attfilename TYPE so_obj_des ,
ld_sender_address LIKE soextreci1-receiver,
ld_sender_address_type LIKE soextreci1-adr_typ,
ld_receiver LIKE sy-subrc.

ld_email = p_email.
ld_mtitle = p_mtitle.
ld_format = p_format.
ld_attdescription = p_attdescription.
ld_attfilename = p_filename.
ld_sender_address = p_sender_address.
ld_sender_address_type = p_sender_addres_type.


* Fill the document data.
w_doc_data-doc_size = 1.

* Populate the subject/generic message attributes
w_doc_data-obj_langu = sy-langu.
w_doc_data-obj_name = 'SAPRPT'.
w_doc_data-obj_descr = ld_mtitle .
w_doc_data-sensitivty = 'F'.

* Fill the document data and get size of attachment
CLEAR w_doc_data.
READ TABLE it_attach INDEX w_cnt.
w_doc_data-doc_size =
( w_cnt - 1 ) * 255 + STRLEN( it_attach ).
w_doc_data-obj_langu = sy-langu.
w_doc_data-obj_name = 'SAPRPT'.
w_doc_data-obj_descr = ld_mtitle.
w_doc_data-sensitivty = 'F'.
CLEAR t_attachment.
REFRESH t_attachment.
t_attachment[] = pit_attach[].

* Describe the body of the message
CLEAR t_packing_list.
REFRESH t_packing_list.
t_packing_list-transf_bin = space.
t_packing_list-head_start = 1.
t_packing_list-head_num = 0.
t_packing_list-body_start = 1.
DESCRIBE TABLE it_message LINES t_packing_list-body_num.
t_packing_list-doc_type = 'RAW'.
APPEND t_packing_list.

* Create attachment notification
t_packing_list-transf_bin = 'X'.
t_packing_list-head_start = 1.
t_packing_list-head_num = 1.
t_packing_list-body_start = 1.

DESCRIBE TABLE t_attachment LINES t_packing_list-body_num.
t_packing_list-doc_type = ld_format.
t_packing_list-obj_descr = ld_attdescription.
t_packing_list-obj_name = ld_attfilename.
t_packing_list-doc_size = t_packing_list-body_num * 255.
APPEND t_packing_list.

* Add the recipients email address
CLEAR t_receivers.
REFRESH t_receivers.
t_receivers-receiver = ld_email.
t_receivers-rec_type = 'U'.
t_receivers-com_type = 'INT'.
t_receivers-notif_del = 'X'.
t_receivers-notif_ndel = 'X'.
APPEND t_receivers.

CALL FUNCTION 'SO_DOCUMENT_SEND_API1'
EXPORTING
document_data = w_doc_data
put_in_outbox = 'X'
sender_address = ld_sender_address
sender_address_type = ld_sender_address_type
commit_work = 'X'
IMPORTING
sent_to_all = w_sent_all
TABLES
object_header = t_object_header
packing_list = t_packing_list
contents_bin = t_attachment
contents_txt = it_message
receivers = t_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.

* Populate zerror return code
ld_error = sy-subrc.

* Populate zreceiver return code
LOOP AT t_receivers.
ld_receiver = t_receivers-retrn_code.
ENDLOOP.
ENDFORM.


*&---------------------------------------------------------------------*
*& Form INITIATE_MAIL_EXECUTE_PROGRAM
*&---------------------------------------------------------------------*
* Instructs mail send program for SAPCONNECT to send email.
*----------------------------------------------------------------------*
FORM initiate_mail_execute_program.
WAIT UP TO 2 SECONDS.
SUBMIT rsconn01 WITH mode = 'INT'
WITH output = 'X'
AND RETURN.
ENDFORM. " INITIATE_MAIL_EXECUTE_PROGRAM


*&---------------------------------------------------------------------*
*& Form POPULATE_EMAIL_MESSAGE_BODY
*&---------------------------------------------------------------------*
* Populate message body text
*----------------------------------------------------------------------*
form populate_email_message_body.
REFRESH it_message.
it_message = 'Please find attached a list test ekpo records'.
APPEND it_message.
endform. " POPULATE_EMAIL_MESSAGE_BODY


u may need to add receipient address in SCOT tcode.

its working for me./

Jogdand M B

Message was edited by:

Jogdand M B