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

need a sample code to send an email

Former Member
0 Likes
712

Urgently needed a sample code to send an email.

function module 'SO_NEW_DOCUMENT_SEND_API1' is used to send mails with sap.

very urgent ,

thanks in advance.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
678

hi

check with this code and tell whether it helps

&----


*& Report ZTESTMAIL *

*& *

&----


*& *

*& *

&----


REPORT ZTESTMAIL .

tables: bseg.

parameters: p_email type somlreci1-receiver default

'praveen.srrec@gmail.com'.

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: zuonr1 like bseg-zuonr.

*DATA: begin of int_bseg occurs 0,

*

  • zuonr like bseg-zuonr,

  • end of int_bseg.

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

'Assignment Date after 2 days'

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

dATA wa_days LIKE P0347-SCRDD.

*DATA int_bseg1 LIKE TABLE OF int_bseg WITH HEADER LINE .

DATA: date LIKE sy-datum,

date1 like P0001-BEGDA.

*changedate(10) TYPE c.

DATA: BEGIN OF it_date1, " To accept date into a internal table

yyyy(4),

mm(2),

dd(2),

END OF it_date1.

DATA: BEGIN OF it_date2, " Internal table for dd/mm/yyyy format

dd(2),

mm(2),

yyyy(4),

END OF it_date2.

DATA: BEGIN OF it_date3, " To accept date into a internal table

yyyy(4),

mm(2),

dd(2),

END OF it_date3.

DATA: BEGIN OF it_date4, " Internal table for dd/mm/yyyy format

dd(2),

mm(2),

yyyy(4),

END OF it_date4.

date = sy-datum.

it_date1 = date.

MOVE-CORRESPONDING it_date1 TO: it_date2.

data : int_bseg like bseg occurs 0 with header line.

select single zuonr from bseg into corresponding fields of int_bseg

where bukrs = 'GSPL' and

gJAHR = '2006'.

*loop at int_bseg.

*write int_bseg-zuonr.

*endloop.

*if sy-subrc <> 0.

  • write 'no records found'.

*endif.

*

*select * from bseg into zuonr1

*where belnr = 100000020 and

  • bukrs = 'GSPL' and

  • zuonr <> ' ' .

  • and

  • belnr = 100000020.

*

LOOP AT int_bseg.

date1 = zuonr1.

it_date3 = date1.

MOVE-CORRESPONDING it_date3 TO: it_date4.

CALL FUNCTION 'HR_HK_DIFF_BT_2_DATES'

EXPORTING

DATE1 = it_date4

DATE2 = it_date2

OUTPUT_FORMAT = '03'

IMPORTING

DAYS = wa_days

.

*else.

  • exit.

*endif.

*

endloop.

*form data_retrieval.

*

*DATA wa_days LIKE P0347-SCRDD.

*DATA int_bseg1 LIKE TABLE OF int_bseg WITH HEADER LINE .

*

*

*DATA: date LIKE sy-datum,

  • date1 like P0001-BEGDA.

**changedate(10) TYPE c.

*

  • DATA: BEGIN OF it_date1, " To accept date into a internal table

  • yyyy(4),

  • mm(2),

  • dd(2),

  • END OF it_date1.

*

  • DATA: BEGIN OF it_date2, " Internal table for dd/mm/yyyy format

  • dd(2),

  • mm(2),

  • yyyy(4),

  • END OF it_date2.

*

*

  • DATA: BEGIN OF it_date3, " To accept date into a internal table

  • yyyy(4),

  • mm(2),

  • dd(2),

  • END OF it_date3.

*

  • DATA: BEGIN OF it_date4, " Internal table for dd/mm/yyyy format

  • dd(2),

  • mm(2),

  • yyyy(4),

  • END OF it_date4.

*

*

*date = sy-datum.

*it_date1 = date.

*

*

*MOVE-CORRESPONDING it_date1 TO: it_date2.

*

*

*select zuonr from bseg into corresponding fields of int_bseg.

*

**where belnr = 10000002 and bukrs = 'gspl'.

