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: 

sending a form as a pdf via email with the BCS interface

Former Member
0 Kudos

Hi,

I would like to send a sapscript form as a pdf to a mail recipient using the BCS interface.

I started to investigate the BCS.

When using the function - CLOSE_FORM - I get the OTFDATA table - how do I send it as a pdf file with BCS ?

How different is sending an Ms-Word attachment ?

Thanks so much.

Promise to award points...

Ruthie.

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Hi,

i don´t know what this BCS story is, but I send emails with attachments so:

DATA: len TYPE i,

pos TYPE i,

fsize TYPE i,

stuff(65000),

objpack LIKE sopcklsti1 OCCURS 0 WITH HEADER LINE,

header LIKE solisti1 OCCURS 0 WITH HEADER LINE,

objbin LIKE solisti1 OCCURS 0 WITH HEADER LINE,

contents LIKE solisti1 OCCURS 0 WITH HEADER LINE,

reclist LIKE somlreci1 OCCURS 0 WITH HEADER LINE,

doc_data LIKE sodocchgi1,

pdf_table LIKE tline OCCURS 0 WITH HEADER LINE,

pdf_line(134).

  • Email´s header:

header = 'My message !!'.

APPEND header.

  • Contents of mail:

contents = 'This is a message to inform that you´ve'.

APPEND contents.

contents = 'got an email from me'.

APPEND contents.

contents = 'Please, read it'.

APPEND contents.

DESCRIBE TABLE contents LINES sy-tfill.

  • Attributes of document:

doc_data-obj_name = 'EMAIL'.

doc_data-obj_descr = 'Header line'.

doc_data-obj_langu = sy-langu.

doc_data-obj_expdat = sy-datum.

doc_data-sensitivty = 'F'. " F (functional) / P (private)

" O (confidential) / C (confid. company)

doc_data-doc_size = ( sy-tfill - 1 ) * 255 + STRLEN( contents ).

objpack-transf_bin = ' '.

objpack-head_start = 1.

objpack-head_num = 0.

objpack-body_start = 1.

objpack-body_num = sy-tfill.

objpack-doc_type = 'RAW'.

APPEND objpack.

objpack-doc_type = 'PDF'. "PDF/HTM/TXT

LOOP AT pdf_table INTO pdf_line.

stuff+pos(134) = pdf_line.

ADD 134 TO pos.

ENDLOOP.

pos = 0.

len = strlen( stuff ).

WHILE len > 0.

SUBTRACT 255 FROM len.

objbin = stuff+pos(255).

APPEND objbin.

ADD 255 TO pos.

ENDWHILE.

DESCRIBE TABLE objbin LINES sy-tfill.

objpack-transf_bin = 'X'.

objpack-head_start = 1.

objpack-head_num = 1.

objpack-body_start = 1.

objpack-body_num = sy-tfill.

objpack-obj_name = 'Myattachment'.

objpack-obj_descr = 'Attachment'.

objpack-doc_size = sy-tfill * 255.

APPEND objpack.

  • Receiver list (communication type in ADRC-DEFLT_COMM):

reclist-receiver = myemail.

reclist-rec_type = 'B'. " U = Internet / B = SAP User

APPEND reclist.

  • Send the email

CALL FUNCTION 'SO_NEW_DOCUMENT_ATT_SEND_API1'

EXPORTING

document_data = doc_data

put_in_outbox = 'X'

TABLES

packing_list = objpack

object_header = header

contents_bin = objbin

contents_txt = contents

receivers = reclist

EXCEPTIONS

OTHERS = 99.

IF sy-subrc = 0.

SUBMIT rsconn01 WITH mode = 'INT' AND RETURN.

MESSAGE i100(mj) WITH 'Message has been sent !'.

ELSE.

MESSAGE i100(mj) WITH 'Error:' sy-subrc.

ENDIF.

4 REPLIES 4

Former Member
0 Kudos

Hi,

i don´t know what this BCS story is, but I send emails with attachments so:

DATA: len TYPE i,

pos TYPE i,

fsize TYPE i,

stuff(65000),

objpack LIKE sopcklsti1 OCCURS 0 WITH HEADER LINE,

header LIKE solisti1 OCCURS 0 WITH HEADER LINE,

objbin LIKE solisti1 OCCURS 0 WITH HEADER LINE,

contents LIKE solisti1 OCCURS 0 WITH HEADER LINE,

