Skip to Content

Archived discussions are read-only. Learn more about SAP Q&A

ABAP Program Send email with PDF Attachment Problem

Hi all,

I am having a problem with my program when I try to send a pdf attachment by email. The email i sent over to the user's inbox but is unable to open the attachment. Error message 'the file cannot be opened and might be damaged' is displayed when trying to open the attachment.

Below is the snippet of my code. please help to see if something is wrong with my code, i have gone through all the forums and still cannot find a solution as it is still giving me the same issue.

*---------------------------------------------------------------------*
*       FORM send_email                                               *
*---------------------------------------------------------------------*
*  -->  p_email                                                       *
*---------------------------------------------------------------------*

FORM send_email USING p_email.

CHECK NOT ( p_email IS INITIAL ).
  REFRESH it_mess_bod.
* Default subject matter

  gd_subject         = 'Subject'.
  gd_attachment_desc = 'Attachname'.
  it_mess_bod        = 'Message Body text, line 1'.
  APPEND it_mess_bod.
  it_mess_bod        = 'Message Body text, line 2...'.
  APPEND it_mess_bod.

* If no sender specified - default blank
  IF p_sender EQ space.
    gd_sender_type  = space.
  ELSE.
    gd_sender_type  = 'INT'.
  ENDIF.

* Send file by email as .pdf
  PERFORM send_file_as_email_attachment
                               tables it_mess_bod
                                      it_mess_att
                                using p_email
                                      'Test Attachment'
                                      'PDF'
                                      gd_attachment_name
                                      gd_attachment_desc
                                      p_sender
                                      gd_sender_type
                             changing gd_error
                                      gd_reciever.
ENDFORM.


*&---------------------------------------------------------------------*
*&      Form  SEND_FILE_AS_EMAIL_ATTACHMENT
*&---------------------------------------------------------------------*
*       Send email
*----------------------------------------------------------------------*
FORM send_file_as_email_attachment tables it_message
                                          it_attach
                                    using p_email
                                          p_mtitle
                                          p_format
                                          p_filename
                                          p_attdescription
                                          p_sender_address
                                          p_sender_addres_type
                                 changing p_error
                                          p_reciever.

  DATA: ld_error    TYPE sy-subrc,
        ld_reciever TYPE sy-subrc,
        ld_mtitle LIKE sodocchgi1-obj_descr,
        ld_email LIKE  somlreci1-receiver,
        ld_format TYPE  so_obj_tp ,
        ld_attdescription TYPE  so_obj_nam ,
        ld_attfilename TYPE  so_obj_des ,
        ld_sender_address LIKE  soextreci1-receiver,
        ld_sender_address_type LIKE  soextreci1-adr_typ,
        ld_receiver LIKE  sy-subrc.

data:   t_packing_list like sopcklsti1 occurs 0 with header line,
        t_contents like solisti1 occurs 0 with header line,
        t_receivers like somlreci1 occurs 0 with header line,
        t_attachment like solisti1 occurs 0 with header line,
        t_object_header like solisti1 occurs 0 with header line,
        w_cnt type i,
        w_sent_all(1) type c,
        w_doc_data like sodocchgi1.

  ld_email   = p_email.
  ld_mtitle = p_mtitle.
  ld_format              = p_format.
  ld_attdescription      = p_attdescription.
  ld_attfilename         = p_filename.
  ld_sender_address      = p_sender_address.
  ld_sender_address_type = p_sender_addres_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_attach INDEX w_cnt.
  w_doc_data-doc_size =  ( w_cnt - 1 ) * 255 + STRLEN( it_attach ).
  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_attach[].

* Describe the body of the message
  CLEAR t_packing_list.
  REFRESH t_packing_list.
  t_packing_list-transf_bin = space.
  t_packing_list-head_start = 1.
  t_packing_list-head_num = 0.
  t_packing_list-body_start = 1.
  DESCRIBE TABLE it_message LINES t_packing_list-body_num.
  t_packing_list-doc_type = 'RAW'.
  APPEND t_packing_list.

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

  DESCRIBE TABLE t_attachment LINES t_packing_list-body_num.
  t_packing_list-doc_type   =  ld_format.
  t_packing_list-obj_descr  =  ld_attdescription.
  t_packing_list-obj_name   =  ld_attfilename.
  t_packing_list-doc_size   =  t_packing_list-body_num * 255.
  APPEND t_packing_list.

