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: 

design a function module to send email

Former Member
0 Kudos

hii gurus ,

i have to design a function module which takes three input email id (reciever), body (text of message) , and header ie subject and when executed should send email to the reciever. pls help

Aditya Shrivastava.

1 ACCEPTED SOLUTION

Former Member
0 Kudos

TABLES: KNA1.

  • data for send function

DATA DOC_DATA LIKE SODOCCHGI1.

DATA OBJECT_ID LIKE SOODK.

DATA OBJCONT LIKE SOLI OCCURS 10 WITH HEADER LINE.

DATA RECEIVER LIKE SOMLRECI1 OCCURS 1 WITH HEADER LINE.

SELECT * FROM KNA1 WHERE ANRED LIKE 'C%'.

WRITE:/ KNA1-KUNNR, KNA1-ANRED.

  • send data internal table

CONCATENATE KNA1-KUNNR KNA1-ANRED

INTO OBJCONT-LINE SEPARATED BY SPACE.

APPEND OBJCONT.

ENDSELECT.

  • insert receiver (sap name)

REFRESH RECEIVER.

CLEAR RECEIVER.

MOVE: SY-UNAME TO RECEIVER-RECEIVER,

'X' TO RECEIVER-EXPRESS,

'B' TO RECEIVER-REC_TYPE.

APPEND RECEIVER.

  • insert mail description

WRITE 'Sending a mail through abap'

TO DOC_DATA-OBJ_DESCR.

CALL FUNCTION 'SO_NEW_DOCUMENT_SEND_API1'

EXPORTING

DOCUMENT_DATA = DOC_DATA

IMPORTING

NEW_OBJECT_ID = OBJECT_ID

TABLES

OBJECT_CONTENT = OBJCONT

RECEIVERS = RECEIVER

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.

10 REPLIES 10

Former Member
0 Kudos

check the FM

SO_NEW_DOCUMENT_ATT_SEND_API1

SO_NEW_DOCUMENT_SEND_API1

these will help you

Former Member
0 Kudos

TABLES: KNA1.

  • data for send function

DATA DOC_DATA LIKE SODOCCHGI1.

DATA OBJECT_ID LIKE SOODK.

DATA OBJCONT LIKE SOLI OCCURS 10 WITH HEADER LINE.

DATA RECEIVER LIKE SOMLRECI1 OCCURS 1 WITH HEADER LINE.

SELECT * FROM KNA1 WHERE ANRED LIKE 'C%'.

WRITE:/ KNA1-KUNNR, KNA1-ANRED.

  • send data internal table

CONCATENATE KNA1-KUNNR KNA1-ANRED

INTO OBJCONT-LINE SEPARATED BY SPACE.

APPEND OBJCONT.

ENDSELECT.

  • insert receiver (sap name)

REFRESH RECEIVER.

CLEAR RECEIVER.

MOVE: SY-UNAME TO RECEIVER-RECEIVER,

'X' TO RECEIVER-EXPRESS,

'B' TO RECEIVER-REC_TYPE.

APPEND RECEIVER.

  • insert mail description

WRITE 'Sending a mail through abap'

TO DOC_DATA-OBJ_DESCR.

CALL FUNCTION 'SO_NEW_DOCUMENT_SEND_API1'

EXPORTING

DOCUMENT_DATA = DOC_DATA

IMPORTING

NEW_OBJECT_ID = OBJECT_ID

TABLES

OBJECT_CONTENT = OBJCONT

RECEIVERS = RECEIVER

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.

0 Kudos

but where did i have to specify email id of the recipient as in this coding there is no email id of reciever

0 Kudos

hi,

email id of the receiver is maintained in the RECEIVERS table of F.M. 'SO_NEW_DOCUMENT_SEND_API1'.

Regards,

Saba

0 Kudos

can we send email to external recipient ie say to gmail or yahoo mail etc

0 Kudos

Hi,

Yes we can send the mails to the external mail id's Yahoo and gmail etc apart from SAP users ..

regards,

Usha

Former Member
0 Kudos

hi aditya,

try this class method......it might help you.

call method cl_http_client=>create_by_url

exporting url = wf_string

importing client = http_client

exceptions argument_not_found = 1

plugin_not_active = 2

internal_error = 3

others = 4.

thanks

Former Member
0 Kudos

Hi Aditya,

If you could not get your problem solved with predefined FMs, Try making this FM.. it will help you..as this FM is made in our server locally..

name : z938_mailsend

import parameters

param name type associated type shrt text

I_TID LIKE THEAD-TDID Text ID of text to be read

I_LAN LIKE THEAD-TDSPRAS Language of text to be read

I_L_NAME LIKE THEAD-TDNAME Name of text to be read

I_OID LIKE THEAD-TDOBJECT Object of text to be read

W_RETURN TYPE SSFCRESCL Smart Forms: Return value at end of form printing

