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: 

Function Module for send email

Former Member
0 Kudos

hi experts,

My aim to send message as email not any attachment

what is the function module i need to use for sending mail from program?

how to use the function module ? give some sample code and explain.

please help me regarding this.

thanks,

nagarjuna.

5 REPLIES 5

Former Member
0 Kudos

Hi Nagarjuna,

Please refer the thread,

Best Regards.

  • Reward points if it is helpful.

Former Member
0 Kudos

Hi Nagarjuna,

Your query is already answered in the previous threadd already opened by you regarding the same issue.

Please do not open multiple threads .

I am also replying for this thread , as done for the previous one.

I am sending a sample program to help you use the standard FM 'SO_NEW_DOCUMENT_ATT_SEND_API1'

Please refer this sample code.

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

zdp_solisti1 is a structure having fields naming LINE of type LCHR and its length being 2500.

*******Email declarations

data : gv_delimit TYPE c,

lv_lncnt1(5) TYPE c,

tempread(10) TYPE c VALUE 0,

objpack TYPE sopcklsti1 OCCURS 0 WITH HEADER LINE,

objhead TYPE zdp_solisti1 OCCURS 1 WITH HEADER LINE,

objbin TYPE soli OCCURS 0 WITH HEADER LINE,

objtxt TYPE zdp_solisti1 OCCURS 0 WITH HEADER LINE,

reclist TYPE somlreci1 OCCURS 0 WITH HEADER LINE,

doc_chng LIKE sodocchgi1,

tab_lines LIKE sy-tabix.

CLEAR GV_DELIMIT.

GV_DELIMIT = CL_ABAP_CHAR_UTILITIES=>HORIZONTAL_TAB.

TEMPREAD = LV_LNCNT1.

REFRESH OBJTXT.

CONCATENATE 'The no. of error records are : ' TEMPREAD

INTO OBJTXT-LINE.

APPEND OBJTXT.

CLEAR OBJPACK. REFRESH OBJPACK.

CONCATENATE C_CODE

C_CMMT

C_COMMENT1

INTO OBJTXT-LINE SEPARATED BY GV_DELIMIT.

APPEND OBJTXT.

LOOP AT IT_DATA INTO WA_DATA.

CONCATENATE WA_DATA-ZCODE

WA_DATA-ZCOMMENT

WA_DATA-ZREASON

INTO OBJTXT-LINE SEPARATED BY GV_DELIMIT.

APPEND OBJTXT.

ENDLOOP.

CLEAR GV_DELIMIT.

CLEAR OBJPACK. REFRESH OBJPACK.

CLEAR DOC_CHNG.

TAB_LINES = 0.

OBJHEAD = REJFILE. APPEND OBJHEAD.

DESCRIBE TABLE OBJTXT LINES TAB_LINES.

READ TABLE OBJTXT INDEX TAB_LINES.

DOC_CHNG-OBJ_NAME = REJFILE.

MOVE TEXT-006 TO

DOC_CHNG-OBJ_DESCR.

DOC_CHNG-DOC_SIZE = ( TAB_LINES - 1 ) * 255 + STRLEN( OBJTXT ).

CLEAR OBJPACK-TRANSF_BIN.

OBJPACK-HEAD_START = 1.

OBJPACK-HEAD_NUM = 0.

OBJPACK-BODY_START = 1.

OBJPACK-BODY_NUM = 1. " 3.

OBJPACK-DOC_TYPE = 'RAW'.

APPEND OBJPACK.

FOR ASCII TO BINARY OBJECT FUNCTION

TOTLINEXFER = 0.

TOTXFER = 0.

BINMAXLEN = 255.

REFRESH OBJBIN.

LV_CR = CL_ABAP_CHAR_UTILITIES=>CR_LF.

TGTMAXLEN = BINMAXLEN.

TGTLEN = 0.

LOOP AT OBJTXT FROM 2.

IF NOT OBJTXT-LINE CP 'The no. of records are '." AND

CONCATENATE OBJTXT-LINE LV_CR INTO BUFFER.

SRCOBJLEN = STRLEN( BUFFER ).

SRCOBJPTR = 0.

WHILE SRCOBJLEN > 0 .

XFERLEN = TGTMAXLEN - TGTLEN.

IF XFERLEN > SRCOBJLEN.

XFERLEN = SRCOBJLEN.

MOVE BUFFERSRCOBJPTR TO OBJBIN-LINETGTLEN.

TGTLEN = TGTLEN + SRCOBJLEN.

SRCOBJLEN = 0.

CLEAR BUFFER.

ELSE.

MOVE BUFFER+SRCOBJPTR(XFERLEN) TO

OBJBIN-LINE+TGTLEN(XFERLEN).

APPEND OBJBIN.

CLEAR OBJBIN-LINE.

TGTLEN = 0.

SRCOBJLEN = SRCOBJLEN - XFERLEN.

SRCOBJPTR = SRCOBJPTR + XFERLEN.

