Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Mail sending program

Former Member
0 Likes
762

Hi,

Is there any function modules or classes there for sending mails outside from SAP..

If its there means give an example of tht,Othewise plz provide me an example progam to send a mail attaching a document

4 REPLIES 4
Read only

Former Member
0 Likes
608

Hi,

try this sample code.......

it sends the smartform as pdf attachment.......

DATA: v_form_name           TYPE rs38l_fnam,
      wa_control_parameters TYPE ssfctrlop,
      it_job_output_info    TYPE ssfcrescl,
      v_bin_filesize        TYPE i,
      it_otf_data           TYPE tsfotf,
      it_doctab_archive     TYPE TABLE OF docs  WITH HEADER LINE,
      it_lines              TYPE TABLE OF tline WITH HEADER LINE.

PARAMETER p_sform   type TDSFNAME OBLIGATORY DEFAULT 'ZTEST12'.

start-of-SELECTION.

CALL FUNCTION 'SSF_FUNCTION_MODULE_NAME'
  EXPORTING
    formname           = p_sform
  IMPORTING
    fm_name            = v_form_name
  EXCEPTIONS
    no_form            = 1
    no_function_module = 2
    OTHERS             = 3.
IF sy-subrc = 0.

  wa_control_parameters-no_dialog = 'X'.
  wa_control_parameters-getotf    = 'X'.

  CALL FUNCTION v_form_name
    EXPORTING
      control_parameters = wa_control_parameters
    IMPORTING
      job_output_info    = it_job_output_info
    EXCEPTIONS
      formatting_error   = 1
      internal_error     = 2
      send_error         = 3
      user_canceled      = 4
      OTHERS             = 5.
  IF sy-subrc = 0.
    it_otf_data = it_job_output_info-otfdata[].

    CALL FUNCTION 'CONVERT_OTF_2_PDF'
      IMPORTING
        bin_filesize           = v_bin_filesize
      TABLES
        otf                    = it_otf_data[]
        doctab_archive         = it_doctab_archive[]
        lines                  = it_lines[]
      EXCEPTIONS
        err_conv_not_possible  = 1
        err_otf_mc_noendmarker = 2
        OTHERS                 = 3.
    IF sy-subrc = 0.
      PERFORM mail_users TABLES it_lines.
    ENDIF.
  ENDIF.
ENDIF.

FORM mail_users  TABLES   p_lines STRUCTURE tline.

  CONSTANTS: c_raw TYPE so_obj_tp VALUE 'RAW',
             c_pdf TYPE so_obj_tp VALUE 'PDF',
             c_255 TYPE i VALUE 255,
             c_134 TYPE i VALUE 134,
             c_x   TYPE c VALUE 'X'.

  DATA: document_data LIKE sodocchgi1 ,
        tab_lines     TYPE i,
        contents_txt  TYPE TABLE OF solisti1   WITH HEADER LINE,
        contents_bin  TYPE TABLE OF solisti1   WITH HEADER LINE,
        packing_list  TYPE TABLE OF sopcklsti1 WITH HEADER LINE,
        object_header TYPE TABLE OF solisti1   WITH HEADER LINE,
        receivers     TYPE TABLE OF somlreci1  WITH HEADER LINE,
        l_str(255) ,
        v1 TYPE i,
        v2 TYPE i,
        v3 TYPE i.

  document_data-obj_name  = 'EMAIL'.
  document_data-obj_descr = 'Hi'.           " SUBJECT LINE

*********mail body************
  CLEAR       contents_txt.
  APPEND      contents_txt.
  MOVE 'SAP generated mail with attachment' TO contents_txt.
  APPEND      contents_txt.
  CLEAR       contents_txt.
  APPEND      contents_txt.
  APPEND      contents_txt.