*

*

*LOOP AT int_bseg.

*

*

  • date1 = int_bseg-zuonr.

  • it_date3 = date1.

*

  • MOVE-CORRESPONDING it_date3 TO: it_date4.

*

*

*

*

*

  • CALL FUNCTION 'HR_HK_DIFF_BT_2_DATES'

  • EXPORTING

  • DATE1 = it_date4

*

  • DATE2 = it_date2

  • OUTPUT_FORMAT = '03'

  • IMPORTING

    • YEARS =

    • MONTHS =

  • DAYS = wa_days

    • EXCEPTIONS

    • INVALID_DATES_SPECIFIED = 1

    • OTHERS = 2

  • .

  • IF SY-SUBRC <> 0.

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

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

  • ENDIF.

*

  • IF wa_days = 3.

  • MOVE-CORRESPONDING int_bseg TO int_bseg1.

  • APPEND int_bseg1.

  • CLEAR int_bseg1.

  • ENDIF.

*endloop.

endform. " DATA_RETRIEVAL

*

*

*&----


*

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

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

if gd_error eq 0.

submit rsconn01 with mode = 'INT'

with output = 'X'

and return.

endif.

endform. " INITIATE_MAIL_EXECUTE_PROGRAM

*&----


*

*& Form POPULATE_EMAIL_MESSAGE_BODY

*&----


*

  • Populate message body text

*----


*

form populate_email_message_body.

refresh it_message.

it_message = 'Assignment Date is coming After 2 Days Please check

doc.'.

append it_message.

endform. " POPULATE_EMAIL_MESSAGE_BODY

*tables: bseg.

*

*

*parameters: p_email type somlreci1-receiver default

*'laxman@gujaratpetro.com'.

*

*

*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: begin of int_bseg occurs 0,

*

  • zuonr like bseg-zuonr,

  • end of int_bseg.

*

*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

*'Assignment Date after 2 days'

*'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 zuonr from bseg into corresponding fields of int_bseg.

*endselect.

*

*DATA: begin of int_bseg1 occurs 0,

*

  • zuonr1 like bseg-zuonr,

  • end of int_bseg1.

*

*

*loop at int_bseg.

*

  • int_bseg1-zuonr1 = int_bseg-zuonr.

*

*

*endloop.

*

*endform. " DATA_RETRIEVAL

*

*

*

**&----


**

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

*

*

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

  • if gd_error eq 0.

  • submit rsconn01 with mode = 'INT'

  • with output = 'X'

  • and return.

  • endif.

*endform. " INITIATE_MAIL_EXECUTE_PROGRAM

*

*

*

**&----


**

**& Form POPULATE_EMAIL_MESSAGE_BODY

**&----


**

    • Populate message body text

**----


**

*form populate_email_message_body.

*refresh it_message.

*it_message = 'Assignment Date is coming After 2 Days Please check

*doc.'.

*append it_message.

*endform. " POPULATE_EMAIL_MESSAGE_BODY

Hope this helps to solve ur problem....

<b>do reward if useful....</b>

regards

dinesh

5 REPLIES 5
Read only

Former Member
0 Likes
679

hi

check with this code and tell whether it helps

&----


*& Report ZTESTMAIL *

*& *

&----


*& *

*& *

&----


REPORT ZTESTMAIL .

tables: bseg.

parameters: p_email type somlreci1-receiver default

'praveen.srrec@gmail.com'.

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: zuonr1 like bseg-zuonr.

*DATA: begin of int_bseg occurs 0,

*

  • zuonr like bseg-zuonr,

  • end of int_bseg.

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

'Assignment Date after 2 days'

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

dATA wa_days LIKE P0347-SCRDD.

*DATA int_bseg1 LIKE TABLE OF int_bseg WITH HEADER LINE .

DATA: date LIKE sy-datum,

date1 like P0001-BEGDA.

*changedate(10) TYPE c.

DATA: BEGIN OF it_date1, " To accept date into a internal table

yyyy(4),

mm(2),

dd(2),

