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: 

How at send a Word document as an email attachment from an ABAP program?

Former Member
0 Kudos

Hi All,

I'm developing an application that has to send an email message along with a Word document. The Word document is a business document (Ex. Application form) with tables and specific formatting. I was able to successfully send an email using the Function module 'SO_OBJECT_SEND'. However, I was unable attach the Word document to the email.

Based on the discussions previously posted on a similar topic I tried different approaches.

1) Function module SO_DOCUMENT_REPOSITORY_MANAGER

- A window pops up requesting Recipient email ids. I couldn't get the window suppressed.

2) Function module SO_NEW_DOCUMENT_ATT_SEND_API1

- Since the attachment is a pre-formatted Word Document and not an Excel or Text document, couldn't process the contents into the internal table

I would appreciate if any of you could shed some light on how to send an email with text in the body and MS Word document attached

Thanks,

Sud

8 REPLIES 8

Former Member
0 Kudos

Hey Sud;

Check out these two weblogs:

/people/thomas.jung3/blog/2004/09/07/sending-e-mail-from-abap--version-46d-and-lower--api-interface

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

and OSS note 190895. It has some really good examples on creating and sending attachments in different formats.

Cheers,

John

Former Member
0 Kudos

Here is the sample code:

*Sending mail with attachment

  • This program will allow you to send email with attachment.

  • First, specify the attachment file from your local hardisk and execute.

  • Next, specify the sender email address and click the send button.

report y_cr17_mail.

data method1 like sy-ucomm.

data g_user like soudnamei1.

data g_user_data like soudatai1.

data g_owner like soud-usrnam.

data g_receipients like soos1 occurs 0 with header line.

data g_document like sood4 .

data g_header like sood2.

data g_folmam like sofm2.

data g_objcnt like soli occurs 0 with header line.

data g_objhead like soli occurs 0 with header line.

data g_objpara like selc occurs 0 with header line.

data g_objparb like soop1 occurs 0 with header line.

data g_attachments like sood5 occurs 0 with header line.

data g_references like soxrl occurs 0 with header line.

data g_authority like sofa-usracc.

data g_ref_document like sood4.

data g_new_parent like soodk.

data: begin of g_files occurs 10 ,

text(4096) type c,

end of g_files.

data : fold_number(12) type c,

fold_yr(2) type c,

fold_type(3) type c.

parameters ws_file(4096) type c default 'c:\debugger.txt'.

  • Can me any file fromyour pc ....either xls or word or ppt etc ...

g_user-sapname = sy-uname.

call function 'SO_USER_READ_API1'

exporting

user = g_user

  • PREPARE_FOR_FOLDER_ACCESS = ' '

importing

user_data = g_user_data

  • EXCEPTIONS

  • USER_NOT_EXIST = 1

  • PARAMETER_ERROR = 2

  • X_ERROR = 3

  • OTHERS = 4

.

if sy-subrc <> 0.

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

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

endif.

fold_type = g_user_data-outboxfol+0(3).

fold_yr = g_user_data-outboxfol+3(2).

fold_number = g_user_data-outboxfol+5(12).

clear g_files.

refresh : g_objcnt,

g_objhead,

g_objpara,

g_objparb,

g_receipients,

g_attachments,

g_references,

g_files.

method1 = 'SAVE'.

g_document-foltp = fold_type.

g_document-folyr = fold_yr.

g_document-folno = fold_number.

g_document-objtp = g_user_data-object_typ.

*g_document-OBJYR = '27'.

*g_document-OBJNO = '000000002365'.

*g_document-OBJNAM = 'MESSAGE'.

g_document-objdes = 'sap-img.com testing by program'.

g_document-folrg = 'O'.

*g_document-okcode = 'CHNG'.

g_document-objlen = '0'.

g_document-file_ext = 'TXT'.

g_header-objdes = 'sap-img.com testing by program'.

g_header-file_ext = 'TXT'.

call function 'SO_DOCUMENT_REPOSITORY_MANAGER'

exporting

method = method1

office_user = sy-uname

ref_document = g_ref_document

new_parent = g_new_parent

importing

authority = g_authority

tables

objcont = g_objcnt

objhead = g_objhead

objpara = g_objpara

objparb = g_objparb

recipients = g_receipients

attachments = g_attachments

references = g_references

files = g_files

changing

document = g_document

header_data = g_header

  • FOLMEM_DATA =

  • RECEIVE_DATA =

.

  • File from the pc to send...

method1 = 'ATTCREATEFROMPC'.

g_files-text = ws_file.

append g_files.

call function 'SO_DOCUMENT_REPOSITORY_MANAGER'

exporting

method = method1

office_user = g_owner

ref_document = g_ref_document

new_parent = g_new_parent

importing

authority = g_authority

tables

objcont = g_objcnt

objhead = g_objhead

objpara = g_objpara

objparb = g_objparb

recipients = g_receipients

attachments = g_attachments

references = g_references

files = g_files

changing

document = g_document

header_data = g_header

.

method1 = 'SEND'.

g_receipients-recnam = 'MK085'.

g_receipients-recesc = 'B'.

g_receipients-sndex = 'X'.

append g_receipients.

call function 'SO_DOCUMENT_REPOSITORY_MANAGER'

exporting

method = method1

office_user = g_owner

ref_document = g_ref_document

new_parent = g_new_parent

