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 List or OTF spool as an attachment to email using ABAP



 
Post new topic   Reply to topic    Russian ABAP Developer's Club Forum Index -> List Generation, LDB, Spool Process, SAP Query, Report Painter
View previous topic :: View next topic  
Author Message
admin
Администратор
Администратор



Joined: 01 Sep 2007
Posts: 1636

PostPosted: Sun Oct 14, 2007 9:43 pm    Post subject: Send List or OTF spool as an attachment to email using ABAP Reply with quote

Requirement:
This program shows how a spool ( SapScript/Smartform -OTF or ABAP List ) can be sent as an attachment to email using ABAP Program.

Processing:
This program takes spool id and email-id as input and converts the spool to pdf using FMs 'CONVERT_ABAPSPOOLJOB_2_PDF' or 'CONVERT_OTFSPOOLJOB_2_PDF'. Further, the program calls mail API FM 'SO_NEW_DOCUMENT_ATT_SEND_API1' to send PDF file as an attachment. Mail Title and mail-body is prepared and the pdf internal table is adjusted first to 255 character inetrnal table suitable for mail API FM.

For real life scenarios, like sending purchase order as an attachment to email :
it can be achieved by modifying the print program of purchase order output type ( NEU ). Actually, SAP standard has configuration for sending PO as email, but the constraint is that you can't use mail body text for external mails. This is only possible for internal SAP Office mails. You can modify the print program ( copy of original ) for external send ( communication type '5' ) to get the spool number or OTF itab from the FORM_OPEN or smartform FM and then convert the spool to pdf and send it as an attachment with email body-text through your own coding in the print program.

Code:
*&---------------------------------------------------------------------*
* Written By   : Ram Manohar Tiwari
* Presented By :  www.rmtiwari.com
* Function     : Send any spool ( OTF / LIST )as attachment to an e-mail
*                using ABAP program.
*&---------------------------------------------------------------------*

REPORT Z_RMTIWARI_SEND_SPOOL_MAIL_ATT .
*----------------------------------------------------------------------*
PARAMETERS : P_SPOOL TYPE TSP01-RQIDENT OBLIGATORY .
PARAMETERS : P_MAIL  TYPE char100 OBLIGATORY .
*----------------------------------------------------------------------*
TYPES : TY_LINE type string.

DATA: IT_ATTACHMENT TYPE soli OCCURS 0 WITH HEADER LINE.
DATA: IT_ATTACHMENT_LONG TYPE TY_LINE OCCURS 0 WITH HEADER LINE.
DATA: LV_PDF_SIZE        type i.
DATA: LT_PDF             type standard table of tline with header line.

*---------------------------------------------------------------------*

PERFORM SEND_EMAIL USING P_SPOOL P_MAIL.

*---------------------------------------------------------------------*
*  FORM send_email
*---------------------------------------------------------------------*
*  -->  X_SPOOL_ID
*  -->  X_EMAIL
*---------------------------------------------------------------------*
 
FORM send_email USING X_SPOOL_ID X_EMAIL.

  DATA: LT_OBJPACK LIKE sopcklsti1 OCCURS 2 WITH HEADER LINE,
        LT_OBJHEAD LIKE solisti1 OCCURS 1 WITH HEADER LINE,
        LT_OBJBIN LIKE solisti1 OCCURS 10 WITH HEADER LINE,
        LT_OBJTXT LIKE solisti1 OCCURS 10 WITH HEADER LINE,
        LT_RECLIST LIKE somlreci1 OCCURS 5 WITH HEADER LINE,
        LV_DOCUMENT_DATA TYPE sodocchgi1.

  DATA: L_ATT_LINES TYPE i.

  DATA : LV_SPOOL_DESC(68) type c.

  CHECK NOT ( X_EMAIL IS INITIAL ).

  CLEAR: LT_RECLIST, LT_RECLIST[],
  LT_OBJHEAD, LT_OBJHEAD[],
  LT_OBJTXT, LT_OBJTXT[],
  LT_OBJBIN, LT_OBJBIN[],
  LT_OBJPACK, LT_OBJPACK[].

  CLEAR LV_DOCUMENT_DATA.

* Read spool and get the pdf internal table and name of spool
  PERFORM READ_SPOOL USING X_SPOOL_ID LV_SPOOL_DESC.

  CHECK NOT ( LT_PDF[] IS INITIAL ).

* Convert pdf itab to 255 line itab.
  data :LV_COUNTER  type i.
  data :LV_FROM     type i.

  loop at LT_PDF.
    translate  LT_PDF using ' ~' .
    concatenate IT_ATTACHMENT_LONG LT_PDF into it_attachment_long.
  endloop.
  translate  IT_ATTACHMENT_LONG using '~ ' .
  append IT_ATTACHMENT_LONG.
  clear : LV_COUNTER.

  DO.
    LV_COUNTER = strlen( IT_ATTACHMENT_LONG ).
    if LV_COUNTER ge 255.
      IT_ATTACHMENT = IT_ATTACHMENT_LONG(255).
      append IT_ATTACHMENT.
      SHIFT IT_ATTACHMENT_LONG by 255 places.
    else.
      IT_ATTACHMENT = IT_ATTACHMENT_LONG(lv_counter).
      append IT_ATTACHMENT.
      exit.
    endif.

  ENDDO.