reclist LIKE somlreci1 OCCURS 0 WITH HEADER LINE,

doc_data LIKE sodocchgi1,

pdf_table LIKE tline OCCURS 0 WITH HEADER LINE,

pdf_line(134).

  • Email´s header:

header = 'My message !!'.

APPEND header.

  • Contents of mail:

contents = 'This is a message to inform that you´ve'.

APPEND contents.

contents = 'got an email from me'.

APPEND contents.

contents = 'Please, read it'.

APPEND contents.

DESCRIBE TABLE contents LINES sy-tfill.

  • Attributes of document:

doc_data-obj_name = 'EMAIL'.

doc_data-obj_descr = 'Header line'.

doc_data-obj_langu = sy-langu.

doc_data-obj_expdat = sy-datum.

doc_data-sensitivty = 'F'. " F (functional) / P (private)

" O (confidential) / C (confid. company)

doc_data-doc_size = ( sy-tfill - 1 ) * 255 + STRLEN( contents ).

objpack-transf_bin = ' '.

objpack-head_start = 1.

objpack-head_num = 0.

objpack-body_start = 1.

objpack-body_num = sy-tfill.

objpack-doc_type = 'RAW'.

APPEND objpack.

objpack-doc_type = 'PDF'. "PDF/HTM/TXT

LOOP AT pdf_table INTO pdf_line.

stuff+pos(134) = pdf_line.

ADD 134 TO pos.

ENDLOOP.

pos = 0.

len = strlen( stuff ).

WHILE len > 0.

SUBTRACT 255 FROM len.

objbin = stuff+pos(255).

APPEND objbin.

ADD 255 TO pos.

ENDWHILE.

DESCRIBE TABLE objbin LINES sy-tfill.

objpack-transf_bin = 'X'.

objpack-head_start = 1.

objpack-head_num = 1.

objpack-body_start = 1.

objpack-body_num = sy-tfill.

objpack-obj_name = 'Myattachment'.

objpack-obj_descr = 'Attachment'.

objpack-doc_size = sy-tfill * 255.

APPEND objpack.

  • Receiver list (communication type in ADRC-DEFLT_COMM):

reclist-receiver = myemail.

reclist-rec_type = 'B'. " U = Internet / B = SAP User

APPEND reclist.

  • Send the email

CALL FUNCTION 'SO_NEW_DOCUMENT_ATT_SEND_API1'

EXPORTING

document_data = doc_data

put_in_outbox = 'X'

TABLES

packing_list = objpack

object_header = header

contents_bin = objbin

contents_txt = contents

receivers = reclist

EXCEPTIONS

OTHERS = 99.

IF sy-subrc = 0.

SUBMIT rsconn01 WITH mode = 'INT' AND RETURN.

MESSAGE i100(mj) WITH 'Message has been sent !'.

ELSE.

MESSAGE i100(mj) WITH 'Error:' sy-subrc.

ENDIF.

Former Member
0 Kudos

Ruthie,

Use the Form below.

Just pass the OTF internal table to this FORM.

Change the sending format and sender accordingly.

Rgds,

TM.

*---------------------------------------------------------------------* 
*       FORM MAIL_OBJECT                                              * 
*---------------------------------------------------------------------* 
*       This routine receives OTF data. OTF data is converted to PDF 
*       format and send to the Partner's email address 
*---------------------------------------------------------------------* 
FORM mail_object TABLES otf_data STRUCTURE itcoo . 

  DATA: pdf_size TYPE i,                             " PDF Size 
        pdf_itab_size TYPE i,                        " Attachment size 
        mailtxt_size TYPE i,                         " Text in mail size 
        l_vbeln LIKE vbdka-vbeln.                    " Order Doc 
  DATA: 
  it_mailtxt LIKE solisti1 OCCURS 0 WITH HEADER LINE,    " Mail Text 
  it_pdf TYPE TABLE OF tline WITH HEADER LINE,           " OTF output 
  it_mailpack LIKE sopcklsti1 OCCURS 0 WITH HEADER LINE, " Dist details 
  it_mailhead LIKE solisti1   OCCURS  1 WITH HEADER LINE," Header data 
  it_reclist LIKE somlreci1 OCCURS 0 WITH HEADER LINE,   " Rec List 
  it_pdfdata LIKE solix OCCURS 0 WITH HEADER LINE.  " Attachment data 
  DATA: it_doc_att LIKE sodocchgi1.                 " Attri of new doc 
  DATA: BEGIN OF it_pdfout OCCURS 0,                " PDF in 255 length 
           tline TYPE char255, 
        END OF it_pdfout. 