END OF it_date1.

DATA: BEGIN OF it_date2, " Internal table for dd/mm/yyyy format

dd(2),

mm(2),

yyyy(4),

END OF it_date2.

DATA: BEGIN OF it_date3, " To accept date into a internal table

yyyy(4),

mm(2),

dd(2),

END OF it_date3.

DATA: BEGIN OF it_date4, " Internal table for dd/mm/yyyy format

dd(2),

mm(2),

yyyy(4),

END OF it_date4.

date = sy-datum.

it_date1 = date.

MOVE-CORRESPONDING it_date1 TO: it_date2.

data : int_bseg like bseg occurs 0 with header line.

select single zuonr from bseg into corresponding fields of int_bseg

where bukrs = 'GSPL' and

gJAHR = '2006'.

*loop at int_bseg.

*write int_bseg-zuonr.

*endloop.

*if sy-subrc <> 0.

  • write 'no records found'.

*endif.

*

*select * from bseg into zuonr1

*where belnr = 100000020 and

  • bukrs = 'GSPL' and

  • zuonr <> ' ' .

  • and

  • belnr = 100000020.

*

LOOP AT int_bseg.

date1 = zuonr1.

it_date3 = date1.

MOVE-CORRESPONDING it_date3 TO: it_date4.

CALL FUNCTION 'HR_HK_DIFF_BT_2_DATES'

EXPORTING

DATE1 = it_date4

DATE2 = it_date2

OUTPUT_FORMAT = '03'

IMPORTING

DAYS = wa_days

.

*else.

  • exit.

*endif.

*

endloop.

*form data_retrieval.

*

*DATA wa_days LIKE P0347-SCRDD.

*DATA int_bseg1 LIKE TABLE OF int_bseg WITH HEADER LINE .

*

*

*DATA: date LIKE sy-datum,

  • date1 like P0001-BEGDA.

**changedate(10) TYPE c.

*

  • DATA: BEGIN OF it_date1, " To accept date into a internal table

  • yyyy(4),

  • mm(2),

  • dd(2),

  • END OF it_date1.

*

  • DATA: BEGIN OF it_date2, " Internal table for dd/mm/yyyy format

  • dd(2),

  • mm(2),

  • yyyy(4),

  • END OF it_date2.

*

*

  • DATA: BEGIN OF it_date3, " To accept date into a internal table

  • yyyy(4),

  • mm(2),

  • dd(2),

  • END OF it_date3.

*

  • DATA: BEGIN OF it_date4, " Internal table for dd/mm/yyyy format

  • dd(2),

  • mm(2),

  • yyyy(4),

  • END OF it_date4.

*

*

*date = sy-datum.

*it_date1 = date.

*

*

*MOVE-CORRESPONDING it_date1 TO: it_date2.

*

*

*select zuonr from bseg into corresponding fields of int_bseg.

*

**where belnr = 10000002 and bukrs = 'gspl'.

*

*

*LOOP AT int_bseg.

*

*

  • date1 = int_bseg-zuonr.

  • it_date3 = date1.

*

  • MOVE-CORRESPONDING it_date3 TO: it_date4.

*

*

*

*

*

  • CALL FUNCTION 'HR_HK_DIFF_BT_2_DATES'

  • EXPORTING

  • DATE1 = it_date4

*

  • DATE2 = it_date2

  • OUTPUT_FORMAT = '03'

  • IMPORTING

    • YEARS =

    • MONTHS =

  • DAYS = wa_days

    • EXCEPTIONS

    • INVALID_DATES_SPECIFIED = 1

    • OTHERS = 2

  • .

  • IF SY-SUBRC <> 0.

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

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

  • ENDIF.

*

  • IF wa_days = 3.

  • MOVE-CORRESPONDING int_bseg TO int_bseg1.

  • APPEND int_bseg1.

  • CLEAR int_bseg1.

  • ENDIF.

*endloop.

endform. " DATA_RETRIEVAL

*

*

*&----


*

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

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

if gd_error eq 0.