I_POTITLE TYPE EBELN purchase order number

W_DOC_CHNG TYPE SODOCCHGI1 data of an object which can be changed

I_ADDRNUM LIKE ADR6-ADDRNUMBER Address number of Vendor

source code :

FUNCTION z938_mailsend.

*"----


""Local Interface:

*" IMPORTING

*" REFERENCE(I_TID) LIKE THEAD-TDID

*" REFERENCE(I_LAN) LIKE THEAD-TDSPRAS

*" REFERENCE(I_L_NAME) LIKE THEAD-TDNAME

*" REFERENCE(I_OID) LIKE THEAD-TDOBJECT

*" REFERENCE(W_RETURN) TYPE SSFCRESCL

*" REFERENCE(I_POTITLE) TYPE EBELN

*" REFERENCE(W_DOC_CHNG) TYPE SODOCCHGI1

*" REFERENCE(I_ADDRNUM) LIKE ADR6-ADDRNUMBER

*"----


----


*data declaration

----


*Internal Table declaration

DATA : it_otf TYPE STANDARD TABLE OF itcoo ,

it_tline TYPE STANDARD TABLE OF tline ,

it_record TYPE STANDARD TABLE OF solisti1,

it_objpack TYPE STANDARD TABLE OF sopcklsti1,

it_objtxt TYPE STANDARD TABLE OF solisti1 ,

it_objbin TYPE STANDARD TABLE OF solisti1 ,

it_reclist TYPE STANDARD TABLE OF somlreci1 ,

it_tdline TYPE STANDARD TABLE OF tline,

*Work Area declarations

wa_objhead TYPE soli_tab,

wa_buffer TYPE string, "To convert from 132 to 255

wa_it_objtxt TYPE solisti1,

wa_it_objpack TYPE sopcklsti1,

wa_it_record TYPE solisti1,

wa_it_reclist TYPE somlreci1,

wa_it_tline TYPE tline,

wa_tdline TYPE tline,

*variable declaration

v_lines_bin TYPE i,

v_lines_txt TYPE i,

v_len_in TYPE sood-objlen, "#EC NEEDED

v_mailaddr TYPE adr6-smtp_addr.

*constant declaration

CONSTANTS : c_msgtype TYPE c VALUE 'E', " for declaring masseage type as error msg

c_X TYPE c VALUE 'X',

c_U TYPE c VALUE 'U'.

it_otf[] = w_return-otfdata[].

*calling function module to convert otf format of smartform to pdf format

CALL FUNCTION 'CONVERT_OTF'

EXPORTING

format = text-001 "PDF

max_linewidth = 132

  • ARCHIVE_INDEX =

  • COPYNUMBER =

  • ASCII_BIDI_VIS2LOG =

  • PDF_DELETE_OTFTAB =

IMPORTING

bin_filesize = v_len_in

  • BIN_FILE =

TABLES

otf = it_otf

lines = it_tline

EXCEPTIONS

err_max_linewidth = 1

err_format = 2

err_conv_not_possible = 3

err_bad_otf = 4

OTHERS = 5 .

IF sy-subrc NE 0.

sy-msgid = text-002. "some msg

sy-msgty = c_msgtype. " E

sy-msgno = 040.

sy-msgv1 = ' '.

sy-msgv2 = ' '.

sy-msgv3 = ' '.

sy-msgv4 = ' '.

EXIT.

ELSE.

  • Convert PDF from 132 to 255.

LOOP AT it_tline INTO wa_it_tline.

TRANSLATE wa_it_tline USING ' ~'. " Replacing space by ~

CONCATENATE wa_buffer wa_it_tline INTO wa_buffer.

ENDLOOP.

TRANSLATE wa_buffer USING '~ '. " Replacing space by ~

DO.

wa_it_record = wa_buffer.

APPEND wa_it_record TO it_record. " Appending 255 characters as a record

SHIFT wa_buffer LEFT BY 255 PLACES.

IF wa_buffer IS INITIAL.

EXIT.

ENDIF.

ENDDO.

REFRESH:it_objbin.

CLEAR wa_objhead.

  • Object with PDF.

it_objbin[] = it_record[].

DESCRIBE TABLE it_objbin LINES v_lines_bin.

*calling a function module read_text to get the text for the mail body

CALL FUNCTION 'READ_TEXT'

EXPORTING

id = i_tid

language = i_lan

name = i_l_name

object = i_oid

  • IMPORTING

  • HEADER =

TABLES

lines = it_tdline

EXCEPTIONS

id = 1

language = 2

name = 3

not_found = 4

object = 5

reference_check = 6

wrong_access_to_archive = 7

OTHERS = 8

.

IF sy-subrc <> 0. "#EC

ENDIF.

IF it_tdline IS NOT INITIAL.

LOOP AT it_tdline INTO wa_tdline.