* Sales doc and Customer 
  DATA: BEGIN OF i_vbeln OCCURS 0, 
          vbeln LIKE vbpa-vbeln,       " Sales Document 
          adrnr LIKE vbpa-adrnr,       " Customer 
        END   OF i_vbeln. 

* Sender Address no and SMTP address 
  DATA: BEGIN OF i_addrs OCCURS 0, 
          addrnumber LIKE adr6-smtp_addr, 
          smtp_addr  LIKE adr6-smtp_addr, 
        END   OF i_addrs. 

* Convert OTF to PDF 
  CALL FUNCTION 'CONVERT_OTF' 
    EXPORTING 
      format       = 'PDF' 
    IMPORTING 
      bin_filesize = pdf_size 
    TABLES 
      otf          = otf_data 
      lines        = it_pdf. 

* Make each line 255 characters 
  CALL FUNCTION 'SX_TABLE_LINE_WIDTH_CHANGE' 
    TABLES 
      content_in  = it_pdf 
      content_out = it_pdfout. 

* Create the PDF File 
  CLEAR it_pdfdata. 
  REFRESH it_pdfdata. 
*  it_pdfdata[] = it_pdfout[]. 
  LOOP AT it_pdfout. 
    MOVE it_pdfout-tline TO it_pdfdata-line. 
    APPEND it_pdfdata. 
    CLEAR it_pdfdata. 
  ENDLOOP. 
  DESCRIBE TABLE it_pdfdata LINES pdf_itab_size. 

* Text in the mail. 
  it_mailtxt-line  = 'ORDER ACKNOWLEDGEMENT'. 
  APPEND it_mailtxt. 
  it_mailtxt-line  = ' This is a test mail,  Line Number--1'. 
  APPEND it_mailtxt. 
  it_mailtxt-line = ' This is a test mail,  Line Number--2' & 
                    ' This is a test mail,  Line Number--2'. 
  APPEND it_mailtxt. 
  it_mailtxt-line = ' This is a test mail,  Line Number--3' & 
                    ' This is a test mail,  Line Number--3' & 
                    ' This is a test mail,  Line Number--3'. 
  APPEND it_mailtxt. 
  it_mailtxt-line = ' This is a test mail,  Line Number--4' & 
                    ' This is a test mail,  Line Number--4' & 
                    ' This is a test mail,  Line Number--4' & 
                    ' This is a test mail,  Line Number--4'. 
  APPEND it_mailtxt. 
  it_mailtxt-line = ' This is a test mail,  Line Number--5' & 
                    ' This is a test mail,  Line Number--5' & 
                    ' This is a test mail,  Line Number--5' & 
                    ' This is a test mail,  Line Number--5' & 
                    ' This is a test mail,  Line Number--5'. 
  APPEND it_mailtxt. 
  DESCRIBE TABLE it_mailtxt LINES mailtxt_size. 

* Document Number for Output 
  CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' 
    EXPORTING 
      input  = vbdka-vbeln 
    IMPORTING 
      output = l_vbeln. 


* Attributes of new doc 
  CONCATENATE 'Order' space 'Acknowledgement' space l_vbeln 
              INTO it_doc_att-obj_descr SEPARATED BY space. 
  it_doc_att-sensitivty = 'F'. 
  it_doc_att-doc_size   = mailtxt_size * 255. 

* Create Pack to text in mail body. 
  CLEAR it_mailpack-transf_bin. 
  it_mailpack-head_start   = 1. 
  it_mailpack-head_num     = 0. 
  it_mailpack-body_start   = 1. 
  it_mailpack-body_num     = mailtxt_size. 
  it_mailpack-doc_type     = 'RAW'. 
  APPEND it_mailpack. 

* Create Pack to PDF Attach. 
  it_mailpack-transf_bin   = 'X'. 
  it_mailpack-head_start   = 1. 
  it_mailpack-head_num     = 1. 
  it_mailpack-body_start   = 1. 
  it_mailpack-body_num     = pdf_itab_size. 
  it_mailpack-doc_type     = 'PDF'. 
  CONCATENATE l_vbeln '.pdf' INTO it_mailpack-obj_name. 
  CONCATENATE 'Order Ack' space l_vbeln INTO it_mailpack-obj_descr. 
  it_mailpack-doc_size     = pdf_itab_size * 255. 
  APPEND it_mailpack. 