submit rsconn01 with mode = 'INT'

with output = 'X'

and return.

endif.

endform. " INITIATE_MAIL_EXECUTE_PROGRAM

*&----


*

*& Form POPULATE_EMAIL_MESSAGE_BODY

*&----


*

  • Populate message body text

*----


*

form populate_email_message_body.

refresh it_message.

it_message = 'Assignment Date is coming After 2 Days Please check

doc.'.

append it_message.

endform. " POPULATE_EMAIL_MESSAGE_BODY

*tables: bseg.

*

*

*parameters: p_email type somlreci1-receiver default

*'laxman@gujaratpetro.com'.

*

*

*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: begin of int_bseg occurs 0,

*

  • zuonr like bseg-zuonr,

  • end of int_bseg.

*

*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

*'Assignment Date after 2 days'

*'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 zuonr from bseg into corresponding fields of int_bseg.

*endselect.

*

*DATA: begin of int_bseg1 occurs 0,

*

  • zuonr1 like bseg-zuonr,

  • end of int_bseg1.

*

*

*loop at int_bseg.

*

  • int_bseg1-zuonr1 = int_bseg-zuonr.

*

*

*endloop.

*

*endform. " DATA_RETRIEVAL

*

*

*

**&----


**

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

*

*

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

  • if gd_error eq 0.

  • submit rsconn01 with mode = 'INT'

  • with output = 'X'

  • and return.

  • endif.

*endform. " INITIATE_MAIL_EXECUTE_PROGRAM

*

*

*

**&----


**

**& Form POPULATE_EMAIL_MESSAGE_BODY

**&----


**

    • Populate message body text

**----


**

*form populate_email_message_body.

*refresh it_message.

*it_message = 'Assignment Date is coming After 2 Days Please check

*doc.'.

*append it_message.

*endform. " POPULATE_EMAIL_MESSAGE_BODY

Hope this helps to solve ur problem....

<b>do reward if useful....</b>

regards

dinesh

Read only

Former Member
0 Likes
678

Hi

Mailing with Attachment by ABAP Coding

Refer this link:

FORM send_list_to_basis .

DATA: w_path LIKE rlgrap OCCURS 0 WITH HEADER LINE,

lt_index TYPE sy-tabix,

doc_type(3) TYPE c,

descr LIKE it_objpack_basis-obj_descr,

temp_data LIKE w_path,

temp1 TYPE string,

tab_lines TYPE i,

langu(15) TYPE c,

expirydate TYPE so_obj_edt,

L_FILE1(100).

CONCATENATE 'C:\' sy-repid '_' sy-datum '.XLS' INTO L_FILE1.

W_PATH-FILENAME = L_FILE1.

APPEND w_path.

CLEAR w_path.

wa_doc_chng-obj_descr = 'User List not logged on for 180 days'.

wa_doc_chng-obj_langu = 'E'.

wa_doc_chng-obj_expdat = sy-datum.

CLEAR w_subject.

CONCATENATE 'Please find attached document with list of users'

'not logged on for 180 days for client' sy-mandt

INTO w_subject SEPARATED BY space.

it_objtxt_basis-line = w_subject.

APPEND it_objtxt_basis.

CLEAR it_objtxt_basis.

it_objtxt_basis-line = text-004.

APPEND it_objtxt_basis.

CLEAR it_objtxt_basis.

CLEAR w_tab_line.

DESCRIBE TABLE it_objtxt_basis LINES w_tab_line.

READ TABLE it_objtxt_basis INDEX w_tab_line INTO l_cline.

wa_doc_chng-doc_size =

( w_tab_line - 1 ) * 255 + STRLEN( l_cline ).

CLEAR it_objpack_basis-transf_bin.

it_objpack_basis-head_start = 1.

it_objpack_basis-head_num = 0.

it_objpack_basis-body_start = 1.

it_objpack_basis-body_num = w_tab_line.

it_objpack_basis-doc_type = 'RAW'.

APPEND it_objpack_basis.

CLEAR it_objpack_basis.

LOOP AT w_path.

