Skip to Content

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

To report this post you need to login first.

15 Comments

You must be Logged on to comment or reply to a post.

  1. G Srivastava Post author
    Due to workprocessor availibilty sometime it may happen it takes time to get visible in mailbox so dont worry mail will be in your mail box for sure 🙂
    (0) 
    1. G Srivastava Post author
      Well it is mere conincidence but the fact is he used classes for the same and i am using function module.So here my blog becomes different.I have been through SDN and i saw same question again and again as a insired coach i thought it is better to make a blog and post.I did not tried to hurt anybody sentiments but if i did unknowingly I would be extremely sorry.
      (0) 
        1. G Srivastava Post author
          i am really pleased that you took interest to go through my blogs indeed i am happy about it.I wish no sentiments hurt from my blog.I was just participating here among all the users.
          (0) 
          1. Gregor Wolf
            Hi Gaurav,

            please see my suggestion as a positive feedback that I want to give you as a long time contributor, Moderator and SAP Mentor. If your Blogger status is still that your Blogs have to be approved then not you’re to blaim. The Blog Moderation policy isn’t always that strictly interpreted as I would do it. So please continue with your contribution. I just want to guide you towards a better way of doing it.

            Best regards
            Gregor

            (0) 
            1. G Srivastava Post author
              yeah and that is why i was replying As i was extremly pleased to get in contact with you.I was trying to converse you this way.Nice meeting you.My pleasure about your words they indeed inspired me Gregor
              (0) 
  2. Shai Eyal
    Hi,
    Very nice and details explanation.
    The only missing standard functionality is generating mail in HTML format.
    It is so obviously required and yet delivered.

    Any ideas on this one? At the moment I manually set doc type to HTM and parse the HTML by myself.

    Tx,
    Shai E.

    (0) 
    1. G Srivastava Post author
      Hi Shai,
            We need to pass values as HTML for that you can get the detailed explanation in the link just below our messages. I think it has been explained wonderfully and you will get some knowledge of configuration part too. If you feel any issues please let me know.
      Have a best day ahead
      (0) 
  3. Nigel James
    Hi There has been a few comments about this blog and let me add one or two thoughts.

    Firstly the images. The appear to be hosted on some external (to SCN) system. I would suggest that the best place to host them is on SCN itself. If the images can’t be seen because of issues with the external server the value of the blog is reduced.

    Also as mentioned by Gregor this topic has been covered before. Even though you took another angle on it FM’s rather than OO it would be useful to link to other content similar to your own so that people finding this blog know there is another OO approach.

    Personally I like the OO approach as referred to in the recent series by Thomas Jung. But then I am a bit of a “Double O” kind of guy 😉

    Welcome to the blogging world of SDN and I hope you continue to improve.

    Have a best day ahead,
    Nigel

    (0) 
  4. Terry Chen
    This blog is very helpful to me. But I still have 2 questions about using FM ‘SO_DOCUMENT_REPOSITORY_MANAGER’.
    1.You used it to send email with file from PC, can it be used to send email with file from server directory? how?
    2.i can’t pass the recipients adrress automatically and i must type it into the window that opens . Can i do it automatically without popup screen.
    (0) 

Leave a Reply