Sending Mail When The Purchase Order Is Released 

Enhancement for sending mail when the purchase order is releaed

When we release any object in me29n transaction then mail has to be send to vendor that the puchase order has been 
released.  The code that is to be written in the enhacement for the same is:

*&---------------------------------------------------------------------*
*&  Include           ZXM06U44
*&---------------------------------------------------------------------*
*Data Declaration
DATA : rel_ind LIKE i_ekko-frgke.
rel_ind = i_ekko-frgke.
*Release PO when final authorized person process PO
IF sy-tcode EQ 'ME29N' AND rel_ind EQ 2.

*Internal table to get vendor name and address number
  DATA : BEGIN OF it_vname OCCURS 0,
         name1 LIKE lfa1-name1,
         adrnr LIKE lfa1-adrnr,
         END OF it_vname.
*Internal table to get email_if with address number
  DATA : BEGIN OF it_vemail OCCURS 0,
         email LIKE adr6-smtp_addr,
         END OF it_vemail.
*Emiail subject
  DATA : psubject(40) TYPE c .

*Data declaration for mail FM
  DATA:   it_packing_list LIKE sopcklsti1 OCCURS 0 WITH HEADER LINE,
          it_contents LIKE solisti1 OCCURS 0 WITH HEADER LINE,
          it_receivers LIKE somlreci1 OCCURS 0 WITH HEADER LINE,
          it_attachment LIKE solisti1 OCCURS 0 WITH HEADER LINE,
          gd_cnt TYPE i,
          gd_sent_all(1) TYPE c,
          gd_doc_data LIKE sodocchgi1,
          gd_error TYPE sy-subrc.

*Internal table for message body
  DATA:   it_message TYPE STANDARD TABLE OF solisti1 INITIAL SIZE 0
                  WITH HEADER LINE,
          it_messagewa LIKE LINE OF it_message        .

  psubject = 'PO Regarding'.

*Accessing name and address number of a vendor
  SELECT SINGLE name1 adrnr FROM lfa1 INTO it_vname WHERE lifnr EQ 
i_ekko-lifnr.

*Accessing mail id of a vendor
  SELECT SINGLE smtp_addr FROM adr6 INTO it_vemail WHERE addrnumber EQ 
it_vname-adrnr.

* Mail Text
  clear it_message.
  REFRESH it_message.
  CONCATENATE 'Dear' it_vname-name1 ',' INTO it_messagewa SEPARATED BY 
space.
  APPEND  it_messagewa TO it_message.
  APPEND 'Please issue the items for the following PO Number .' TO 
it_message.
  CLEAR it_messagewa.
  CONCATENATE 'PO NUmber : ' i_ekko-ebeln  INTO it_messagewa SEPARATED 
BY space.
  APPEND it_messagewa TO it_message.
  APPEND 'you can view it at www.mindteck/sap/mm/login.' TO it_message.
  APPEND 'Regards,' TO it_message.
  APPEND 'Anand.' TO it_message.

* Fill the document data.
  gd_doc_data-doc_size = 1.

* Populate the subject/generic message attributes
  gd_doc_data-obj_langu = sy-langu.
  gd_doc_data-obj_name  = 'SAPRPT'.
  gd_doc_data-obj_descr = psubject.
  gd_doc_data-sensitivty = 'F'.

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

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

* Call the FM to post the message to SAPMAIL
  CALL FUNCTION 'SO_NEW_DOCUMENT_ATT_SEND_API1'
    EXPORTING
      document_data                    = gd_doc_data
      put_in_outbox                    = 'X'
      commit_work                      = 'X'
   IMPORTING
     sent_to_all                      = gd_sent_all
*   NEW_OBJECT_ID                    =
    TABLES
      packing_list                     = it_packing_list
*   OBJECT_HEADER                    =
*   CONTENTS_BIN                     =
      contents_txt                     = it_message
*   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.

* Store function module return code
  gd_error = sy-subrc.

* Get it_receivers return code
  LOOP AT it_receivers.
  ENDLOOP.
ENDIF.
This we need to write in the mm06e005.  In this enhacement in this enhacement there is a function exit exit_sapmm06e_013 in this I had wrote the code for the same enhacement. 

*-- Praveen Kumar (praveen417@gmail.com)

More ABAP Email Samples
How can you send a fax from within ABAP/4 programs

Get help for your ABAP problems
Do you have a ABAP Question?

ABAP Books
ABAP Certification, BAPI, Java, Web Programming, Smart Forms, Sapscripts Reference Books

ABAP Tips
ABAP Forum for Discussion and Samples Program Codes for Abapers

Best regards,
SAP Basis, ABAP Programming and Other IMG Stuff
http://www.erpgreat.com

All the site contents are Copyright © www.erpgreat.com and the content authors. All rights reserved.
All product names are trademarks of their respective companies.  The site www.erpgreat.com is in no way affiliated with SAP AG.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
 The content on this site may not be reproduced or redistributed without the express written permission of
www.erpgreat.com or the content authors.