wa_it_objtxt = wa_tdline-tdline.

APPEND wa_it_objtxt TO it_objtxt.

CLEAR: wa_tdline,

wa_it_objtxt.

ENDLOOP.

DESCRIBE TABLE it_objtxt LINES v_lines_txt.

ENDIF.

  • Pack to main body as RAW.

CLEAR wa_it_objpack-transf_bin. "Obj. to be transported not in binary form

wa_it_objpack-head_start = 1. "Start line of object header in transport packet

wa_it_objpack-head_num = 0. "Number of lines of an object header in object packet

wa_it_objpack-body_start = 1. "Start line of object contents in an object packet

wa_it_objpack-body_num = v_lines_txt. "Number of lines of the object contents in an object packet

  • Code for document class

wa_it_objpack-doc_type = text-004. "RAW

APPEND wa_it_objpack TO it_objpack.

  • Packing as PDF.

wa_it_objpack-transf_bin = c_X. " X

wa_it_objpack-head_start = 1.

wa_it_objpack-head_num = 1.

wa_it_objpack-body_start = 1.

wa_it_objpack-body_num = v_lines_bin.

wa_it_objpack-doc_type = text-001. "PDF

wa_it_objpack-obj_name = text-006. "SmartForm

CONCATENATE i_potitle '.pdf' INTO wa_it_objpack-obj_descr.

wa_it_objpack-doc_size = v_lines_bin * 255.

APPEND wa_it_objpack TO it_objpack.

  • Document information.

CLEAR wa_it_reclist.

  • e-mail receivers.

SELECT SINGLE smtp_addr FROM adr6 INTO v_mailaddr "#EC *

WHERE addrnumber = i_addrnum.

wa_it_reclist-receiver = v_mailaddr.

wa_it_reclist-express = c_X. " X

wa_it_reclist-rec_type = c_U. "U -> Internet address

APPEND wa_it_reclist TO it_reclist.

  • Sending Mail

CALL FUNCTION 'SO_NEW_DOCUMENT_ATT_SEND_API1'

EXPORTING

document_data = w_doc_chng

put_in_outbox = c_X "X

  • COMMIT_WORK =

  • IMPORTING

  • SENT_TO_ALL =

  • NEW_OBJECT_ID =

TABLES

packing_list = it_objpack

object_header = wa_objhead

contents_bin = it_objbin

contents_txt = it_objtxt

  • CONTENTS_HEX =

  • OBJECT_PARA =

  • OBJECT_PARB =

receivers = it_reclist

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.

sy-msgid = text-008. "some msg

sy-msgty = c_msgtype. " E

sy-msgno = 041.

sy-msgv1 = ' '.

sy-msgv2 = ' '.

sy-msgv3 = ' '.

sy-msgv4 = ' '.

EXIT.

ENDIF.

ENDIF.

ENDFUNCTION.

Regards,,

Nikita

0 Kudos

hii i have written code like this . there is no syntax error but on execution it is giving .........pls ckh

*"----


""Local Interface:

*" IMPORTING

*" REFERENCE(EMAIL_ID) TYPE CHAR25

*" REFERENCE(HEADER) TYPE CHAR25

*" REFERENCE(BODY) TYPE CHAR250

*"----


&----


*& Internal Table Declerations *

*& *

&----


*& Internal table decleration for SAPoffice: line, length 255 (soli)

DATA : OBJ_CONT LIKE SOLI OCCURS 1 WITH HEADER LINE, "ForText Content Of Email

*& Internal table decleration for SAPoffice: Structure of the API Recipient List (somlredi1)

RECeiVER LIKE SOMLRECI1 OCCURS 1 WITH HEADER LINE. "For Storing Email Id Of Receipient

&----


*& Variable Declerations *

*& *

&----


DATA: DOC_DATA LIKE SODOCCHGI1, "Data of an object which can be changed

OBJECT_ID LIKE SOODK. "SAPoffice: Definition of an Object (Key Part)

obj_cont = body.

receiver-receiver = email_id.

receiver-rec_type = 'U'.

DOC_DATA-OBJ_DESCR = header .

CALL FUNCTION 'SO_NEW_DOCUMENT_SEND_API1'

EXPORTING

DOCUMENT_DATA = doc_data

  • DOCUMENT_TYPE = 'RAW'

  • PUT_IN_OUTBOX = ' '

  • COMMIT_WORK = ' '

  • IMPORTING

  • SENT_TO_ALL =

NEW_OBJECT_ID = object_id

TABLES

  • OBJECT_HEADER =

OBJECT_CONTENT = OBJ_CONT

  • CONTENTS_HEX =

  • OBJECT_PARA =

  • OBJECT_PARB =

RECEIVERS = RECEIVER

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

ENDIF.

ENDFUNCTION.

0 Kudos

can any one check this code its giving error messege not sent