Additional Blogs by Members
cancel
Showing results for 
Search instead for 
Did you mean: 
g_srivastava
Active Contributor
0 Kudos

I have been through SDN for quite a long time.I figured out that sending mails programatically using function module always has been creating some issues.This blog gives us an idea about how to code those function modules to achieve the desired result.Using SO_USER_READ_API1 , SO_DOCUMENT_REPOSITORY_MANAGER.

*---------------------------------------------------------------------*
* Types Declaration for Types types_s_files                           *
*---------------------------------------------------------------------*

TYPES:
  BEGIN OF  types_s_files,
      text(4096) TYPE c,               " Text
  END OF types_s_files.

*---------------------------------------------------------------------*
* Types Declaration for Types types_s_fold                            *
*---------------------------------------------------------------------*

TYPES :
  BEGIN OF types_s_folder,
     number(12) TYPE c,                " ID of Outbox Folder
     year(2) TYPE c,                   " Year from ID
     type(3) TYPE c,                   " Object type from ID
  END OF types_s_folder.

*---------------------------------------------------------------------*
* Data Declaration for Field String.                                        *
*---------------------------------------------------------------------*

DATA
  fs_ucomm type sy-ucomm.              " User Command

DATA
  fs_uname type soudnamei1.            " SAP Name and SAPoffice Name of a User

DATA
  fs_udat type soudatai1.                   " Data of an Office User

DATA
  fs_udef type soud-usrnam.              " User definition

DATA
  fs_recep type soos1.                       " Recipient with attributes

DATA
  fs_docu type sood4.                       " Interface for send screen and MOM

DATA
  fs_header type sood2.                    " object definition, display attributes

DATA
  fs_ocount type soli.                        " line, length 255

DATA
  fs_oheader type soli.                     " line, length 255

DATA
  fs_ostruc  type selc.                      " Old structure of selection table

DATA
  fs_opara  type soop1.                   "  Parameter for Dialog Modules

DATA
  fs_ref type soxrl.                          "  Link List of Documents

DATA
  fs_author type sofa-usracc.            " Authorizations for shared folders

DATA
  fs_sha_docu type sood4.               " Authorizations for shared folders

DATA
  fs_okey type soodk.                     " Definition of an Object (Key Part)

DATA
  fs_attach type sood5.                  " Interface for attachment list

DATA:
   fs_files TYPE types_s_files.           " Field String for the types_s_files

DATA :
   fs_folder TYPE types_s_folder.        " Field String for the types_s_folder

DATA:
  lw_file TYPE string.                          " Field string to hold file location address

DATA:
  fs_title(256) TYPE c.                        " To provide the mail subject line

*---------------------------------------------------------------------*
* Internal table declarations.                                                *
*---------------------------------------------------------------------*

DATA
   t_recep TYPE
  STANDARD TABLE
        OF soos1.

DATA
   t_docu TYPE
 STANDARD TABLE
       OF sood4.

DATA
   t_ocount TYPE
   STANDARD TABLE
         OF  soli .

DATA
   t_oheader TYPE
    STANDARD TABLE
          OF  soli.

DATA
   t_ostruc  TYPE
   STANDARD TABLE
         OF  selc .

DATA
   t_attach TYPE
   STANDARD TABLE
         OF sood5.

DATA
   t_opara  TYPE
  STANDARD TABLE
        OF  soop1.

DATA
   t_ref TYPE
STANDARD TABLE
      OF  soxrl.

DATA
     t_files TYPE
    STANDARD TABLE
          OF types_s_files.

DATA:
   t_folder TYPE
   STANDARD TABLE
         OF types_s_folder.

**---------------------------------------------------------------------*
** Parameter for provideing the file location                            *
**---------------------------------------------------------------------*

SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE file.

PARAMETERS:
  p_file TYPE
         rlgrap-filename OBLIGATORY.     " File Location Input

PARAMETERS:
  p_head TYPE string.                         " Mail Heading Input

SELECTION-SCREEN END OF BLOCK b1.

**At Selection-Screen..................................................*
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file .

  CALL FUNCTION 'F4_FILENAME'
    EXPORTING
      program_name  = syst-cprog
      dynpro_number = syst-dynnr
    IMPORTING
      file_name     = p_file.

