Skip to Content
avatar image
Former Member

design a function module to send email

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.

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

4 Answers

  • Best Answer
    avatar image
    Former Member
    Oct 16, 2008 at 06:14 AM

    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.

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Oct 16, 2008 at 06:12 AM

    check the FM

    SO_NEW_DOCUMENT_ATT_SEND_API1

    SO_NEW_DOCUMENT_SEND_API1

    these will help you

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Oct 16, 2008 at 08:30 AM

    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 😊

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Oct 16, 2008 at 08:40 AM

    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

    Add comment
    10|10000 characters needed characters exceeded