Skip to Content
author's profile photo Former Member
Former Member

To send TXT file as an attachment through Email

Hi Experts,

I am working on requirement where i have to send a txt file as an attachment.

Could any one suggest me a solution.

Thanks and Regards,

Jeswanth Kadali

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

2 Answers

  • Best Answer
    author's profile photo Former Member
    Former Member
    Posted on Dec 04, 2007 at 04:52 PM

    Hi,

    Use the function module SO_NEW_DOCUMENT_ATT_SEND_API1.

    We find it convenient to use a custom function module as a wrapper for that.

    Here's the code.

    FUNCTION Z_EMAILS_ATTACH.

    *"----


    ""Local interface:

    *" IMPORTING

    *" VALUE(EMAIL_SUBJECT) LIKE SODOCCHGI1-OBJ_DESCR

    *" VALUE(ATTACHMENT_SUBJECT) LIKE SOPCKLSTI1-OBJ_DESCR OPTIONAL

    *" VALUE(ATTACHMENT_DOC_TYPE) TYPE SO_OBJ_TP OPTIONAL

    *" REFERENCE(ATTACHMENT_HEADER) LIKE SOLISTI1 STRUCTURE SOLISTI1

    *" TABLES

    *" EMAIL_BODY STRUCTURE SOLISTI1

    *" EMAIL_ATTACHMENT STRUCTURE SOLISTI1 OPTIONAL

    *" RECEIVERS STRUCTURE SOMLRECI1

    *" EXCEPTIONS

    *" UNKNOWN_COMMUNICATION_TYPE

    *" ERROR_SENDING_MAIL

    *" EMPTY_ATTACHMENT

    *" NO_ATTACHMENT_SUBJECT

    *" USER_HAS_NO_EMAIL_ADDRESS

    *" NO_RECEIVERS

    *"----


    • NOTE

    • Single-testing when the tables passed to the function have reference

    • structure SOLISTI1 meets with a problem.

    • Entering non-blank rows into these tables is impossible, it seems.

    • This is believed to be because the single testing fails to cope with

    • the width of SOLISTI1-LINE [255 char].

    • For single testing, use CVDTLINE as the reference structure - it's

    • only 132 char. Then remember to change it back to SOLISTI1...

    • Based on Z_EMAIL_ATTACH which sends just one mail.

    • 08.06.2007 JNM

    DATA: document LIKE sodocchgi1.

    DATA: packlist LIKE sopcklsti1 OCCURS 0 WITH HEADER LINE.

    DATA: contents LIKE solisti1 OCCURS 0 WITH HEADER LINE.

    DATA: header LIKE solisti1 OCCURS 0 WITH HEADER LINE.

    DATA: RECVLIST LIKE SOMLRECI1 OCCURS 0 WITH HEADER LINE.

    DATA: NEXT_ROW LIKE SY-TABIX.

    DATA: lines LIKE sy-tabix.

    DATA: LAST_LINE_LENGTH TYPE I.

    DATA: d_doc_size LIKE packlist-doc_size.

    data: email_address type ad_smtpadr.

    • A user without an email address cannot email.

    CALL FUNCTION 'Z_USER_EMAIL_ADDRESS'

    EXPORTING

    USER_NAME = SY-UNAME

    IMPORTING

    EMAIL_ADDRESS = email_address

    EXCEPTIONS

    UNKNOWN_USER = 1

    NO_ADDRESS_KEY = 2

    NO_ADDRESS_DATA = 3

    NO_EMAIL_ADDRESS = 4

    OTHERS = 5.

    IF SY-SUBRC <> 0.

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

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

    raising USER_HAS_NO_EMAIL_ADDRESS.

    ENDIF.

    • Receivers?

    describe table receivers lines lines.

    if lines eq 0.

    raise no_receivers.

    endif.

    • initialization

    CLEAR: CONTENTS, DOCUMENT, HEADER, PACKLIST, RECVLIST.

    REFRESH: CONTENTS, HEADER, PACKLIST, RECVLIST.

    • attachment?

    DESCRIBE TABLE EMAIL_ATTACHMENT LINES LINES.

    IF LINES EQ 0 AND ATTACHMENT_SUBJECT CN SPACE.

    RAISE EMPTY_ATTACHMENT.

    ENDIF.

    IF LINES NE 0 AND ATTACHMENT_SUBJECT CO SPACE.

    RAISE NO_ATTACHMENT_SUBJECT.

    ENDIF.

    • email body

    concatenate 'SAP client:'

    sy-host sy-sysid sy-mandt

    into contents separated by space.

    append contents.

    clear contents.

    append contents.

    APPEND LINES OF EMAIL_BODY TO CONTENTS.

    • header - row for body

    HEADER = 'BODY HEADER'.

    APPEND HEADER.

    • packing list - row for body

    PACKLIST-TRANSF_BIN = SPACE.

    PACKLIST-HEAD_START = 1.

    PACKLIST-HEAD_NUM = 1.

    PACKLIST-BODY_START = 1.

    DESCRIBE TABLE CONTENTS LINES PACKLIST-BODY_NUM.

    NEXT_ROW = 1 + PACKLIST-BODY_NUM.

    *packlist-doc_type = 'EXT'.

    PACKLIST-DOC_TYPE = 'RAW'.

    APPEND PACKLIST.

    IF ATTACHMENT_SUBJECT CN SPACE. " if there's an attachment

    • attachment into contents

    APPEND LINES OF EMAIL_ATTACHMENT TO CONTENTS.

    DESCRIBE TABLE EMAIL_ATTACHMENT LINES LINES.

    READ TABLE EMAIL_ATTACHMENT INDEX LINES.

    LAST_LINE_LENGTH = STRLEN( EMAIL_ATTACHMENT ).

    • header - attachment

    if attachment_header is initial.

    HEADER = 'ATTACH'.

    else.

    header = attachment_header.

    endif.

    APPEND HEADER.

    • packing list - row for attachment

    CLEAR PACKLIST.

    case attachment_doc_type.

    • when 'XLS'.

    • PACKLIST-TRANSF_BIN = 'X'.

    when others.

    PACKLIST-TRANSF_BIN = SPACE.

    endcase.

    PACKLIST-HEAD_START = 2.

    PACKLIST-HEAD_NUM = 1.

    PACKLIST-BODY_START = NEXT_ROW.

    if not attachment_doc_type is initial.

    packlist-doc_type = attachment_doc_type.

    else.

    PACKLIST-DOC_TYPE = 'RAW'.

    endif.

    • packlist-doc_type = 'EXT'.

    PACKLIST-OBJ_DESCR = ATTACHMENT_SUBJECT.

    PACKLIST-OBJ_NAME = 'ATTACHMENT'.

    PACKLIST-BODY_NUM = LINES.

    D_DOC_SIZE = LAST_LINE_LENGTH + ( 255 * ( LINES - 1 ) ).

    PACKLIST-DOC_SIZE = D_DOC_SIZE.

    APPEND PACKLIST.

    ENDIF. "attachment

    • document

    document-obj_name = 'EMAIL'.

    DOCUMENT-OBJ_DESCR = EMAIL_SUBJECT.

    document-obj_langu = sy-langu.

    document-obj_expdat = sy-datum.

    document-sensitivty = 'F'.

    document-obj_prio = 9.

    document-no_change = 'X'.

    document-priority = 9.

    document-expiry_dat = sy-datum.

    ADD 1 TO: DOCUMENT-OBJ_EXPDAT, DOCUMENT-EXPIRY_DAT.

    DESCRIBE TABLE contents LINES lines.

    D_DOC_SIZE = LAST_LINE_LENGTH + ( 255 * ( LINES - 1 ) ).

    document-doc_size = d_doc_size.

    • call the mail function

    CALL FUNCTION 'SO_NEW_DOCUMENT_ATT_SEND_API1'

    EXPORTING

    document_data = document

    • put_in_outbox = 'X'

    commit_work = 'X'

    TABLES

    object_header = header

    packing_list = packlist

    contents_txt = contents

    receivers = 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.

    RAISE ERROR_SENDING_MAIL.

    ENDIF.

    ENDFUNCTION.

    Also remember that the SAP user sending the email must have an email address in the user details, so that SAP has something to put in the "From" field when creating the email.

    That's why we use this:

    FUNCTION Z_USER_EMAIL_ADDRESS.

    *"----


    ""Local interface:

    *" IMPORTING

    *" REFERENCE(USER_NAME) TYPE SYUNAME DEFAULT SY-UNAME

    *" EXPORTING

    *" REFERENCE(EMAIL_ADDRESS) TYPE AD_SMTPADR

    *" EXCEPTIONS

    *" UNKNOWN_USER

    *" NO_ADDRESS_KEY

    *" NO_ADDRESS_DATA

    *" NO_EMAIL_ADDRESS

    *"----


    tables:

    adr6,

    usr21,

    usr02.

    • SAP logon data

    select single *

    from usr02

    where bname = user_name.

    if sy-subrc ne 0.

    message i017(ZREP) with

    'SAP user' user_name 'is unknown'

    raising unknown_user.

    endif.

    • SAP user address key

    select single *

    from usr21

    where bname = user_name.

    if sy-subrc ne 0.

    message i017(ZREP) with

    'No address data assigned to SAP user' user_name

    raising no_address_key.

    endif.

    • SAP user address

    select single *

    from adr6

    where addrnumber = usr21-addrnumber and

    persnumber = usr21-persnumber.

    if sy-subrc ne 0.

    message i017(ZREP) with

    'No address data found for SAP user' user_name

    raising no_address_data.

    endif.

    email_address = adr6-smtp_addr.

    if email_address is initial.

    message i017(ZREP) with

    'No address data found for SAP user' user_name

    raising no_email_address.

    endif.

    ENDFUNCTION.

    John

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Dec 04, 2007 at 05:04 PM

    Hello,

    check this link

    http://sap4.com/wiki/index.php?title=ZOUTLOOK

    Bye

    Gabriel

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.