******************************+

  DESCRIBE TABLE contents_txt LINES tab_lines.
  READ TABLE     contents_txt INDEX tab_lines.
  document_data-doc_size  = tab_lines .
  CLEAR packing_list-transf_bin.
  packing_list-head_start = 1.
  packing_list-head_num   = 0.
  packing_list-body_start = 1.
  packing_list-body_num   = tab_lines.
  packing_list-doc_type   = c_raw.
  packing_list-doc_size   = document_data-doc_size.
  APPEND packing_list.
  LOOP AT p_lines.
    CLEAR l_str.
    l_str+0(2)   = p_lines-tdformat.
    l_str+2(132) = p_lines-tdline.
    v2 = v1 + c_134.
    IF v2 LE c_255.
      contents_bin-line+v1(c_134) = l_str. v1 = v2.
    ELSE.
      v3 = v2 - c_255.
      v2 = c_255 - v1.
      IF NOT v2 IS INITIAL.
        contents_bin-line+v1(v2) = l_str+0(v2).
      ENDIF.
      APPEND contents_bin.
      CLEAR  contents_bin.
      v1 = v3.
      v3 = 134 - v1.
      IF NOT v1 IS INITIAL.
        contents_bin-line+0(v1) = l_str+v3(v1).
      ENDIF.
    ENDIF.
  ENDLOOP.
  APPEND contents_bin.
  CLEAR  contents_bin.
  DESCRIBE TABLE contents_bin LINES tab_lines.
  READ     TABLE contents_bin INDEX tab_lines.
  CONCATENATE 'test'
              '.pdf'
         INTO object_header.
  APPEND object_header.
  packing_list-transf_bin = c_x.
  packing_list-head_start = 1.
  packing_list-head_num   = 1.
  packing_list-body_start = 1.
  packing_list-body_num   = tab_lines.
  packing_list-doc_type   = c_pdf.
  packing_list-obj_name   = 'text1'.
  packing_list-obj_descr  = 'report'.
  packing_list-doc_size   = tab_lines * 255 + STRLEN( contents_bin ).
  APPEND packing_list.
  receivers-receiver      = 'hi@hello.com'.
  receivers-rec_type      = 'U'.
  APPEND receivers.
  CLEAR  receivers.
  CALL FUNCTION 'SO_DOCUMENT_SEND_API1'
    EXPORTING
      document_data              = document_data
      put_in_outbox              = ' '
      commit_work                = 'X'
    TABLES
      packing_list               = packing_list
      object_header              = object_header
      contents_bin               = contents_bin
      contents_txt               = contents_txt
      receivers                  = 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.
    WAIT UP TO 2 SECONDS.
    SUBMIT rsconn01 WITH mode = 'INT' AND RETURN.
  ENDIF.
ENDFORM.                    " mail_users

Also check the documentation gn for this FM 'SO_DOCUMENT_SEND_API1' in SE37

Cheers,

jose.

Edited by: Jose on Mar 26, 2008 6:59 AM

Read only

gopi_narendra
Active Contributor
0 Likes
608

This is the FM : SO_NEW_DOCUMENT_ATT_SEND_API1. Check the below sample code

FORM SEND_EMAIL.
  DATA: RECIPIENT_INT LIKE SADRUD,        "RECIPIENT ADDRESS
        OBJPACK LIKE SOPCKLSTI1 OCCURS 2 WITH HEADER LINE,
        OBJHEAD LIKE SOLISTI1 OCCURS 1 WITH HEADER LINE,
        RECLIST LIKE SOMLRECI1 OCCURS 1 WITH HEADER LINE,
        DOC_CHNG LIKE SODOCCHGI1,
        TAB_LINES LIKE SY-TABIX,
        SENT_TO_ALL LIKE SONV-FLAG.

  PERFORM BUILD_XLS_DATA_TABLE.
  PERFORM POPULATE_EMAIL_MESSAGE_BODY.

  CLEAR DOC_CHNG.
  DOC_CHNG-OBJ_NAME = 'MESSAGE'.
  DOC_CHNG-OBJ_DESCR    = 'Outstanding PO'.

* EMAIL MESSAGE
  DESCRIBE TABLE IT_MESSAGE LINES TAB_LINES.
  OBJPACK-TRANSF_BIN = 'X'.
  OBJPACK-HEAD_START = 1.
  OBJPACK-HEAD_NUM   = 0.
  OBJPACK-BODY_START = 1.
  OBJPACK-BODY_NUM   = TAB_LINES.
  OBJPACK-DOC_TYPE   = 'TXT'.
  OBJPACK-OBJ_NAME   = 'Message'.
  OBJPACK-DOC_SIZE   = TAB_LINES * 255.
  APPEND OBJPACK.

