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 Mail

Former Member
0 Kudos

Hi gurus,

Can any one tell me how to send a mail through program? What is the procedure for that and which FM i ve to use?

Thanks in Advance

5 REPLIES 5

Former Member
0 Kudos

Hi Ravi,

You can use FM SO_NEW_DOCUMENT_ATT_SEND_API1 to send the mail. The FM has a well documented help. You can go through that for details. Let me know if you require any further inputs.

Do not forget to give the reward points if the answer helps!

Regards

Anurag

Former Member
0 Kudos

Hello Ravi,

Check this report


Example original file
12345 aa pq
Email attachment file
-> This line
12345 aa pq

This is my code:

REPORT ZTEST_FILE .
DATA:BEGIN OF t_upload occurs 0,
matnr LIKE zwplcsmev-matnr,
zwgehrrg LIKE zwplcsmev-zwgehrrg,
zwgehrct LIKE zwplcsmev-zwgehrct,
zwgbev LIKE zwplcsmev-zwgbev,
zwpldt LIKE zwplcsmev-zwpldt,
zwacdt LIKE zwplcsmev-zwacdt,
END OF t_upload.
*DATA: maildata type sodocchgi1.
*DATA: mailtxt type table of solisti1 with header line.
*DATA: mailrec type table of somlrec90 with header line.
DATA: it_message TYPE STANDARD TABLE OF solisti1 INITIAL SIZE 0
WITH HEADER LINE.
DATA:REC(80) TYPE C.
DATA g_mask(20) TYPE c VALUE ',*.*, *.*.'.

DATA: it_attach TYPE STANDARD TABLE OF solisti1 INITIAL SIZE 0
WITH HEADER LINE.
DATA:P_ERROR(3).
DATA:P_REFO(3).
DATA: gd_error TYPE sy-subrc,
gd_reciever TYPE sy-subrc.
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.
selection-screen begin of block b1 with frame title text-001.
parameters:p_file type localfile.
parameter:p_email type ad_smtpadr.
selection-screen end of block b1.
*----------------------------------------------------------------------*
*---------------At Selection-Screen--- --------------------------------*
*----------------------------------------------------------------------*
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.

CALL FUNCTION 'TB_LIMIT_WS_FILENAME_GET'
EXPORTING
def_filename = p_file
mask = g_mask
* mode = 'S'
title = 'INPUT FILE'
IMPORTING
filename = p_file
EXCEPTIONS
selection_cancel = 1
selection_error = 2
OTHERS = 3.
IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.

START-OF-SELECTION.
PERFORM process_file.
if p_error = 'X' AND P_REFO <> 'X'.

PERFORM populate_message_body.
ENDIF.
IF P_REFO = 'X' AND P_ERROR <> 'X'.
it_message = 'Please find Attached file'.
APPEND it_message.

PERFORM send_attachment tables it_message
it_attach
using p_email
'Crest to Plc Data'
'TXT'
p_file
' '
' '
' '
changing gd_error
gd_reciever.
ENDIF.
* Instructs mail send program for SAPCONNECT to send email(rsconn01)
* PERFORM initiate_mail_execute_program.



END-OF-SELECTION.

*&--------------------------------------------------------------------*
*& Form process_file
*&--------------------------------------------------------------------*
* text
*---------------------------------------------------------------------*
FORM process_file.
*CONSTANTS: con_cret TYPE x VALUE '0D', "OK for non Unicode
* con_tab TYPE x VALUE '09'. "OK for non Unicode
DATA :l_path TYPE string.
l_path = p_file.

CONSTANTS: con_tab type c value cl_abap_char_utilities=>HORIZONTAL_TAB
,
con_cret type c value cl_abap_char_utilities=>newline.

OPEN DATASET P_FILE FOR INPUT IN TEXT MODE encoding default.
if sy-subrc = 0.
do.
read dataset p_file into IT_ATTACH.
IF SY-SUBRC NE 0 .
EXIT.
ELSE.
T_UPLOAD = IT_ATTACH.
CONCATENATE con_cret it_attach INTO it_attach .
append it_attach.
clear it_attach.

P_REFO = 'X'.
APPEND T_UPLOAD .
clear t_upload.
ENDIF.
ENDDO.
else.
p_error = 'X'.


ENDIF.

ENDFORM. "process_file
*&--------------------------------------------------------------------*
*& Form populate_message_body
*&--------------------------------------------------------------------*
* text
*---------------------------------------------------------------------*
FORM populate_message_body.
w_doc_data-obj_name = 'TEST'.
w_doc_data-obj_descr = 'Crest to Plc Data'.
w_doc_data-obj_langu = sy-langu.
it_message = 'File Not Found'.
APPEND it_message.
t_receivers-receiver = p_email.
t_receivers-rec_type = 'U'.
append t_receivers.

call function 'SO_NEW_DOCUMENT_SEND_API1'
exporting
document_data = w_doc_data
document_type = 'RAW'
put_in_outbox = 'X'
tables
object_header = it_message
object_content = 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.

if sy-subrc = 0.
commit work.
SUBMIT rsconn01 USING SELECTION-SET 'SAP&CONNECTINT' AND RETURN.

else.
MESSAGE s027(vv) WITH 'E-mail not sent'.

endif.




ENDFORM. "populate_message_body
*&--------------------------------------------------------------------*
*& Form send_attachment
*&--------------------------------------------------------------------*
* text
*---------------------------------------------------------------------*
FORM send_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.

w_doc_data-doc_size = 1.

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[].
clear t_attachment.

* 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
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.
if sy-subrc = 0.
commit work.
SUBMIT rsconn01 USING SELECTION-SET 'SAP&CONNECTINT' AND RETURN.

else.
MESSAGE s027(vv) WITH 'E-mail not sent'.

endif.

* Populate error return code
ld_error = sy-subrc.

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

ENDFORM. "send_attachment

If useful reward.

Vasanth

Former Member
0 Kudos

Hi,

Please refer the links,

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

/people/thomas.jung3/blog/2004/09/08/sending-e-mail-from-abap--version-610-and-higher--bcs-interface

Regards,

Hema.

    • Reward points if it is useful.

Former Member
0 Kudos

Hi!

Check out this blog: /people/thomas.jung3/blog/2004/09/08/sending-e-mail-from-abapversion-610-and-higherbcs-interface

Regards

Tamá

p291102
Active Contributor
0 Kudos

Hi,

Kindly go through this coding.

REPORT YMS_EXTERNALMAIL .

&----


*& Report ZSENDEMAIL *

*& *

&----


*& Example of sending external email via SAPCONNECT *

*& *

&----


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

p_email(40) type c default 'test@sapdev.co.uk' .

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

Thanks,

Shankar