Menu

Change the SAP Sender Email Address in ERP

2015-04-24       

We sometimes want to control which email address is displayed when we send an email from SAP. Unfortunately, the function module SO_NEW_DOCUMENT_SEND_API1 uses the current user’s email address by default. Here’s a way to change this behaviour and set the SAP sender email address manually.

When we analyze the function module interface of SO_NEW_DOCUMENT_SEND_API1, there is no way to input a sender email address. This is on purpose, as SAP normally is supposed to use the current user’s email address maintained in SU01.

However, the email address is determined very early in the send process. SO_NEW_DOCUMENT_SEND_API1 calls SO_OBJECT_SEND, which does have input parameters for the sender mail address, namely ORIGINATOR and ORIGINATOR_TYPE. To expose these parameters, a few steps are enough:

  1. Make a copy of the function module SO_NEW_DOCUMENT_SEND_API1.
  2. Modify the new function module Z_SO_NEW_DOCUMENT_SEND_API1 to expose the parameters in its interface and supply them to SO_OBJECT_SEND.
  3. Modify the coding of the new function module slightly so the integration with function group SOI1 still works.

Step 3 is a bit tricky, but can be done quite easily if you know how. This is how to call the new function module to manually set the sender email address.

CALL FUNCTION 'Z_SO_NEW_DOCUMENT_SEND_API1'
 EXPORTING
   document_data = ls_docdata
   originator = 'senderemail@example.com'
   originator_type = 'U' "This needs to be 'U'
 TABLES
   object_content = lt_content
   receivers = lt_receiv
 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.

Simply supply the parameters ORIGINATOR and ORIGINATOR_TYPE. ORIGINATOR will contain the email address you want to use as sender. ORIGINATOR_TYPE must be set to “U”.

Complete Source Code

This is the complete coding of a copy of the function module SO_NEW_DOCUMENT_SEND_API1. It will enable you to manually set the sender email address of mails sent from SAP ERP.

If you take this over, make sure to add the correct interface parameters and also make sure to change some of the perform routines to work as external performs (applicable lines of code are marked in red).

FUNCTION z_so_new_document_send_api1.
*"-------------------------------------------------
*"*"Local Interface:
*" IMPORTING
*" VALUE(DOCUMENT_DATA) LIKE SODOCCHGI1 STRUCTURE SODOCCHGI1
*" VALUE(DOCUMENT_TYPE) LIKE SOODK-OBJTP DEFAULT 'RAW'
*" VALUE(PUT_IN_OUTBOX) LIKE SONV-FLAG DEFAULT SPACE
*" VALUE(ORIGINATOR) LIKE SOOS1-RECEXTNAM DEFAULT SPACE
*" VALUE(ORIGINATOR_TYPE) LIKE SOOS1-RECESC DEFAULT 'J'
*" EXPORTING
*" VALUE(SENT_TO_ALL) LIKE SONV-FLAG
*" VALUE(NEW_OBJECT_ID) LIKE SOFOLENTI1-OBJECT_ID
*" TABLES
*" OBJECT_HEADER STRUCTURE SOLISTI1 OPTIONAL
*" OBJECT_CONTENT STRUCTURE SOLISTI1 OPTIONAL
*" CONTENTS_HEX STRUCTURE SOLIX OPTIONAL
*" OBJECT_PARA STRUCTURE SOPARAI1 OPTIONAL
*" OBJECT_PARB STRUCTURE SOPARBI1 OPTIONAL
*" RECEIVERS STRUCTURE SOMLRECI1
*" EXCEPTIONS
*" TOO_MANY_RECEIVERS
*" DOCUMENT_NOT_SENT
*" DOCUMENT_TYPE_NOT_EXIST
*" OPERATION_NO_AUTHORIZATION
*" PARAMETER_ERROR
*" X_ERROR
*" ENQUEUE_ERROR
*"--------------------------------------------------
* This is a copy of SO_NEW_DOCUMENT_SEND_API1 with the parameters
* ORIGINATOR and ORIGINATOR_TYPE of FM SO_OBJECT_SEND exposed.
* It is thus possible to set the sender address of an email manually.
*"---------------------------------------------------
* This include is needed for the constants it contains.
INCLUDE rssocons.
* ---------------------------------------------------