* ATTACHMENT
  CLEAR TAB_LINES.
  DESCRIBE TABLE IT_ATTACH LINES TAB_LINES.
  OBJPACK-TRANSF_BIN = 'X'.
  OBJPACK-HEAD_START = 1.
  OBJPACK-HEAD_NUM   = 0.
  OBJPACK-BODY_START = 1.
  OBJPACK-BODY_NUM   = TAB_LINES.
  OBJPACK-DOC_TYPE   = 'XLS'.
  OBJPACK-OBJ_NAME   = 'Attachment'.
  CONCATENATE 'Outstanding PO -' SY-DATUM INTO OBJPACK-OBJ_DESCR
    SEPARATED BY SPACE.
*  OBJPACK-OBJ_DESCR  = 'Outstanding PO/WO attachment'.
  OBJPACK-DOC_SIZE   = TAB_LINES * 255.
  APPEND OBJPACK.

* FILL EMAIL RECIPIENT
  CONCATENATE P_USER '@YAHOO.COM' INTO RECIPIENT_INT-ADDRESS.
  RECLIST-RECEIVER = RECIPIENT_INT.
  RECLIST-REC_TYPE = 'U'.
  APPEND RECLIST.

  CALL FUNCTION 'SO_NEW_DOCUMENT_ATT_SEND_API1'
    EXPORTING
      DOCUMENT_DATA                    = DOC_CHNG
      PUT_IN_OUTBOX                    = 'X'
    IMPORTING
      SENT_TO_ALL                      = SENT_TO_ALL
*     NEW_OBJECT_ID                    =
    TABLES
      PACKING_LIST                     = OBJPACK
*     OBJECT_HEADER                    =
      CONTENTS_BIN                     = IT_ATTACH
      CONTENTS_TXT                     = IT_MESSAGE
*     CONTENTS_HEX                     =
*     OBJECT_PARA                      =
*     OBJECT_PARB                      =
      RECEIVERS                        = RECLIST
    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.
    WRITE:/ 'ERROR SENDING EMAIL TO ', P_USER.
  ENDIF.

ENDFORM.                    " SEND_EMAIL

Regards

Gopi

Read only

Former Member
0 Likes
608

hi,

Sending External Mail via ABAP

REPORT ZSENDEXTERNAL.

DATA: OBJPACK LIKE SOPCKLSTI1 OCCURS 2 WITH HEADER LINE.

DATA: OBJHEAD LIKE SOLISTI1 OCCURS 1 WITH HEADER LINE.

DATA: OBJBIN LIKE SOLISTI1 OCCURS 10 WITH HEADER LINE.

DATA: OBJTXT LIKE SOLISTI1 OCCURS 10 WITH HEADER LINE.

DATA: RECLIST LIKE SOMLRECI1 OCCURS 5 WITH HEADER LINE.

DATA: DOC_CHNG LIKE SODOCCHGI1.

DATA: TAB_LINES LIKE SY-TABIX.

  • Creation of the document to be sent

  • File Name

DOC_CHNG-OBJ_NAME = 'SENDFILE'.

  • Mail Subject

DOC_CHNG-OBJ_DESCR = 'Send External Mail'.

  • Mail Contents

OBJTXT = 'Minimum bid : $250000'.

APPEND OBJTXT.

OBJTXT = 'A representation of the pictures up for auction'.

APPEND OBJTXT.

OBJTXT = 'was included as attachment.'.

APPEND OBJTXT.

DESCRIBE TABLE OBJTXT LINES TAB_LINES.

READ TABLE OBJTXT INDEX TAB_LINES.

DOC_CHNG-DOC_SIZE = ( TAB_LINES - 1 ) * 255 + STRLEN( OBJTXT ).

  • Creation of the entry for the compressed document

CLEAR OBJPACK-TRANSF_BIN.

OBJPACK-HEAD_START = 1.

OBJPACK-HEAD_NUM = 0.

OBJPACK-BODY_START = 1.