TOTLINEXFER = TOTLINEXFER + 1.

ENDIF.

TOTXFER = TOTXFER + XFERLEN.

ENDWHILE.

ENDIF.

ENDLOOP.

IF TGTLEN > 0.

APPEND OBJBIN.

TOTLINEXFER = TOTLINEXFER + 1.

ENDIF.

CLEAR OBJPACK-TRANSF_BIN.

OBJPACK-TRANSF_BIN = 'X'.

OBJPACK-HEAD_START = 1.

OBJPACK-HEAD_NUM = 0.

OBJPACK-BODY_START = 1.

OBJPACK-BODY_NUM = TOTLINEXFER.

OBJPACK-DOC_TYPE = 'XLS'.

OBJPACK-OBJ_NAME = REJFILE.

OBJPACK-OBJ_DESCR = 'Rejected Entries File'.

OBJPACK-DOC_SIZE = TOTXFER.

APPEND OBJPACK.

REFRESH RECLIST.

MAIL1 = 'xxxxxxxx' " Email Address

RECLIST-RECEIVER = MAIL1.

RECLIST-REC_TYPE = 'U'.

APPEND RECLIST.

CALL FUNCTION 'SO_NEW_DOCUMENT_ATT_SEND_API1'

EXPORTING

DOCUMENT_DATA = DOC_CHNG

PUT_IN_OUTBOX = 'X'

COMMIT_WORK = 'X'

TABLES

PACKING_LIST = OBJPACK

OBJECT_HEADER = OBJHEAD

CONTENTS_BIN = OBJBIN

CONTENTS_TXT = OBJTXT

RECEIVERS = RECLIST.

IF SY-SUBRC 0.

MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO

WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

ENDIF.

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

Hope this is helpful to you. If you need further information, revert back. Please close this thread if useful.

Reward all the helpful answers.

Regards

Nagaraj T

Former Member
0 Kudos

hi,

Please go through the following link[http://sapabapnotes.blogspot.com/2008/05/sample-abap-program-for-sending-sap.html]

you can find various code samples in this site.

Reward points if helpful.

Thanks and Regards.

Former Member
0 Kudos

Hi

i am sending a sample program.

REPORT ZMAIL.

TABLES: ekko.

PARAMETERS: p_email TYPE somlreci1-receiver .

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.

*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.

data: ld_store(50) type c. "Leading zeros

CONSTANTS: con_cret(5) TYPE c VALUE '0D', "OK for non Unicode

con_tab(5) TYPE c VALUE '09'. "OK for non Unicode

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 '=REPLACE("' wa_charekpo-ebelp '",1,5,"'

wa_charekpo-ebelp '")' into ld_store .

CONCATENATE wa_charekpo-ebeln ld_store 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

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

Former Member
0 Kudos

Hi

Hope it will help you.

Tell your basis team to configure the SMTP Server for sending a mails to your company domain.

Reward if help.

&----


*& Report ZEMAIL_SIVA

*&

&----


*&

*&

&----


REPORT zemail_siva.

DATA : gd_doc_data LIKE sodocchgi1,

l_i_mail TYPE STANDARD TABLE OF solisti1 WITH HEADER LINE,

it_receivers TYPE TABLE OF somlreci1 WITH HEADER LINE.

DATA: w_pack TYPE sopcklsti1,

i_pack TYPE TABLE OF sopcklsti1.

PARAMETERS : p_email(100) TYPE c.

gd_doc_data-obj_descr = 'TEST EMAIL FROM ABAP SE TEAM'.

l_i_mail-line = 'Testing the Mail from ABAP on 31st March 2008'.

APPEND l_i_mail.

it_receivers-receiver = p_email.

it_receivers-rec_type = 'A'.

it_receivers-com_type = 'INT'.

*it_receivers-notif_del = 'X'.

*it_receivers-notif_ndel = 'X'.

APPEND it_receivers.

CLEAR w_pack.

w_pack-head_start = 1.

w_pack-head_num = 0.

w_pack-body_start = 1.

DESCRIBE TABLE l_i_mail LINES w_pack-body_num.

w_pack-doc_type = 'RAW'.

w_pack-doc_size = w_pack-body_num * 255.

APPEND w_pack TO i_pack.

CALL FUNCTION 'SO_NEW_DOCUMENT_ATT_SEND_API1'

EXPORTING

document_data = gd_doc_data

PUT_IN_OUTBOX = 'X'

COMMIT_WORK = 'X'

  • IMPORTING

  • SENT_TO_ALL =

  • NEW_OBJECT_ID =

TABLES

packing_list = i_pack

  • OBJECT_HEADER =

  • CONTENTS_BIN =

contents_txt = l_i_mail

  • CONTENTS_HEX =

  • OBJECT_PARA =

  • OBJECT_PARB =

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

..

IF sy-subrc <> 0.

MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno

WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.

ELSE.

WRITE : 'Mail Sent to specified address'.

ENDIF.