SAP R/3 форум ABAP консультантов
Russian ABAP Developer's Club

Home - FAQ - Search - Memberlist - Usergroups - Profile - Log in to check your private messages - Register - Log in - English
Blogs - Weblogs News

To send email with PDF attachment



 
Post new topic   Reply to topic    Russian ABAP Developer's Club Forum Index -> Programming Techniques | Приемы программирования -> Mail
View previous topic :: View next topic  
Author Message
admin
Администратор
Администратор



Joined: 01 Sep 2007
Posts: 1639

PostPosted: Fri Jan 25, 2008 12:56 pm    Post subject: To send email with PDF attachment Reply with quote

Code:
REPORT z34332_mail_with_attachment1.

TYPES: BEGIN OF t_mara,
matnr TYPE mara-matnr,
matkl TYPE mara-matkl,
mtart TYPE mara-mtart,
meins TYPE mara-meins,
END OF t_mara.

DATA: gt_mara TYPE TABLE OF t_mara,
wa_mara LIKE LINE OF gt_mara,
it_packing_list TYPE TABLE OF sopcklsti1,
wa_packing_list LIKE LINE OF it_packing_list,
it_receivers TYPE TABLE OF somlreci1,
wa_receivers LIKE LINE OF it_receivers,
it_mailbody TYPE TABLE OF solisti1,
wa_mailbody LIKE LINE OF it_mailbody,
it_attachment TYPE TABLE OF solisti1,
wa_attachment LIKE LINE OF it_attachment.

DATA: la_doc TYPE sodocchgi1.

CONSTANTS:
con_tab TYPE c VALUE cl_abap_char_utilities=>horizontal_tab,
con_cret TYPE c VALUE cl_abap_char_utilities=>cr_lf.

GET material
select matnr matkl mtart meins
into table gt_mara
from mara
up to 25 rows.

* Populate the subject/generic message attributes
  la_doc-obj_langu = sy-langu.
  la_doc-obj_descr = 'Material Details' . "Mail Header
  la_doc-sensitivty = 'F'.
  la_doc-doc_size = 1.

* Add the recipients email address
  CLEAR wa_receivers.
  REFRESH it_receivers.
  wa_receivers-receiver = 'PCSDEVL'.
  wa_receivers-rec_type = 'U'.
  wa_receivers-com_type = 'INT'.
  wa_receivers-notif_del = 'X'.
  wa_receivers-notif_ndel = 'X'.
  APPEND wa_receivers TO it_receivers.


* Mail Body
  CLEAR wa_mailbody.
  REFRESH it_mailbody.
  wa_mailbody-line = 'Please find the attachment'.
  APPEND wa_mailbody TO it_mailbody.

* Mail attachmwnt
  CLEAR wa_attachment.
  REFRESH it_attachment.

  CONCATENATE 'MATNR' 'MATKL' 'MTART' 'MEINS'
  INTO wa_attachment SEPARATED BY con_tab.
  CONCATENATE con_cret wa_attachment INTO wa_attachment.
  APPEND wa_attachment TO it_attachment.

  LOOP AT gt_mara INTO wa_mara.
    CONCATENATE wa_mara-matnr wa_mara-matkl
    wa_mara-mtart wa_mara-meins
    INTO wa_attachment SEPARATED BY con_tab.
    CONCATENATE con_cret wa_attachment INTO wa_attachment.
    APPEND wa_attachment TO it_attachment.
  ENDLOOP.

* Describe the body of the message
  CLEAR wa_packing_list.
  REFRESH it_packing_list.
  wa_packing_list-transf_bin = space.
  wa_packing_list-head_start = 1.
  wa_packing_list-head_num = 0.
  wa_packing_list-body_start = 1.
  wa_packing_list-body_num = 1.
  wa_packing_list-doc_type = 'RAW'.
  APPEND wa_packing_list TO it_packing_list.

* Create attachment notification
  wa_packing_list-transf_bin = 'X'.
  wa_packing_list-head_start = 1.
  wa_packing_list-head_num = 1.
  wa_packing_list-body_start = 1.

  DESCRIBE TABLE it_attachment LINES wa_packing_list-body_num.
  wa_packing_list-doc_type = 'XLS'. " To word attachment change this as
  'DOC'
  wa_packing_list-obj_descr = ' '.
  CONCATENATE wa_packing_list-doc_type 'file' INTO
  wa_packing_list-obj_descr
  SEPARATED BY space.
  wa_packing_list-doc_size = wa_packing_list-body_num * 255.
  APPEND wa_packing_list TO it_packing_list.

  CALL FUNCTION 'SO_DOCUMENT_SEND_API1'
  EXPORTING
    document_data = la_doc
    put_in_outbox = 'X'
    sender_address = sy-uname
    sender_address_type = 'B'
    commit_work = 'X'
  IMPORTING