*Get email addresses based on Sales document. 
  SELECT vbeln adrnr INTO TABLE i_vbeln 
         FROM vbpa 
         WHERE vbeln = vbdka-vbeln AND 
               parvw = nast-parvw. 
  IF NOT i_vbeln[] IS INITIAL. 
    SELECT addrnumber smtp_addr INTO TABLE i_addrs 
           FROM adr6 FOR ALL ENTRIES IN i_vbeln 
           WHERE addrnumber =  i_vbeln-adrnr AND 
                 smtp_addr NE space. 
  ENDIF. 

  IF i_addrs[] IS NOT INITIAL. 
    LOOP AT i_addrs. 
      it_reclist-receiver   = i_addrs-smtp_addr. 
      it_reclist-express    = 'X'. 
      it_reclist-rec_type   = 'U'. 
      it_reclist-notif_del  = 'X'. " request delivery notification 
      it_reclist-notif_ndel = 'X'. " request not delivered notification 
      APPEND it_reclist. 
      CLEAR: i_addrs. 
    ENDLOOP. 
  ENDIF. 

* Call FM to send email 
  CALL FUNCTION 'SO_NEW_DOCUMENT_ATT_SEND_API1' 
    EXPORTING 
      document_data              = it_doc_att 
      put_in_outbox              = 'X' 
    TABLES 
      packing_list               = it_mailpack 
      object_header              = it_mailhead 
      contents_txt               = it_mailtxt 
      contents_hex               = it_pdfdata 
      receivers                  = it_reclist 
    EXCEPTIONS 
      too_many_receivers         = 1 
      document_not_sent          = 2 
      document_type_not_exist    = 3 
      operation_no_authorizationfiltered= 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. 

ENDFORM.                    " MAIL_OBJECT

Former Member
0 Kudos

Thanks Thomas and Jorge.

But I am trying to send the email through BCS because

in sap note 190669 it is recommended not to use the API1

interface but to use BCS interface instead.

Thanks.

Ruthie.

Former Member
0 Kudos

Hai

Go through the following Code that will help you

REPORT ZRICH_0003.

DATA: ITCPO LIKE ITCPO,

TAB_LINES LIKE SY-TABIX.

  • Variables for EMAIL functionality

DATA: MAILDATA LIKE SODOCCHGI1.

DATA: MAILPACK LIKE SOPCKLSTI1 OCCURS 2 WITH HEADER LINE.

DATA: MAILHEAD LIKE SOLISTI1 OCCURS 1 WITH HEADER LINE.

DATA: MAILBIN LIKE SOLISTI1 OCCURS 10 WITH HEADER LINE.

DATA: MAILTXT LIKE SOLISTI1 OCCURS 10 WITH HEADER LINE.

DATA: MAILREC LIKE SOMLREC90 OCCURS 0 WITH HEADER LINE.

DATA: SOLISTI1 LIKE SOLISTI1 OCCURS 0 WITH HEADER LINE.

PERFORM SEND_FORM_VIA_EMAIL.

************************************************************************

  • FORM SEND_FORM_VIA_EMAIL *

************************************************************************

FORM SEND_FORM_VIA_EMAIL.

CLEAR: MAILDATA, MAILTXT, MAILBIN, MAILPACK, MAILHEAD, MAILREC.

REFRESH: MAILTXT, MAILBIN, MAILPACK, MAILHEAD, MAILREC.

  • Creation of the document to be sent File Name

MAILDATA-OBJ_NAME = 'TEST'.

  • Mail Subject

MAILDATA-OBJ_DESCR = 'Subject'.

  • Mail Contents

MAILTXT-LINE = 'Here is your file'.

APPEND MAILTXT.

  • Prepare Packing List

PERFORM PREPARE_PACKING_LIST.

  • Set recipient - email address here!!!

MAILREC-RECEIVER = 'itsme@whatever.com'.

MAILREC-REC_TYPE = 'U'.

APPEND MAILREC.

  • Sending the document

CALL FUNCTION 'SO_NEW_DOCUMENT_ATT_SEND_API1'

EXPORTING

DOCUMENT_DATA = MAILDATA

PUT_IN_OUTBOX = ' '

