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

Send an email with report output as 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: 1636

PostPosted: Sat Sep 22, 2007 5:13 pm    Post subject: Send an email with report output as PDF attachment Reply with quote

Code:
*-----------------------------------------------------------------------
*-----------------------------------------------------------------------
*
*----------------------------------------------------------------------
*----------------------------------------------------------------------

REPORT z_pdf_mail NO STANDARD PAGE HEADING.

*-----------------DATA DECLARATION FOR SIZE----------------------------

DATA: gd_bytecount TYPE tst01-dsize,
      c_no(1) TYPE c VALUE ' ',
      c_device(4) TYPE c VALUE 'LOCL',
      gd_destination TYPE rlgrap-filename,
      gd_buffer TYPE string.

DATA: gd_subject TYPE sodocchgi1-obj_descr,
      it_mess_bod TYPE TABLE OF solisti1 ,
      it_mess_att TYPE TABLE OF solisti1 ,
      wa_mess_att TYPE solisti1 ,
      wa_mess_bod TYPE solisti1 ,
      gd_sender_type TYPE soextreci1-adr_typ,
      gd_attachment_desc TYPE so_obj_nam,
      gd_attachment_name TYPE so_obj_des.

DATA:  BEGIN OF lw_final OCCURS 10,
       kunnr          LIKE kna1-kunnr,
       addrnumber     LIKE adr6-addrnumber,
       smtp_addr      LIKE adr6-smtp_addr,
       END OF lw_final.

DATA:  lt_final LIKE lw_final OCCURS 10 WITH HEADER LINE.



*--------------- Binary store for PDF-----------------------------------

DATA: it_pdf_output TYPE TABLE OF tline,

      wa_pdf_output TYPE tline.

DATA: gd_recsize TYPE i.

DATA: v_spool LIKE tsp01-rqident.


REFRESH lt_final .


IMPORT lt_final FROM MEMORY ID 'ABCD'.

IMPORT v_spool TO v_spool FROM MEMORY ID 'SUB'.

*-----------------pass this spool number here----------------

CALL FUNCTION 'CONVERT_ABAPSPOOLJOB_2_PDF'
     EXPORTING
          src_spoolid              = v_spool  "sy-spono
          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.


*------------ Transfer the 132-long strings to 255-long strings---------

LOOP AT it_pdf_output INTO wa_pdf_output.

  TRANSLATE wa_pdf_output USING ' ~'.

  CONCATENATE gd_buffer wa_pdf_output INTO gd_buffer.

ENDLOOP.

TRANSLATE gd_buffer USING '~ '.

DO.

  wa_mess_att = gd_buffer.

  APPEND wa_mess_att TO it_mess_att.

  SHIFT gd_buffer LEFT BY 255 PLACES.

  IF gd_buffer IS INITIAL.

    EXIT.

  ENDIF.

ENDDO.

DESCRIBE TABLE it_mess_att LINES gd_recsize.

CHECK gd_recsize > 0.

REFRESH it_mess_bod.

*-------------- Default subject matter---------------------

gd_subject = 'A/C Statement '.

READ TABLE lt_final INDEX 1.

gd_attachment_desc = lt_final-kunnr.

CONCATENATE 'attach_name' ' ' INTO gd_attachment_name.

wa_mess_bod = 'Dear Sir,'.

APPEND wa_mess_bod TO it_mess_bod.

wa_mess_bod = ' '.

APPEND wa_mess_bod TO it_mess_bod.

wa_mess_bod = 'Your Account statement is attached herewith :'.

Append WA_MESS_BOD to it_mess_bod.

*wa_mess_bod = 'From date to date'.

*Append wa_mess_bod to it_mess_bod.
wa_mess_bod = ' '.

Append WA_MESS_BOD to it_mess_bod.

wa_mess_bod = 'Regards,'.

APPEND wa_mess_bod TO it_mess_bod.

wa_mess_bod = ' '.

Append WA_MESS_BOD to it_mess_bod.

wa_mess_bod = 'Note: System generated mail do not reply'.

wa_mess_bod = ' '.

Append WA_MESS_BOD to it_mess_bod.



append WA_MESS_BOD to it_mess_bod.

*-------------If no sender specified - default blank--------


DATA: p_sender(40) TYPE c.

p_sender = 'sap.sd@relianceada.com'.