DATA BEGIN OF object_id.
INCLUDE STRUCTURE soodk.
DATA END OF object_id.
DATA BEGIN OF object_hd_change.
INCLUDE STRUCTURE sood1.
DATA END OF object_hd_change.
DATA BEGIN OF object_fl_change.
INCLUDE STRUCTURE sofm1.
DATA END OF object_fl_change.
DATA BEGIN OF rec_table OCCURS 1.
INCLUDE STRUCTURE soos1.
DATA END OF rec_table.
DATA rcode LIKE sonv-rcode.
DATA i LIKE sy-tabix.
DATA: hex_size LIKE sy-tabix.

DESCRIBE TABLE contents_hex LINES hex_size.
IF hex_size GT 0.
  REFRESH object_content.
  MOVE contents_hex[] TO object_content[].
ENDIF.
* set default type
IF document_type = space.
  MOVE raw TO document_type.
ENDIF.
* check whether this document type is allowed at all
CALL FUNCTION 'SO_TSOPE_CHECK'
  EXPORTING
    file_ext = document_type
    query = crea
  IMPORTING
    rcode = rcode
  EXCEPTIONS
    x_error = 1000.
  IF sy-subrc = x_error.
    RAISE x_error.
  ENDIF.
  IF rcode NE ok.
    RAISE document_type_not_exist.
  ENDIF.
* transfer folder data
PERFORM transfer_objdat_to_obj(saplsoi1) USING
  document_data
  object_hd_change
  object_fl_change.
* check whther document_type is ok. If not it's a PC-bject.
IF document_type = 'INT'.
  MOVE document_type TO object_hd_change-file_ext.
  MOVE raw TO document_type.
ENDIF.
PERFORM check_object_type(sapfsso3) USING 
  document_type
  rcode.
IF rcode NE ok.
* it's an Pc application object
  MOVE document_type TO object_hd_change-file_ext.
  MOVE 'EXT' TO document_type.
ENDIF.
* transfer receiver data
PERFORM transfer_rec_to_tab(saplsoi1) TABLES 
  receivers
  rec_table.
* for old types move size to header
PERFORM size_to_header(saplsoi1) TABLES 
  object_header
USING 
  document_type
  document_data-doc_size.
CALL FUNCTION 'SO_OBJECT_SEND'
  EXPORTING
    object_fl_change = object_fl_change
    object_hd_change = object_hd_change
    object_type = document_type
    outbox_flag = put_in_outbox
    originator = originator
    originator_type = originator_type
  IMPORTING
    object_id_new = object_id
    sent_to_all = sent_to_all
  TABLES
    objcont = object_content
    objhead = object_header
    objpara = object_para
    objparb = object_parb
    receivers = rec_table
  EXCEPTIONS
    object_not_sent = 15
    object_type_not_exist = 17
    operation_no_authorization = 21
    parameter_error = 23
    too_much_receivers = 73
    OTHERS = 1000.
CASE sy-subrc.
  WHEN ok.
  WHEN object_not_sent.
    PERFORM transfer_tab_to_rec(saplsoi1) TABLES
      rec_table
      receivers.
    RAISE document_not_sent.
  WHEN too_much_receivers.
    RAISE too_many_receivers.
  WHEN object_type_not_exist.
    RAISE document_type_not_exist.
  WHEN operation_no_authorization.
    RAISE operation_no_authorization.
  WHEN parameter_error.
    RAISE parameter_error.
  WHEN OTHERS.
    RAISE x_error.
ENDCASE.
MOVE object_id TO new_object_id.
PERFORM transfer_tab_to_rec(saplsoi1) TABLES 
  rec_table
  receivers.
ENDFUNCTION.