*   SENT_TO_ALL =
*   NEW_OBJECT_ID =
*   SENDER_ID =
  TABLES
    packing_list = it_packing_list
*   object_header =
  contents_bin = it_attachment
  contents_txt = it_mailbody
*  contents_hex =
*  object_para =
*  object_parb =
    receivers = it_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.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
    WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.


For PDF attachment:
1. you have to create a spool request
2.Convert that spool request to pdf
3. Attach that pdf with amil and send.

First u write Internal table to spool then u can convert the spool to pdf....see the below code.. hope it helps...

Code:

  DATA: BEGIN OF i_mara OCCURS 0,
  matnr LIKE mara-matnr.
  DATA: END OF i_mara.

  DATA: v_dest LIKE tsp01-rqdest,
  v_handle LIKE sy-tabix,
  v_spool_id LIKE tsp01-rqident,
  v_rc TYPE c,
  v_errmessage(100) TYPE c,
  v_text(70) TYPE c.

START-OF-SELECTION.

  SELECT matnr FROM mara INTO TABLE i_mara.

  CALL FUNCTION 'RSPO_OPEN_SPOOLREQUEST'
       EXPORTING
            dest            = 'LOCL'
*            layout          =
*            name            =
*            suffix1         =
*            suffix2         =
*            copies          =
*            prio            =
*            immediate_print =
*            auto_delete     =
*            titleline       =
*            receiver        =
*            division        =
*            authority       =
*            posname         =
*            acttime         =
            lifetime        = '8'
*            append          =
*            coverpage       =
*            codepage        =
*            doctype         =
       importing
            handle          = v_handle
            spoolid         = gd_spool_nr
            rc              = v_rc
            errmessage      = v_errmessage.

  LOOP AT i_mara.
    v_text = i_mara-matnr.
    CALL FUNCTION 'RSPO_WRITE_SPOOLREQUEST'
         EXPORTING
              handle           = v_handle
              text             = v_text
              length           =
              codepage         =
              truncate         =
         importing
              rc               = v_rc
              errmessage       = v_errmessage
         EXCEPTIONS
              handle_not_valid = 1
              OTHERS           = 2.
    IF sy-subrc 0.
      MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
      WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
    ENDIF.

  ENDLOOP.

  CALL FUNCTION 'RSPO_CLOSE_SPOOLREQUEST'
       EXPORTING
            handle           = v_handle
       IMPORTING
            rc               = v_rc
            errmessage       = v_errmessage
       EXCEPTIONS
            handle_not_valid = 1
            OTHERS           = 2.
  IF sy-subrc 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
    WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.

  CALL FUNCTION 'CONVERT_ABAPSPOOLJOB_2_PDF'
       EXPORTING
            src_spoolid              = gd_spool_nr
            no_dialog                = c_no
            dst_device               = c_device
       IMPORTING
            pdf_bytecount            = gd_bytecount
       TABLES
            pdf                      = it_pdf_output
       EXCEPTIONS
            err_no_abap_spooljob     = 1
            err_no_spooljob          = 2
            err_no_permission        = 3
            err_conv_not_possible    = 4
            err_bad_destdevice       = 5
            user_cancelled           = 6
            err_spoolerror           = 7
            err_temseerror           = 8
            err_btcjob_open_failed   = 9
            err_btcjob_submit_failed = 10
            err_btcjob_close_failed  = 11
            OTHERS                   = 12.
  CHECK sy-subrc = 0.


* get the spool number
* Declaration of local variables.
  DATA:
  lv_rq2name LIKE tsp01-rq2name.

  CONCATENATE sy-repid+0(8)
  sy-uname+0(3)
  INTO lv_rq2name SEPARATED BY '_'.

* Get the spool number.
  SELECT * FROM tsp01 WHERE rq2name = lv_rq2name
  ORDER BY rqcretime DESCENDING.
    v_rqident = tsp01-rqident.
    EXIT.
  ENDSELECT.
  IF sy-subrc NE 0.
    CLEAR v_rqident.
  ENDIF
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Russian ABAP Developer's Club Forum Index -> Programming Techniques | Приемы программирования -> Mail All times are GMT + 4 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


All product names are trademarks of their respective companies. SAPNET.RU websites are in no way affiliated with SAP AG.
SAP, SAP R/3, R/3 software, mySAP, ABAP, BAPI, xApps, SAP NetWeaver and any other are registered trademarks of SAP AG.
Every effort is made to ensure content integrity. Use information on this site at your own risk.