IF p_sender EQ space.

  gd_sender_type = space.

ELSE.

  gd_sender_type = 'INT'.

ENDIF.

DATA: ld_error TYPE sy-subrc,

      ld_reciever TYPE sy-subrc,

      ld_mtitle TYPE sodocchgi1-obj_descr,

      ld_email TYPE somlreci1-receiver,

      ld_format TYPE so_obj_tp ,

      ld_attdescription TYPE so_obj_nam ,

      ld_attfilename TYPE so_obj_des ,

      ld_sender_address TYPE soextreci1-receiver,

      ld_sender_address_type TYPE soextreci1-adr_typ,

      ld_receiver TYPE sy-subrc.

DATA: t_packing_list TYPE TABLE OF sopcklsti1,

      wa_packing_list TYPE sopcklsti1,

      t_contents TYPE TABLE OF solisti1 ,

      t_receivers TYPE TABLE OF somlreci1 ,

      wa_receivers TYPE somlreci1 ,

      t_attachment TYPE TABLE OF solisti1 ,

      t_object_header TYPE TABLE OF solisti1 ,

      w_cnt TYPE i,

      w_sent_all(1) TYPE c,

      w_doc_data TYPE sodocchgi1.


READ TABLE lt_final INDEX 1.

ld_email = lt_final-smtp_addr. " 'vamsi.bethapudi@relianceada.com'.
"p_email.

ld_mtitle = 'Example .xls documnet attachment'.

ld_format = 'PDF'.

ld_attdescription = gd_attachment_desc.

ld_attfilename = gd_attachment_name.

ld_sender_address = p_sender.

ld_sender_address_type = gd_sender_type.



*--------------------- Fill the document data--------------------.

w_doc_data-doc_size = 1.

*----------------- Populate the subject/generic message attributes

w_doc_data-obj_langu = sy-langu.

w_doc_data-obj_name = 'SAPRPT'.

w_doc_data-obj_descr = ld_mtitle .

w_doc_data-sensitivty = 'F'.

*----------------- Fill the document data and get size of attachment

CLEAR w_doc_data.

READ TABLE it_mess_att INTO wa_mess_att

INDEX w_cnt.

w_doc_data-doc_size =
( w_cnt - 1 ) * 255 + STRLEN( wa_mess_att ).

w_doc_data-obj_langu = sy-langu.

w_doc_data-obj_name = 'SAPRPT'.

w_doc_data-obj_descr = ld_mtitle.

w_doc_data-sensitivty = 'F'.

CLEAR t_attachment.

REFRESH t_attachment.

t_attachment[] = it_mess_att[].

*--------------- Describe the body of the message--------------------

CLEAR t_packing_list.

REFRESH t_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.

DESCRIBE TABLE it_mess_bod LINES wa_packing_list-body_num.

wa_packing_list-doc_type = 'RAW'.

APPEND wa_packing_list TO t_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 t_attachment LINES wa_packing_list-body_num.

wa_packing_list-doc_type = ld_format.

wa_packing_list-obj_descr = ld_attdescription.

wa_packing_list-obj_name = ld_attfilename.

wa_packing_list-doc_size = wa_packing_list-body_num * 255.

APPEND wa_packing_list TO t_packing_list.

*----------- Add the recipients email address---------------------

CLEAR t_receivers.

REFRESH t_receivers.

wa_receivers-receiver = ld_email.

wa_receivers-rec_type = 'U'.

wa_receivers-com_type = 'INT'.

wa_receivers-notif_del = 'X'.

wa_receivers-notif_ndel = 'X'.

APPEND wa_receivers TO t_receivers.

CALL FUNCTION 'SO_DOCUMENT_SEND_API1'
     EXPORTING
          document_data              = w_doc_data
          put_in_outbox              = 'X'
          sender_address             = ld_sender_address
          sender_address_type        = ld_sender_address_type
          commit_work                = 'X'
     IMPORTING
          sent_to_all                = w_sent_all
     TABLES
          packing_list               = t_packing_list
          contents_bin               = t_attachment
          contents_txt               = it_mess_bod
          receivers                  = t_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.

*----------- Populate zerror return code---------------------------

ld_error = sy-subrc.

*----------- Populate zreceiver return code------------------------

LOOP AT t_receivers INTO wa_receivers.

  ld_receiver = wa_receivers-retrn_code.

ENDLOOP.
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.