temp1 = w_path.

descr = w_path.

CALL FUNCTION 'STRING_REVERSE'

EXPORTING

string = descr

lang = 'E'

IMPORTING

rstring = descr.

CALL FUNCTION 'STRING_SPLIT'

EXPORTING

delimiter = '\'

string = descr

IMPORTING

head = descr

tail = temp_data.

CALL FUNCTION 'STRING_REVERSE'

EXPORTING

string = descr

lang = 'E'

IMPORTING

rstring = descr.

CALL FUNCTION 'STRING_SPLIT'

EXPORTING

delimiter = '.'

string = descr

IMPORTING

head = temp_data

tail = doc_type.

CALL FUNCTION 'GUI_UPLOAD'

EXPORTING

filename = temp1

filetype = 'BIN'

header_length = 0

read_by_line = 'X'

replacement = '#'

TABLES

data_tab = it_upload.

DESCRIBE TABLE it_upload LINES tab_lines.

DESCRIBE TABLE it_objbin_basis LINES lt_index.

lt_index = lt_index + 1.

LOOP AT it_upload.

wa_objbin_basis-line = it_upload-line.

APPEND wa_objbin_basis TO it_objbin_basis.

CLEAR wa_objbin_basis.

ENDLOOP.

it_objpack_basis-transf_bin = 'X'.

it_objpack_basis-head_start = 0.

it_objpack_basis-head_num = 0.

it_objpack_basis-body_start = lt_index.

it_objpack_basis-body_num = tab_lines.

it_objpack_basis-doc_type = doc_type.

it_objpack_basis-obj_descr = descr.

it_objpack_basis-doc_size = tab_lines * 255.

APPEND it_objpack_basis.

CLEAR it_objpack_basis.

ENDLOOP.

it_reclist_basis-receiver = 'XXX@.com'.

it_reclist_basis-rec_type = 'U'.

APPEND it_reclist_basis.

CLEAR it_reclist_basis.

CALL FUNCTION 'SO_NEW_DOCUMENT_ATT_SEND_API1'

EXPORTING

document_data = wa_doc_chng

put_in_outbox = 'X'

commit_work = 'X'

TABLES

packing_list = it_objpack_basis

contents_txt = it_objtxt_basis

contents_bin = it_objbin_basis

receivers = it_reclist_basis

EXCEPTIONS

too_many_receivers = 1

document_not_sent = 2

operation_no_authorization = 4

OTHERS = 99.

IF sy-subrc EQ 0.

SUBMIT rsconn01 WITH mode = 'INT' AND RETURN.

ENDIF.

ENDFORM. " send_list_to_basis

<b>Reward points for useful Answers</b>

Regards

Anji

Read only

Former Member
0 Likes
678

hi,

Go thru this blog

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

<b>Rewards if helpful.</b>

Swati

Read only

Former Member
0 Likes
678

DATA: SENT LIKE SONV-FLAG.

DATA: EMAIL_ID LIKE SOFOLENTI1-OBJECT_ID.

DATA: BEGIN OF EMAIL_DATA.

INCLUDE STRUCTURE SODOCCHGI1.

DATA: END OF EMAIL_DATA.

DATA: BEGIN OF EMAIL_SEND OCCURS 10.

INCLUDE STRUCTURE SOMLRECI1.

DATA: END OF EMAIL_SEND.

DATA: BEGIN OF EMAIL_TEXT OCCURS 10.

INCLUDE STRUCTURE SOLISTI1.

DATA: END OF EMAIL_TEXT.

EMAIL_TEXT-LINE = 'Hi,'.

APPEND EMAIL_TEXT.

CONCATENATE 'A purchase request has been created with number ‘' lv_po_reqno '’. Please use transaction ‘ZP_VEN’ to process the request.' into EMAIL_TEXT-LINE .

APPEND EMAIL_TEXT.

EMAIL_TEXT-LINE = 'Best Regards,'.

APPEND EMAIL_TEXT.

EMAIL_TEXT-LINE = 'Parag P. Bhise'.