* Add the recipients email address
  CLEAR t_receivers.
  REFRESH t_receivers.
  t_receivers-receiver = ld_email.
  t_receivers-rec_type = 'U'.
  t_receivers-com_type = 'INT'.
  t_receivers-notif_del = 'X'.
  t_receivers-notif_ndel = 'X'.
  APPEND t_receivers.

  CALL FUNCTION 'SO_DOCUMENT_SEND_API1'
       EXPORTING
            document_data              = w_doc_data
            put_in_outbox              = 'X'
            sender_address             = 'test@email.com'
            sender_address_type        = 'INT'
            commit_work                = 'X'
       IMPORTING
            sent_to_all                = w_sent_all
       TABLES
            packing_list               = t_packing_list
            contents_bin               = t_attachment
            contents_txt               = it_message
            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.
    ld_receiver = t_receivers-retrn_code.
  ENDLOOP.

ENDFORM.

Tags:
Former Member
Former Member replied

Hi,

     I guess there would be some problem in attachment size.

Just try to compare this part of code with urs.

FORM send_file_as_email_attachment TABLES pit_message
                                          pit_attach
                                          pt_receivers
                          USING p_mtitle
                                p_format
                                p_filename
                                p_attdescription
                                p_sender_address
                                p_sender_addres_type
                         CHANGING p_error
                                 p_reciever.

  DATA: ld_error TYPE sy-subrc,
  ld_reciever TYPE sy-subrc,
  ld_mtitle LIKE sodocchgi1-obj_descr,
  ld_email LIKE somlreci1-receiver,
  ld_format TYPE so_obj_tp ,
  ld_attdescription TYPE so_obj_nam ,
  ld_attfilename TYPE so_obj_des ,
  ld_sender_address LIKE soextreci1-receiver,
  ld_sender_address_type LIKE soextreci1-adr_typ,
  ld_receiver LIKE sy-subrc,
  t_receivers LIKE somlreci1 OCCURS 0 WITH HEADER LINE.

  ld_mtitle = p_mtitle.
  ld_format = p_format.
  ld_attdescription = p_attdescription.
  ld_attfilename = p_filename.
  ld_sender_address = p_sender_address.
  ld_sender_address_type = p_sender_addres_type.

* attachment info
  CLEAR gt_attachment.
  REFRESH gt_attachment.
  gt_attachment[] = pit_attach[].

* Fill the document data and get size of attachment
  CLEAR w_doc_data.
  DESCRIBE TABLE gt_attachment LINES sy-tfill.
  w_doc_data-doc_size =  sy-tfill * 255 + 62.
  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'.

* Add the recipients email address
  CLEAR t_receivers.
  REFRESH t_receivers.
  t_receivers[] = pt_receivers[].


* Describe the body of the message
  CLEAR gt_packing_list.
  REFRESH gt_packing_list.
  gt_packing_list-transf_bin = space.
  gt_packing_list-head_start = 1.
  gt_packing_list-head_num = 0.
  gt_packing_list-body_start = 1.
  DESCRIBE TABLE gt_message LINES gt_packing_list-body_num.
  gt_packing_list-doc_type = 'RAW'.
  APPEND gt_packing_list.
* Create attachment notification
  gt_packing_list-transf_bin = 'X'.
  gt_packing_list-head_start = 1.
  gt_packing_list-head_num = 1.
  gt_packing_list-body_start = 1.
  DESCRIBE TABLE gt_attachment LINES gt_packing_list-body_num.
  gt_packing_list-doc_type = 'RAW'. "ld_format.
  gt_packing_list-obj_descr = p_filename.
  gt_packing_list-obj_name = ld_attfilename.
  gt_packing_list-doc_size = gt_packing_list-body_num * 255.
  APPEND gt_packing_list.

  CLEAR gt_objhead.
  REFRESH gt_objhead.
  MOVE gv_fn TO gt_objhead.
  APPEND gt_objhead. CLEAR gt_objhead.

  CALL FUNCTION 'SO_NEW_DOCUMENT_ATT_SEND_API1'
    EXPORTING
      document_data                    = w_doc_data
      put_in_outbox                    = 'X'
      commit_work                      = 'X'
    IMPORTING
      sent_to_all                      = w_sent_all
*    NEW_OBJECT_ID                    =
    TABLES
      packing_list                     = gt_packing_list
      object_header                    = gt_objhead
      contents_bin                     = gt_attachment
      contents_txt                     = gt_message
*     CONTENTS_HEX                     =
*     OBJECT_PARA                      =
*     OBJECT_PARB                      =
      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
            .
  IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  ENDIF.

* Populate zerror return code
  ld_error = sy-subrc.
* Populate zreceiver return code
  LOOP AT t_receivers.
    ld_receiver = t_receivers-retrn_code.
  ENDLOOP.

*if sy-subrc = 0.
*message i000 with 'Email sent succesfully'.
*else.
*message i000 with 'Email could not be sent'.
*endif.


ENDFORM.                    "SEND_FILE_AS_EMAIL_ATTACHMENT

Thankyou

Ikshula

0 View this answer in context

Helpful Answer

by
Not what you were looking for? View more on this topic or Ask a question