importing

authority = g_authority

tables

objcont = g_objcnt

objhead = g_objhead

objpara = g_objpara

objparb = g_objparb

recipients = g_receipients

attachments = g_attachments

references = g_references

files = g_files

changing

document = g_document

header_data = g_header.

*-- End of Program

Former Member
0 Kudos

Hello,

Try the following code (its long form of sending email w/ attachment);

open dataset fname for input.

if sy-subrc = 0.

do.

read dataset fname into intab length len1.

append intab. clear intab.

add len1 to len.

if sy-subrc <> 0. exit. endif.

enddo.

else.

exit.

endif.

close dataset fname.

  • Get user data from SAPOffice like Outbox folder id ****

CALL FUNCTION 'SO_USER_READ_API1'

IMPORTING

USER_DATA = user_data

EXCEPTIONS

OTHERS = 4.

doc_chng-obj_name = 'Test Word form'.

concatenate XXXX form' 'Test'

into doc_chng-obj_descr separated by space.

doc_chng-obj_langu = sy-langu.

doc_chng-sensitivty = 'O'.

move 'Test' to objcont-line.

append objcont. clear objcont.

describe table objcont lines kount.

doc_chng-doc_size = ( kount - 1 ) * 255 + strlen( objcont ).

  • Create a new document (email) in the outbox folder id ****

  • Object header table should not have anything in it. Because

  • I'm just creating a plain document(email) first and then

  • I call another function for attachment ****

CALL FUNCTION 'SO_DOCUMENT_INSERT_API1'

EXPORTING

FOLDER_ID = user_data-outboxfol

DOCUMENT_DATA = doc_chng

DOCUMENT_TYPE = 'RAW'

importing

document_info = doc_info

TABLES

OBJECT_CONTENT = objcont

EXCEPTIONS

OTHERS = 7.

  • Read the outbox folder and get all the emails created ****

CALL FUNCTION 'SO_FOLDER_READ_API1'

EXPORTING

FOLDER_ID = user_data-outboxfol

TABLES

FOLDER_CONTENT = fol_cont

EXCEPTIONS

OTHERS = 4.

  • Get the most recent document(email) from the outbox, that we

  • just created, so that we can sent it to the recipients ****

clear fol_cont.

read table fol_cont with key doc_id = doc_info-doc_id

object_id = doc_info-object_id.

  • Create an attachment for the existing document ****

att_data-obj_name = 'XXX Form'.

att_data-obj_descr = 'XXX Form'.

att_header-line = 'XXX FORM'. append att_header.

att_data-obj_langu = sy-langu.

att_data-att_size = len.

att_header-line = '&SO_FORMAT=BIN'. append att_header.

CALL FUNCTION 'SO_ATTACHMENT_INSERT_API1'

EXPORTING

DOCUMENT_ID = fol_cont-doc_id

ATTACHMENT_DATA = att_data

ATTACHMENT_TYPE = 'DOC'

TABLES

ATTACHMENT_HEADER = att_header

ATTACHMENT_CONTENT = intab

EXCEPTIONS

DOCUMENT_NOT_EXIST = 1

ATTACHMENT_TYPE_NOT_EXIST = 2

OPERATION_NO_AUTHORIZATION = 3

PARAMETER_ERROR = 4

OTHERS = 7.

  • After attachment, send the email ****

move: XXX@XXX.COM' to recipients-receiver,

'U' to recipients-rec_type,

'X' to recipients-express.

append recipients. clear recipients.

CALL FUNCTION 'SO_OLD_DOCUMENT_SEND_API1'

EXPORTING

DOCUMENT_ID = fol_cont-doc_id

TABLES

RECEIVERS = recipients

EXCEPTIONS

OTHERS = 8.

if sy-subrc = 0.

message s398(00) with 'Message sent successfully'.

endif.

Former Member
0 Kudos

Thanks for all your replies. The issue I'm facing is how to attach a preformatted MS Word document in an email. First of all, I'm not sure if this is possible. Secondly, If I have to read this into an internal table, as some of the solutions suggest, how would I do that? Meaning, I know I can do this through GUI_UPLOAD or Open Dataset but which file type and other parameters should be used.

Thanks,

Sud

Former Member
0 Kudos

Thank you all for your responses

Former Member
0 Kudos

Hi all,

i got request to develop background run program which check which staff is due CONFIRM and able to attach confirmation form (Microsoft Word file) to HR and CC staff manager as recipients.

I try almost all relevant FM but still face problem below, please assist.

1) FM SO_DOCUMENT_REPOSITORY_MANAGER

- A window pops up requesting Recipient email ids. I couldn't get the window suppressed.

2) FM SO_NEW_DOCUMENT_ATT_SEND_API1

- Since the attachment is a pre-formatted Word Document and not an Excel or Text document, couldn't process the contents into the internal table (most sample in forum is Excel not word)

Appreciate ABAP folks who have sample code attach a word file will help on this.

Thanks and regards,

Felicek

avinash_babu
Member
0 Kudos

1. Convert OTF to ASCII using convert_otf function module. Output is table of Tlines

2. Convert Tlines to String using FM IDMX_DI_TLINE_INTO_STRING

3. Convert String to XString using SCMS_STRING_TO_XSTRING

4. Convert XString to Binary using SCMS_XSTRING_TO_BINARY

5. Attach the binary content to email.