OBJPACK-BODY_NUM = TAB_LINES.

OBJPACK-DOC_TYPE = 'RAW'.

APPEND OBJPACK.

  • Creation of the document attachment

  • (Assume that the data in OBJBIN is in BMP format)

*OBJBIN = ' \O/ '. APPEND OBJBIN.

*OBJBIN = ' | '. APPEND OBJBIN.

*OBJBIN = ' / \ '. APPEND OBJBIN.

*DESCRIBE TABLE OBJBIN LINES TAB_LINES.

*OBJHEAD = 'PICTURE.BMP'.

*APPEND OBJHEAD.

    • Creation of the entry for the compressed attachment

*OBJPACK-TRANSF_BIN = 'X'.

*OBJPACK-HEAD_START = 1.

*OBJPACK-HEAD_NUM = 1.

*OBJPACK-BODY_START = 1.

*OBJPACK-BODY_NUM = TAB_LINES.

*OBJPACK-DOC_TYPE = 'BMP'.

*OBJPACK-OBJ_NAME = 'PICTURE'.

*OBJPACK-OBJ_DESCR = 'Representation of object 138'.

*OBJPACK-DOC_SIZE = TAB_LINES * 255.

*APPEND OBJPACK.

  • Completing the recipient list

RECLIST-RECEIVER = 'youremail@sap.com'.

RECLIST-REC_TYPE = 'U'.

APPEND RECLIST.

*RECLIST-RECEIVER = 'SAPUSERNAME'.

*RECLIST-REC_TYPE = 'P'.

*APPEND RECLIST.

  • Sending the document

CALL FUNCTION 'SO_NEW_DOCUMENT_ATT_SEND_API1'

EXPORTING

DOCUMENT_DATA = DOC_CHNG

PUT_IN_OUTBOX = 'X'

TABLES

PACKING_LIST = OBJPACK

OBJECT_HEADER = OBJHEAD

CONTENTS_BIN = OBJBIN

CONTENTS_TXT = OBJTXT

RECEIVERS = RECLIST

EXCEPTIONS

TOO_MANY_RECEIVERS = 1

DOCUMENT_NOT_SENT = 2

OPERATION_NO_AUTHORIZATION = 4

OTHERS = 99.

CASE SY-SUBRC.

WHEN 0.

WRITE: / 'Result of the send process:'.

LOOP AT RECLIST.

WRITE: / RECLIST-RECEIVER(48), ':'.

IF RECLIST-RETRN_CODE = 0.

WRITE 'The document was sent'.

ELSE.

WRITE 'The document could not be sent'.

ENDIF.

ENDLOOP.

WHEN 1.

WRITE: / 'No authorization for sending to the specified number',

'of recipients'.

WHEN 2.

WRITE: / 'Document could not be sent to any recipient'.

WHEN 4.

WRITE: / 'No send authorization'.

WHEN OTHERS.

WRITE: / 'Error occurred while sending'.

ENDCASE.

Reward if helpful

Regards

Vodka.

Read only

Former Member
0 Likes
608

And below is one example using Classes:

PARAMETERS: p_mail TYPE ad_smtpadr OBLIGATORY.
 
DATA: i_mara TYPE STANDARD TABLE OF mara.  " MARA Entries
DATA: l_text TYPE char255.  " Text
DATA: l_lines TYPE i,
      l_size            TYPE           sood-objlen.
" Size of Attachment
 
 
* Mail related
DATA: i_content         TYPE   soli_tab, " Mail content
      i_attach          TYPE   soli_tab. " Attachment
 
DATA: l_send_request    TYPE REF TO    cl_bcs,
                                            " E-Mail Send Request
      l_document        TYPE REF TO    cl_document_bcs,
                                            " E-Mail Attachment
      l_recipient       TYPE REF TO    if_recipient_bcs,
                                            " Distribution List
      l_sender          TYPE REF TO    if_sender_bcs,
                                            " Address of Sender
      l_uname           TYPE           salrtdrcpt,
                                            " Sender Name(SY-UNAME)
      l_bcs_exception   TYPE REF TO    cx_document_bcs,
                                            " BCS Exception
      l_addr_exception  TYPE REF TO    cx_address_bcs,
                                            " Address Exception
      l_send_exception  TYPE REF TO    cx_send_req_bcs.