* Body of email
  MOVE 'Email sent to you from SAP' TO LT_OBJTXT.
  APPEND LT_OBJTXT.

  LV_DOCUMENT_DATA-obj_name = 'SpoolMail'.

* Title of the email as spool name
  LV_DOCUMENT_DATA-obj_descr = LV_SPOOL_DESC.
  LV_DOCUMENT_DATA-sensitivty = 'O'.
  LV_DOCUMENT_DATA-expiry_dat = SY-datum + 15.
  LV_DOCUMENT_DATA-doc_size = STRLEN( LT_OBJTXT ).

* e-mail body
  CLEAR LT_OBJPACK.
  LT_OBJPACK-head_start = 1.
  LT_OBJPACK-head_num = 0.
  LT_OBJPACK-body_start = 1.
  LT_OBJPACK-body_num = 1.
  LT_OBJPACK-doc_type = 'RAW'.
  LT_OBJPACK-doc_size = STRLEN( LT_OBJTXT ).
  APPEND LT_OBJPACK.

* For e-mail attachment
  DESCRIBE TABLE IT_ATTACHMENT LINES L_ATT_LINES.

  READ TABLE IT_ATTACHMENT INDEX L_ATT_LINES.

  CLEAR LT_OBJPACK.
  LT_OBJPACK-transf_bin = 'X'.
  LT_OBJPACK-head_start = 1.
  LT_OBJPACK-head_num = 1.
  LT_OBJPACK-body_start = 1.
  LT_OBJPACK-body_num = L_ATT_LINES.
  LT_OBJPACK-doc_type = 'PDF'.
  LT_OBJPACK-obj_name = 'email'.
  LT_OBJPACK-obj_descr = LV_SPOOL_DESC.
  LT_OBJPACK-doc_size = ( 255 * ( L_ATT_LINES - 1 ) ) + STRLEN(
  IT_ATTACHMENT-line ).
  APPEND LT_OBJPACK.

* make recipient list
  LT_RECLIST-receiver = X_EMAIL.
  LT_RECLIST-rec_type = 'U'. "To external email id

  APPEND LT_RECLIST.

* send mail with attachment
  CALL FUNCTION 'SO_NEW_DOCUMENT_ATT_SEND_API1'
    EXPORTING
      document_data              = LV_DOCUMENT_DATA
      put_in_outbox              = 'X'
    TABLES
      packing_list               = LT_OBJPACK
      object_header              = LT_OBJHEAD
      contents_bin               = IT_ATTACHMENT
      contents_txt               = LT_OBJTXT
      receivers                  = LT_RECLIST
    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.
    write:/ 'Message sent'.
  else.
    write:/ 'Error encountered'.
  endif.

ENDFORM. " send_email
*&---------------------------------------------------------------------*
*&      Form  read_spool
*&---------------------------------------------------------------------*
 
FORM read_spool USING X_SPOOL_ID Y_SPOOL_DESC.

  DATA : LV_SPOOL_TYPE TYPE TSP01-RQDOCTYPE.

  SELECT SINGLE RQDOCTYPE RQTITLE
           INTO (lv_spool_type, y_spool_desc)
           FROM TSP01
          WHERE RQIDENT eq X_SPOOL_ID.

  IF Y_SPOOL_DESC IS INITIAL.
    concatenate 'Spool-' X_SPOOL_ID into Y_SPOOL_DESC.
  ENDIF.

  IF LV_SPOOL_TYPE eq 'LIST'.   " If spool is  a list

    CALL FUNCTION 'CONVERT_ABAPSPOOLJOB_2_PDF'
      EXPORTING
        SRC_SPOOLID                    = X_SPOOL_ID
*   NO_DIALOG                      =
*   DST_DEVICE                     =
*   PDF_DESTINATION                =
      IMPORTING
        PDF_BYTECOUNT                  = LV_PDF_SIZE
*   PDF_SPOOLID                    =
*   LIST_PAGECOUNT                 =
*   BTC_JOBNAME                    =
*   BTC_JOBCOUNT                   =
      TABLES
        PDF                            = LT_PDF
     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
              .
    IF SY-SUBRC <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
    ENDIF.

  ELSE.            " If spool is OTF

    CALL FUNCTION 'CONVERT_OTFSPOOLJOB_2_PDF'
      EXPORTING
        SRC_SPOOLID                    = X_SPOOL_ID
*   NO_DIALOG                      =
*   DST_DEVICE                     =
*   PDF_DESTINATION                =
     IMPORTING
       PDF_BYTECOUNT                  = LV_PDF_SIZE
*   PDF_SPOOLID                    =
*   OTF_PAGECOUNT                  =
*   BTC_JOBNAME                    =
*   BTC_JOBCOUNT                   =
     TABLES
       PDF                            = LT_PDF
     EXCEPTIONS
       ERR_NO_OTF_SPOOLJOB            = 1
       ERR_NO_SPOOLJOB                = 2
       ERR_NO_PERMISSION              = 3
       ERR_CONV_NOT_POSSIBLE          = 4
       ERR_BAD_DSTDEVICE              = 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
              .
    IF SY-SUBRC <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
    ENDIF.
  ENDIF.

ENDFORM.                    " read_spool
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 -> List Generation, LDB, Spool Process, SAP Query, Report Painter 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.