Friday, October 7, 2011

Send PDF attachment to email

REPORT  ZSENDPDF.

TYPE-POOLS: IXML.
DATA T_ATT_CONTENT_HEX type SOLIX_TAB.
DATA LO_SEND_REQUEST TYPE REF TO CL_BCS VALUE IS INITIAL.
DATA: LT_MESSAGE_BODY TYPE BCSY_TEXT VALUE IS INITIAL,
            LO_DOCUMENT TYPE REF TO CL_DOCUMENT_BCS VALUE IS INITIAL.
DATA: LX_DOCUMENT_BCS TYPE REF TO CX_DOCUMENT_BCS VALUE IS INITIAL.
DATA: LO_RECIPIENT TYPE REF TO IF_RECIPIENT_BCS VALUE IS INITIAL.

DATA FILE_NAME TYPE IBIPPARMS-PATH.
DATA FILE_NAME1 TYPE STRING.

*TO GET THE FILE NAME IN PRESENTATION SERVER
CALL FUNCTION 'F4_FILENAME'
 EXPORTING
   PROGRAM_NAME        = SYST-CPROG
   DYNPRO_NUMBER       = SYST-DYNNR
   FIELD_NAME          = ' '
 IMPORTING
   FILE_NAME           = FILE_NAME
          .
DATA IT_MAIL_HEX TYPE SOLIX_TAB.
FILE_NAME1 = FILE_NAME.
CALL FUNCTION 'GUI_UPLOAD'
  EXPORTING
    FILENAME                      = FILE_NAME1
   FILETYPE                      = 'BIN'

  TABLES
    DATA_TAB                      = IT_MAIL_HEX
 EXCEPTIONS
   FILE_OPEN_ERROR               = 1
   FILE_READ_ERROR               = 2
   NO_BATCH                      = 3
   GUI_REFUSE_FILETRANSFER       = 4
   INVALID_TYPE                  = 5
   NO_AUTHORITY                  = 6
   UNKNOWN_ERROR                 = 7
   BAD_DATA_FORMAT               = 8
   HEADER_NOT_ALLOWED            = 9
   SEPARATOR_NOT_ALLOWED         = 10
   HEADER_TOO_LONG               = 11
   UNKNOWN_DP_ERROR              = 12
   ACCESS_DENIED                 = 13
   DP_OUT_OF_MEMORY              = 14
   DISK_FULL                     = 15
   DP_TIMEOUT                    = 16
   OTHERS                        17
          .
IF SY-SUBRC <> 0.
 MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.



CLASS CL_BCS DEFINITION LOAD.
  LO_SEND_REQUEST = CL_BCS=>CREATE_PERSISTENT( ).

* MESSAGE BODY AND SUBJECT
APPEND 'Dear,' TO LT_MESSAGE_BODY.
append ' ' to LT_MESSAGE_BODY.
APPEND 'Please find the attached form.'
TO lt_message_body.
APPEND ' '
TO lt_message_body.
APPEND 'Thank You,'
TO lt_message_body.

*CREATING DOCUMENT
LO_DOCUMENT = CL_DOCUMENT_BCS=>CREATE_DOCUMENT(
I_TYPE = 'RAW'
I_TEXT = LT_MESSAGE_BODY
I_SUBJECT = 'Mail Attachment Sample Example'
).
T_ATT_CONTENT_HEX = IT_MAIL_HEX.
*CREATE ATTCHMENT AS PDF
TRY.
LO_DOCUMENT->ADD_ATTACHMENT(
EXPORTING
I_ATTACHMENT_TYPE = 'PDF'
I_ATTACHMENT_SUBJECT = 'Example Application for mail attachment'
I_ATT_CONTENT_HEX = t_att_content_hex ).
 CATCH CX_DOCUMENT_BCS INTO LX_DOCUMENT_BCS.

ENDTRY.

* ADD ATTACHMENT
* PASS THE DOCUMENT TO SEND REQUEST
LO_SEND_REQUEST->SET_DOCUMENT( LO_DOCUMENT ).
DATA: LO_SENDER TYPE REF TO IF_SENDER_BCS VALUE IS INITIAL,
L_SEND TYPE ADR6-SMTP_ADDR VALUE 'yourmailid@gmail.com'.


*SET SENDER:
*           THIS IS NECESSARY ONLY IF YOU WANT TO SET THE SENDER
*           DIFFERENT FROM ACTUAL USER (SY-UNAME). OTHERWISE SENDER IS
*           SET AUTOMATICALLY WITH ACTUAL USER.

LO_SENDER = CL_SAPUSER_BCS=>CREATE( SY-uname ).
LO_SEND_REQUEST->SET_SENDER(
EXPORTING
I_SENDER = LO_SENDER ).

* CREATE RECIPIENT
*ADD RECIPIENT (E-MAIL ADDRESS)
  LO_RECIPIENT = CL_CAM_ADDRESS_BCS=>CREATE_INTERNET_ADDRESS(
                            'yourmaiid@gmail.com' ).

* SET RECIPIENT
LO_SEND_REQUEST->ADD_RECIPIENT(
EXPORTING
I_RECIPIENT = LO_RECIPIENT
I_EXPRESS = 'X'           ).

* SEND EMAIL
DATA: LV_SENT_TO_ALL(1) TYPE VALUE IS INITIAL.
LO_SEND_REQUEST->SEND(
EXPORTING
I_WITH_ERROR_SCREEN = 'X'
RECEIVING
RESULT = LV_SENT_TO_ALL ).
 COMMIT WORK.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.