" E-Mail sending Exception
 
.
 
*
*Constants------------------------------------------------------------*
CONSTANTS: c_tab(1) TYPE c VALUE
               cl_abap_char_utilities=>horizontal_tab,
                                     " Tab Character
 
           c_cr(1)  TYPE c VALUE cl_abap_char_utilities=>cr_lf,
                                     " Line Feed for End-Of_line
 
           c_ext    TYPE soodk-objtp VALUE 'XLS'. " XLS Extension
 
START-OF-SELECTION.
 
  SELECT * FROM mara INTO TABLE i_mara UP TO 20 ROWS.
 
* Preparing body of the Mail
  MOVE 'Mail Body' TO l_text.
  APPEND l_text TO i_content.
 
* Preparing contents of attachment with Change Log
  PERFORM prepare_attachment.
 
* Creates persistent send request
  TRY.
      l_send_request = cl_bcs=>create_persistent( ).
 
* Creating Document
      l_document = cl_document_bcs=>create_document(
                                    i_type  = 'RAW'
                                    i_text  = i_content[]
                                    i_subject = 'Material Details' ).
 
      DESCRIBE TABLE i_mara LINES l_lines.
* Size to multiplied by 2 for UNICODE enabled systems
      l_size = l_lines * 2 * 255.
 
* Adding Attachment
      CALL METHOD l_document->add_attachment
        EXPORTING
          i_attachment_type    = c_ext
          i_attachment_size    = l_size
          i_attachment_subject = 'Material Details'
          i_att_content_text   = i_attach[].
 
* Add document to send request
      CALL METHOD l_send_request->set_document( l_document ).
 
* Get Sender Object
      l_uname = sy-uname.
 
      l_sender = cl_sapuser_bcs=>create( l_uname ).
 
      CALL METHOD l_send_request->set_sender
        EXPORTING
          i_sender = l_sender.
 
* E-Mail
      TRANSLATE p_mail TO LOWER CASE.
 
    l_recipient = cl_cam_address_bcs=>create_internet_address( p_mail )
.
 
      CALL METHOD l_send_request->add_recipient
        EXPORTING
          i_recipient  = l_recipient
          i_express    = 'U'
          i_copy       = ' '
          i_blind_copy = ' '
          i_no_forward = ' '.
 
 
*Trigger E-Mail immediately
      l_send_request->set_send_immediately( 'X' ).
 
      CALL METHOD l_send_request->send( ).
 
      COMMIT WORK.
 
    CATCH cx_document_bcs INTO l_bcs_exception.
 
    CATCH cx_send_req_bcs INTO l_send_exception.
 
    CATCH cx_address_bcs  INTO l_addr_exception.
 
  ENDTRY.
 
 
*&---------------------------------------------------------------------
*
*&      Form  PREPARE_ATTACHMENT
*&---------------------------------------------------------------------
*
FORM prepare_attachment .
 
  FIELD-SYMBOLS: <lfs_table>,    " Internal table structure
                 <lfs_con>.      " Field Content
  DATA: l_text TYPE char1024.     " Text content for mail attachment
  DATA: l_con(50) TYPE c.        " Field Content in character format
 
* Columns to be tab delimeted
  LOOP AT i_mara ASSIGNING <lfs_table>.
    DO.
      ASSIGN COMPONENT sy-index OF STRUCTURE <lfs_table>
             TO <lfs_con>.
      IF sy-subrc NE 0.
        CONCATENATE c_cr l_text INTO l_text.
        APPEND l_text TO i_attach.
        EXIT.
      ELSE.
        CLEAR: l_con.
        MOVE <lfs_con> TO l_con.
        CONDENSE l_con.
        IF sy-index = 1.
          CLEAR: l_text.
          MOVE l_con TO l_text.
        ELSE.
          CONCATENATE l_text l_con INTO l_text
             SEPARATED BY c_tab.
        ENDIF.
      ENDIF.
    ENDDO.
  ENDLOOP.
 
ENDFORM.                    " PREPARE_ATTACHMENT