APPEND EMAIL_TEXT.

EMAIL_DATA-OBJ_NAME = 'PurchaseReq'.

EMAIL_DATA-OBJ_DESCR = 'New Purchase Requsition '.

EMAIL_DATA-OBJ_LANGU = 'E'.

EMAIL_DATA-SENSITIVTY = 'P'.

EMAIL_DATA-OBJ_PRIO = '1'.

EMAIL_DATA-NO_CHANGE = 'X'.

EMAIL_DATA-PRIORITY = '1'.

EMAIL_SEND-RECEIVER = 'STARSAP' . '' SAP login name

EMAIL_SEND-REC_TYPE = 'B'.

EMAIL_SEND-EXPRESS = 'X'.

APPEND EMAIL_SEND.

CALL FUNCTION 'SO_NEW_DOCUMENT_SEND_API1'

EXPORTING

DOCUMENT_DATA = EMAIL_DATA

DOCUMENT_TYPE = 'RAW'

PUT_IN_OUTBOX = 'X'

IMPORTING

SENT_TO_ALL = SENT

NEW_OBJECT_ID = EMAIL_ID

TABLES

OBJECT_CONTENT = EMAIL_TEXT

RECEIVERS = EMAIL_SEND

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.

Read only

0 Likes
678

Send Mail In OOABAP

DATA: SENT TYPE SONV-FLAG.

DATA: EMAIL_ID TYPE SOFOLENTI1-OBJECT_ID,

EMAIL_DATA TYPE STANDARD TABLE OF SODOCCHGI1 ,

LS_EMAIL_DATA like line of EMAIL_DATA,

EMAIL_SEND TYPE STANDARD TABLE OF SOMLRECI1,

LS_EMAIL_SEND LIKE LINE OF EMAIL_SEND ,

EMAIL_TEXT TYPE STANDARD TABLE OF SOLISTI1 ,

ls_email_text like line of EMAIL_TEXT .

  • Code to Send mail

ls_email_text-LINE = 'Hi,'.

APPEND ls_email_text to EMAIL_TEXT.

CONCATENATE 'A purchase request has been created with number ‘' ls_nd_poinfo-req_no '’. Please use transaction ‘ZP_VEN’ to process the request.' into ls_EMAIL_TEXT-LINE .

APPEND ls_email_text to EMAIL_TEXT.

ls_EMAIL_TEXT-LINE = 'Best Regards,'.

APPEND ls_EMAIL_TEXT to EMAIL_TEXT.

ls_EMAIL_TEXT-LINE = 'Parag P. Bhise'.

APPEND ls_EMAIL_TEXT to EMAIL_TEXT.

LS_EMAIL_DATA-OBJ_NAME = 'PurchaseReq'.

LS_EMAIL_DATA-OBJ_DESCR = 'New Purchase Requsition '.

LS_EMAIL_DATA-OBJ_LANGU = 'E'.

LS_EMAIL_DATA-SENSITIVTY = 'P'.

LS_EMAIL_DATA-OBJ_PRIO = '1'.

LS_EMAIL_DATA-NO_CHANGE = 'X'.

LS_EMAIL_DATA-PRIORITY = '1'.

APPEND LS_EMAIL_DATA to EMAIL_DATA .

LS_EMAIL_SEND-RECEIVER = 'Idfgd'.

LS_EMAIL_SEND-REC_TYPE = 'B'.

LS_EMAIL_SEND-EXPRESS = 'X'.

APPEND LS_EMAIL_SEND to EMAIL_SEND .

CALL FUNCTION 'SO_NEW_DOCUMENT_SEND_API1'

EXPORTING

DOCUMENT_DATA = LS_EMAIL_DATA

DOCUMENT_TYPE = 'RAW'

PUT_IN_OUTBOX = 'X'

IMPORTING

SENT_TO_ALL = SENT

NEW_OBJECT_ID = EMAIL_ID

TABLES

OBJECT_CONTENT = EMAIL_TEXT

RECEIVERS = EMAIL_SEND

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.