START-OF-SELECTION.
  fs_uname-sapname = sy-uname.

*---------------------------------------------------------------------*
* Call Function SO_USER_READ_API1                                     *
*---------------------------------------------------------------------*

  CALL FUNCTION 'SO_USER_READ_API1'
    EXPORTING
      user            = fs_uname
    IMPORTING
      user_data       = fs_udat
    EXCEPTIONS
      user_not_exist  = 1
      parameter_error = 2
      x_error         = 3
      OTHERS          = 4.
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
            WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.

  fs_folder-type = fs_udat-outboxfol+0(3).
  fs_folder-year = fs_udat-outboxfol+3(2).
  fs_folder-number =  fs_udat-outboxfol+5(12).
  APPEND fs_folder TO t_folder.
  CLEAR fs_files.

  REFRESH :
    t_ocount,
    t_oheader,
    t_ostruc,
    t_opara,
    t_recep,
    t_attach,
    t_ref,
    t_files.

  fs_ucomm = 'SAVE'.
  fs_title = p_head.
  fs_docu-foltp = fs_folder-type.
  fs_docu-folyr = fs_folder-year.
  fs_docu-folno = fs_folder-number.
  fs_docu-objtp = fs_udat-object_typ.
  fs_docu-objdes = fs_title.
  fs_header-objdes =  fs_title.

*---------------------------------------------------------------------*
* Call Function SO_DOCUMENT_REPOSITORY_MANAGER           *
*---------------------------------------------------------------------*

  CALL FUNCTION 'SO_DOCUMENT_REPOSITORY_MANAGER'
    EXPORTING
      method       = fs_ucomm
      office_user  = sy-uname
      ref_document = fs_sha_docu
      new_parent   = fs_okey
    IMPORTING
      authority    = fs_author
    TABLES
      objcont      = t_ocount
      objhead      = t_oheader
      objpara      = t_ostruc
      objparb      = t_opara
      recipients   = t_recep
      attachments  = t_attach
      references   = t_ref
      files        = t_files
    CHANGING
      document     = fs_docu
      header_data  = fs_header.

  fs_ucomm = 'ATTCREATEFROMPC'.
  lw_file = p_file.
  fs_files-text = p_file.
  APPEND fs_files TO t_files.

*---------------------------------------------------------------------*
* Call Function SO_DOCUMENT_REPOSITORY_MANAGER           *
*---------------------------------------------------------------------*

  CALL FUNCTION 'SO_DOCUMENT_REPOSITORY_MANAGER'
    EXPORTING
      method       = fs_ucomm
      office_user  = fs_udef
      ref_document = fs_sha_docu
      new_parent   = fs_okey
    IMPORTING
      authority    = fs_author
    TABLES
      objcont      = t_ocount
      objhead      = t_oheader
      objpara      = t_ostruc
      objparb      = t_opara
      recipients   = t_recep
      attachments  = t_attach
      references   = t_ref
      files        = t_files
    CHANGING
      document     = fs_docu
      header_data  = fs_header.

  fs_ucomm = 'SEND'.

*---------------------------------------------------------------------*
* Call Function SO_DOCUMENT_REPOSITORY_MANAGER           *
*---------------------------------------------------------------------*
  CALL FUNCTION 'SO_DOCUMENT_REPOSITORY_MANAGER'
    EXPORTING
      method       = fs_ucomm
      office_user  = fs_udef
      ref_document = fs_sha_docu
      new_parent   = fs_okey
    IMPORTING
      authority    = fs_author
    TABLES
      objcont      = t_ocount
      objhead      = t_oheader
      objpara      = t_ostruc
      objparb      = t_opara
      recipients   = t_recep
      attachments  = t_attach
      references   = t_ref
      files        = t_files
    CHANGING
      document     = fs_docu
      header_data  = fs_header.

 

On Execution:

Execute the report we get the selection screen for the File path and Mail Subject Line. Here the subject is Business One File :

After providing the details we get the mail recipients address selection screen from SBWP.In the meantime the file is attached as attachment.Provide the name of the receiver and the recipient type.

Here I provided the details: 

Then when we click on send mail tab we get the message Document Sent.as shown below

We can see the mail sent in SAP Inbox using SBWP  

 

 

Have a best day ahead

15 Comments