TABLES

PACKING_LIST = MAILPACK

OBJECT_HEADER = MAILHEAD

CONTENTS_BIN = MAILBIN

CONTENTS_TXT = MAILTXT

RECEIVERS = MAILREC

EXCEPTIONS

TOO_MANY_RECEIVERS = 1

DOCUMENT_NOT_SENT = 2

OPERATION_NO_AUTHORIZATION = 4

OTHERS = 99.

ENDFORM.

************************************************************************

  • Form PREPARE_PACKING_LIST

************************************************************************

FORM PREPARE_PACKING_LIST.

CLEAR: MAILPACK, MAILBIN, MAILHEAD.

REFRESH: MAILPACK, MAILBIN, MAILHEAD.

DESCRIBE TABLE MAILTXT LINES TAB_LINES.

READ TABLE MAILTXT INDEX TAB_LINES.

MAILDATA-DOC_SIZE = ( TAB_LINES - 1 ) * 255 + STRLEN( MAILTXT ).

  • Creation of the entry for the compressed document

CLEAR MAILPACK-TRANSF_BIN.

MAILPACK-HEAD_START = 1.

MAILPACK-HEAD_NUM = 0.

MAILPACK-BODY_START = 1.

MAILPACK-BODY_NUM = TAB_LINES.

MAILPACK-DOC_TYPE = 'RAW'.

APPEND MAILPACK.

  • Creation of the document attachment

  • This form gets the OTF code from the SAPscript form.

  • If you already have your OTF code, I believe that you may

  • be able to skip this form. just do the following code, looping thru

  • your SOLISTI1 and updating MAILBIN.

PERFORM GET_OTF_CODE.

LOOP AT SOLISTI1.

MOVE-CORRESPONDING SOLISTI1 TO MAILBIN.

APPEND MAILBIN.

ENDLOOP.

DESCRIBE TABLE MAILBIN LINES TAB_LINES.

MAILHEAD = 'TEST.OTF'.

APPEND MAILHEAD.

    • Creation of the entry for the compressed attachment

MAILPACK-TRANSF_BIN = 'X'.

MAILPACK-HEAD_START = 1.

MAILPACK-HEAD_NUM = 1.

MAILPACK-BODY_START = 1.

MAILPACK-BODY_NUM = TAB_LINES.

MAILPACK-DOC_TYPE = 'OTF'.

MAILPACK-OBJ_NAME = 'TEST'.

MAILPACK-OBJ_DESCR = 'Subject'.

MAILPACK-DOC_SIZE = TAB_LINES * 255.

APPEND MAILPACK.

ENDFORM.

************************************************************************

  • Form GET_OTF_CODE

************************************************************************

FORM GET_OTF_CODE.

DATA: BEGIN OF OTF OCCURS 0.

INCLUDE STRUCTURE ITCOO .

DATA: END OF OTF.

DATA: ITCPO LIKE ITCPO.

DATA: ITCPP LIKE ITCPP.

CLEAR ITCPO.

ITCPO-TDGETOTF = 'X'.

  • Start writing OTF code

CALL FUNCTION 'OPEN_FORM'

EXPORTING

FORM = 'ZTEST_FORM'

LANGUAGE = SY-LANGU

OPTIONS = ITCPO

DIALOG = ' '

EXCEPTIONS

OTHERS = 1.

CALL FUNCTION 'START_FORM'

EXCEPTIONS

ERROR_MESSAGE = 01

OTHERS = 02.

CALL FUNCTION 'WRITE_FORM'

EXPORTING

WINDOW = 'MAIN'

EXCEPTIONS

ERROR_MESSAGE = 01

OTHERS = 02.

  • Close up Form and get OTF code

CALL FUNCTION 'END_FORM'

EXCEPTIONS

ERROR_MESSAGE = 01

OTHERS = 02.

MOVE-CORRESPONDING ITCPO TO ITCPP.

CALL FUNCTION 'CLOSE_FORM'

IMPORTING

RESULT = ITCPP

TABLES

OTFDATA = OTF

EXCEPTIONS

OTHERS = 1.

  • Move OTF code to structure SOLI form email

CLEAR SOLISTI1. REFRESH SOLISTI1.

LOOP AT OTF.

SOLISTI1-LINE = OTF.

APPEND SOLISTI1.

ENDLOOP.

ENDFORM.

Regards

Sreeni