Skip to Content
avatar image
Former Member

Send Smartforms Output as pdf Attachment in E-Mail

Hi!

I've searched quite a long time and tried to use different examples I've found in the forum but it doesn't work for me.

I tried to do the following:

Send output of smartforms by mail to a specified e-mail adress. Sometimes there have to be sent 2 pdf's (both are output of smartforms) in one e-mail.

The function to download the output of smartform with FM CONVERT_OTF_2_PDF and GUI_DOWNLOAD works fine for me but I couldn't get the FM SO_NEW_DOCUMENT_SEND_API1 to work.

Can someone give me an example or a description to get from the output of the smartform to the e-mail with attachment?

I'm pretty new to this so it maybe is a very simple question.

Regards,

Jacko

Add comment
10|10000 characters needed characters exceeded

  • Follow
  • Get RSS Feed

4 Answers

  • Best Answer
    avatar image
    Former Member
    Apr 15, 2009 at 10:21 AM

    use fm ZSO_NEW_DOCUMENT_ATT_SEND_API1.

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Apr 15, 2009 at 10:24 AM

    Try the following code

     report zpp430_report_in_pdf no standard page heading line-size 255.
    
    *& Converts spool request into PDF document and emails it to *
    *& recipicant. *
    *& *
    *& Execution *
    *& --------- *
    *& This program must be run as a background job in-order for the write *
    *& commands to create a Spool request rather than be displayed on *
    *& screen *
    *&---------------------------------------------------------------------*
    tables: tsp01.
    parameter: p_email1 like somlreci1-receiver.
    p_sender like somlreci1-receiver.
    p_repid like sy-repid, " Report to execute
    p_linsz like sy-linsz default 132, " Line size
    p_paart like sy-paart default 'X_65_132', " Paper Format
    p_slset like sy-slset, "Variant name
    p_odescr like sodocchgi1-obj_descr,
    p_adescr type so_obj_nam,
    p_delspl as checkbox.
    *DATA DECLARATION
    data: gd_recsize type i.
    * Spool IDs
    types: begin of t_tbtcp.
    include structure tbtcp.
    types: end of t_tbtcp.
    data: it_tbtcp type standard table of t_tbtcp initial size 0,
    wa_tbtcp type t_tbtcp.
    * Job Runtime Parameters
    data: gd_eventid like tbtcm-eventid,
    gd_eventparm like tbtcm-eventparm,
    gd_external_program_active like tbtcm-xpgactive,
    gd_jobcount like tbtcm-jobcount,
    gd_jobname like tbtcm-jobname,
    gd_stepcount like tbtcm-stepcount,
    gd_error type sy-subrc,
    gd_reciever type sy-subrc.
    data: w_recsize type i,
    mc_valid(1) type c.
    data: gd_subject like sodocchgi1-obj_descr,
    it_mess_bod like solisti1 occurs 0 with header line,
    it_mess_att like solisti1 occurs 0 with header line,
    gd_sender_type like soextreci1-adr_typ,
    gd_attachment_desc type so_obj_nam,
    gd_attachment_name type so_obj_des,
    mi_rqident like tsp01-rqident.
    * Spool to PDF conversions
    data: gd_spool_nr like tsp01-rqident,
    w_spool_nr like tsp01-rqident,
    gd_destination like rlgrap-filename,
    gd_bytecount like tst01-dsize,
    gd_buffer type string.
    data:
    mstr_print_parms like pri_params.
    * Binary store for PDF
    data: begin of it_pdf_output occurs 0.
    include structure tline.
    data: end of it_pdf_output.
    constants: c_dev like sy-sysid value 'DEV',
    c_no(1) type c value ' ',
    c_device(4) type c value 'LOCL'.
    ************************************************************************
    *START-OF-SELECTION.
    start-of-selection.
    * Write statement to represent report output. Spool request is created
    * if write statement is executed in background. This could also be an
    * ALV grid which would be converted to PDF without any extra effort
    * WRITE 'Hello World'.
    * NEW-PAGE.
    * COMMIT WORK.
    * NEW-PAGE PRINT OFF.
    * IF SY-BATCH EQ 'X'.
    * PERFORM GET_JOB_DETAILS.
    * PERFORM OBTAIN_SPOOL_ID.
    ************************************
    *** Alternative way could be to submit another program and store spool
    *** id into memory.
    *submit ZSPOOLTOPDF2
    * to sap-spool
    * spool parameters %_print
    * archive parameters %_print
    * without spool dynpro
    * and return.
    ************************************
    call function 'GET_PRINT_PARAMETERS'
    exporting
    authority = space
    copies = '1'
    cover_page = space
    data_set = space
    department = space
    destination = space
    expiration = '1'
    immediately = space
    * in_archive_parameters = space
    * in_parameters = space
    layout = space
    mode = space
    new_list_id = 'X'
    no_dialog = 'X'
    user = sy-uname
    importing
    out_parameters = mstr_print_parms
    valid = mc_valid
    exceptions
    archive_info_not_found = 1
    invalid_print_params = 2
    invalid_archive_params = 3
    others = 4.
    if mstr_print_parms-pdest = space.
    mstr_print_parms-pdest = 'LOCL'.
    endif.
    mstr_print_parms-linsz = p_linsz.
    mstr_print_parms-paart = p_paart.
    submit (p_repid) to sap-spool without spool dynpro
    spool parameters mstr_print_parms
    using selection-set p_slset
    and return.
    * Get spool id from program called above
    perform get_spool_number using sy-repid sy-uname changing mi_rqident.
    * IMPORT w_spool_nr FROM MEMORY ID SY-REPID.
    perform convert_spool_to_pdf.
    perform process_email.
    if p_delspl eq 'X'.
    perform delete_spool.
    endif.
    if sy-sysid = c_dev.
    wait up to 5 seconds.
    submit rsconn01 with mode = 'INT'
    with output = 'X'
    and return.
    endif.
    * ELSE.
    * SKIP.
    * WRITE:/ 'Program must be executed in background in-order for spool'
    *,
    * 'request to be created.'.
    * ENDIF.
    *---------------------------------------------------------------------*
    * FORM obtain_spool_id *
    *---------------------------------------------------------------------*
    form obtain_spool_id.
    check not ( gd_jobname is initial ).
    check not ( gd_jobcount is initial ).
    select * from tbtcp
    into table it_tbtcp
    where jobname = gd_jobname
    and jobcount = gd_jobcount
    and stepcount = gd_stepcount
    and listident <> '0000000000'
    order by jobname
    jobcount
    stepcount.
    read table it_tbtcp into wa_tbtcp index 1.
    if sy-subrc = 0.
    message s004(zdd) with gd_spool_nr.
    gd_spool_nr = wa_tbtcp-listident.
    message s004(zdd) with gd_spool_nr.
    else.
    message s005(zdd).
    endif.
    endform. "OBTAIN_SPOOL_ID
    *---------------------------------------------------------------------*
    * FORM get_job_details *
    *---------------------------------------------------------------------*
    form get_job_details.
    * Get current job details
    call function 'GET_JOB_RUNTIME_INFO'
    importing
    eventid = gd_eventid
    eventparm = gd_eventparm
    external_program_active = gd_external_program_active
    jobcount = gd_jobcount
    jobname = gd_jobname
    stepcount = gd_stepcount
    exceptions
    no_runtime_info = 1
    others = 2.
    endform. "GET_JOB_DETAILS
    *---------------------------------------------------------------------*
    * FORM convert_spool_to_pdf *
    *---------------------------------------------------------------------*
    form convert_spool_to_pdf.
    call function 'CONVERT_ABAPSPOOLJOB_2_PDF'
    exporting
    src_spoolid = mi_rqident
    no_dialog = c_no
    dst_device = c_device
    importing
    pdf_bytecount = gd_bytecount
    tables
    pdf = it_pdf_output
    exceptions
    err_no_abap_spooljob = 1
    err_no_spooljob = 2
    err_no_permission = 3
    err_conv_not_possible = 4
    err_bad_destdevice = 5
    user_cancelled = 6
    err_spoolerror = 7
    err_temseerror = 8
    err_btcjob_open_failed = 9
    err_btcjob_submit_failed = 10
    err_btcjob_close_failed = 11
    others = 12.
    check sy-subrc = 0.
    * Transfer the 132-long strings to 255-long strings
    loop at it_pdf_output.
    translate it_pdf_output using ' ~'.
    concatenate gd_buffer it_pdf_output into gd_buffer.
    endloop.
    translate gd_buffer using '~ '.
    do.
    it_mess_att = gd_buffer.
    append it_mess_att.
    shift gd_buffer left by 255 places.
    if gd_buffer is initial.
    exit.
    endif.
    enddo.
    endform. "CONVERT_SPOOL_TO_PDF
    *---------------------------------------------------------------------*
    * FORM process_email *
    *---------------------------------------------------------------------*
    form process_email.
    describe table it_mess_att lines gd_recsize.
    check gd_recsize > 0.
    perform send_email using p_email1.
    * perform send_email using p_email2.
    endform. "PROCESS_EMAIL
    *---------------------------------------------------------------------*
    * FORM send_email *
    *---------------------------------------------------------------------*
    * --> p_email *
    *---------------------------------------------------------------------*
    form send_email using p_email.
    check not ( p_email is initial ).
    refresh it_mess_bod.
    * Default subject matter
    gd_subject = p_odescr.
    gd_attachment_desc = p_adescr.
    * CONCATENATE 'attach_name' ' ' INTO gd_attachment_name.
    it_mess_bod = 'This is an automated report from SAP.'.
    append it_mess_bod.
    it_mess_bod = 'Please do not reply to this mail id.'.
    append it_mess_bod.
    *IT_MESS_BOD = 'For any clarification on the details of this report'
    *.
    * APPEND IT_MESS_BOD.
    * IT_MESS_BOD = 'please contact Business Planning. Thank you'.
    * APPEND IT_MESS_BOD.
    * If no sender specified - default blank
    if p_sender eq space.
    gd_sender_type = space.
    else.
    gd_sender_type = 'INT'.
    endif.
    * Send file by email as .xls speadsheet
    perform send_file_as_email_attachment
    tables it_mess_bod
    it_mess_att
    using p_email
    p_odescr
    'PDF'
    gd_attachment_name
    gd_attachment_desc
    p_sender
    gd_sender_type
    changing gd_error
    gd_reciever.
    endform. "SEND_EMAIL
    *---------------------------------------------------------------------*
    * FORM delete_spool *
    *---------------------------------------------------------------------*
    form delete_spool.
    data: ld_spool_nr type tsp01_sp0r-rqid_char.
    ld_spool_nr = gd_spool_nr.
    check p_delspl <> c_no.
    call function 'RSPO_R_RDELETE_SPOOLREQ'
    exporting
    spoolid = ld_spool_nr.
    endform. "DELETE_SPOOL
    *&---------------------------------------------------------------------*
    *& Form SEND_FILE_AS_EMAIL_ATTACHMENT
    *&---------------------------------------------------------------------*
    * Send email
    *----------------------------------------------------------------------*
    form send_file_as_email_attachment tables it_message
    it_attach
    using p_email
    p_mtitle
    p_format
    p_filename
    p_attdescription
    p_sender_address
    p_sender_addres_type
    changing p_error
    p_reciever.
    data: ld_error type sy-subrc,
    ld_reciever type sy-subrc,
    ld_mtitle like sodocchgi1-obj_descr,
    ld_email like somlreci1-receiver,
    ld_format type so_obj_tp ,
    ld_attdescription type so_obj_nam ,
    ld_attfilename type so_obj_des ,
    ld_sender_address like soextreci1-receiver,
    ld_sender_address_type like soextreci1-adr_typ,
    ld_receiver like sy-subrc.
    data: t_packing_list like sopcklsti1 occurs 0 with header line,
    t_contents like solisti1 occurs 0 with header line,
    t_receivers like somlreci1 occurs 0 with header line,
    t_attachment like solisti1 occurs 0 with header line,
    t_object_header like solisti1 occurs 0 with header line,
    w_cnt type i,
    w_sent_all(1) type c,
    w_doc_data like sodocchgi1.
    ld_email = p_email.
    ld_mtitle = p_mtitle.
    ld_format = p_format.
    ld_attdescription = p_attdescription.
    ld_attfilename = p_filename.
    ld_sender_address = p_sender_address.
    ld_sender_address_type = p_sender_addres_type.
    * Fill the document data.
    w_doc_data-doc_size = 1.
    * Populate the subject/generic message attributes
    w_doc_data-obj_langu = sy-langu.
    w_doc_data-obj_name = 'SAPRPT'.
    w_doc_data-obj_descr = ld_mtitle .
    w_doc_data-sensitivty = 'F'.
    * Fill the document data and get size of attachment
    clear w_doc_data.
    read table it_attach index w_cnt.
    w_doc_data-doc_size =
    ( w_cnt - 1 ) * 255 + strlen( it_attach ).
    w_doc_data-obj_langu = sy-langu.
    w_doc_data-obj_name = 'SAPRPT'.
    w_doc_data-obj_descr = ld_mtitle.
    w_doc_data-sensitivty = 'F'.
    clear t_attachment.
    refresh t_attachment.
    t_attachment[] = it_attach[].
    * Describe the body of the message
    clear t_packing_list.
    refresh t_packing_list.
    t_packing_list-transf_bin = space.
    t_packing_list-head_start = 1.
    t_packing_list-head_num = 0.
    t_packing_list-body_start = 1.
    describe table it_message lines t_packing_list-body_num.
    t_packing_list-doc_type = 'RAW'.
    append t_packing_list.
    * Create attachment notification
    t_packing_list-transf_bin = 'X'.
    t_packing_list-head_start = 1.
    t_packing_list-head_num = 1.
    t_packing_list-body_start = 1.
    describe table t_attachment lines t_packing_list-body_num.
    t_packing_list-doc_type = ld_format.
    t_packing_list-obj_descr = ld_attdescription.
    t_packing_list-obj_name = ld_attfilename.
    t_packing_list-doc_size = t_packing_list-body_num * 255.
    append t_packing_list.
    * Add the recipients email address
    clear t_receivers.
    refresh t_receivers.
    t_receivers-receiver = ld_email.
    t_receivers-rec_type = 'U'.
    t_receivers-com_type = 'INT'.
    t_receivers-notif_del = 'X'.
    t_receivers-notif_ndel = 'X'.
    append t_receivers.
    call function 'SO_DOCUMENT_SEND_API1'
    exporting
    document_data = w_doc_data
    put_in_outbox = 'X'
    sender_address = ld_sender_address
    sender_address_type = ld_sender_address_type
    commit_work = 'X'
    importing
    sent_to_all = w_sent_all
    tables
    packing_list = t_packing_list
    contents_bin = t_attachment
    contents_txt = it_message
    receivers = t_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.
    * Populate zerror return code
    ld_error = sy-subrc.
    * Populate zreceiver return code
    loop at t_receivers.
    ld_receiver = t_receivers-retrn_code.
    endloop.
    endform. "SEND_FILE_AS_EMAIL_ATTACHMENT
    *&---------------------------------------------------------------------*
    *& Form GET_SPOOL_NUMBER
    *&---------------------------------------------------------------------*
    * text
    *----------------------------------------------------------------------*
    * -->P_SY_REPID text
    * -->P_SY_UNAME text
    * <--P_MI_RQIDENT text
    *----------------------------------------------------------------------*
    form get_spool_number using f_repid
    f_uname
    changing f_rqident.
    data:
    lc_rq2name like tsp01-rq2name.
    concatenate f_repid+0(9)
    f_uname+0(3)
    into lc_rq2name.
    select * from tsp01 where rq2name = lc_rq2name
    order by rqcretime descending.
    f_rqident = tsp01-rqident.
    exit.
    endselect.
    if sy-subrc ne 0.
    clear f_rqident.
    endif.
    endform. " GET_SPOOL_NUMBER 

    there are lot's of posts available on sdn which u can refer to.

    tx

    ashwa

    Add comment
    10|10000 characters needed characters exceeded

  • Apr 15, 2009 at 10:30 AM

    hi please search in the forum for the same..

    /people/pavan.bayyapu/blog/2005/08/30/sending-html-email-from-sap-crmerp

    check the program BCS_EXAMPLE_6

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Apr 15, 2009 at 11:54 AM

    thank you for your answers but i still got a problem:

    I get an x_error for FM SO_NEW_DOCUMENT_ATT_SEND_API1 what can that be?

    here is my code! (Got most of the stuff out of the forum!)

    FORM mail_test.
      DATA: lv_fm_name              TYPE rs38l_fnam.
      DATA: ls_control_parameters   TYPE ssfctrlop.
      DATA: ls_output_options       TYPE ssfcompop.
    
      DATA: st_job_output_info      TYPE ssfcrescl,
            st_document_output_info TYPE ssfcrespd,
            st_job_output_options   TYPE ssfcresop.
      DATA: default_filename        TYPE string.
      CLEAR: lv_fm_name.
    
    DATA:
     I_OTF TYPE ITCOO OCCURS 0 WITH HEADER LINE,
     I_TLINE TYPE TABLE OF TLINE WITH HEADER LINE,
     I_RECEIVERS TYPE  TABLE OF SOMLRECI1 WITH HEADER LINE,
     I_RECORD LIKE SOLISTI1 OCCURS 0 WITH HEADER LINE,
     I_OBJPACK LIKE SOPCKLSTI1 OCCURS 0 WITH HEADER LINE,
     I_OBJTXT LIKE SOLISTI1 OCCURS 0 WITH HEADER LINE,
     I_OBJBIN LIKE SOLISTI1  OCCURS 0 WITH HEADER LINE,
     I_RECLIST LIKE SOMLRECI1  OCCURS 0 WITH HEADER LINE.
    
    DATA:
      W_OBJHEAD TYPE SOLI_TAB,
      W_CTRLOP TYPE SSFCTRLOP,
      W_COMPOP TYPE SSFCOMPOP,
      W_RETURN TYPE SSFCRESCL,
      W_DOC_CHNG TYPE SODOCCHGI1,
      W_DATA TYPE SODOCCHGI1,
      W_BUFFER TYPE STRING.
    
    DATA:
     V_FORM_NAME TYPE RS38L_FNAM,
     V_LEN_IN LIKE SOOD-OBJLEN,
     V_LEN_OUT LIKE SOOD-OBJLEN,
     V_LEN_OUTN TYPE I,
     V_LINES_TXT TYPE I,
     V_LINES_BIN TYPE I.
    
      DATA: v_bin_filesize  TYPE i.
      DATA: it_docs         TYPE STANDARD TABLE OF docs.
    
    * set control parameters
      CLEAR ls_control_parameters.
    
      ls_control_parameters-langu     = sy-langu.
      ls_control_parameters-no_dialog = 'X'.
      ls_control_parameters-getotf    = 'X'.
    
    * Determine smartform function module for purchase document
      CALL FUNCTION 'SSF_FUNCTION_MODULE_NAME'
        EXPORTING
          formname           = 'mysmartform'
        IMPORTING
          fm_name            = lv_fm_name
        EXCEPTIONS
          no_form            = 1
          no_function_module = 2
          OTHERS             = 3.
      IF sy-subrc <> 0.
        WRITE: 'error'.
      ENDIF.
    
    * print form
      CLEAR: st_document_output_info,
             st_job_output_info,
             st_job_output_options.
      CALL FUNCTION lv_fm_name
        EXPORTING
          control_parameters         = ls_control_parameters
          output_options             = ls_output_options
          user_settings              = 'X'
        IMPORTING
          document_output_info      = st_document_output_info
          job_output_info           = st_job_output_info
          job_output_options        = st_job_output_options
        TABLES
          t_gvn                    = gt_table
        EXCEPTIONS
         formatting_error           = 1
         internal_error             = 2
         send_error                 = 3
         user_canceled              = 4
         OTHERS                     = 5.
    
    ***convert otf
      CALL FUNCTION 'CONVERT_OTF_2_PDF'
        IMPORTING
          bin_filesize           = v_bin_filesize
        TABLES
          otf                    = st_job_output_info-otfdata
          doctab_archive         = it_docs
          lines                  = i_tline
        EXCEPTIONS
          err_conv_not_possible  = 1
          err_otf_mc_noendmarker = 2
          OTHERS                 = 3.
    
    
      LOOP AT i_tline.
        TRANSLATE i_tline USING '~'.
        CONCATENATE w_buffer i_tline INTO w_buffer.
      ENDLOOP.
    
      TRANSLATE w_buffer USING '~'.
    
      DO.
        i_record = w_buffer.
        APPEND i_record.
        SHIFT w_buffer LEFT BY 255 PLACES.
        IF w_buffer IS INITIAL.
          EXIT.
        ENDIF.
      ENDDO.
    
      REFRESH :
        i_reclist,
        i_objtxt,
        i_objbin,
        i_objpack.
    
      CLEAR w_objhead.
    
      i_objbin[] = i_record[].
      DESCRIBE TABLE i_objbin LINES v_lines_bin.
    
      i_objtxt = 'Smartform output'.
      APPEND i_objtxt.
      i_objtxt = 'Greets,'.
      APPEND i_objtxt.
      i_objtxt = 'sm'.
      APPEND i_objtxt.
    
      DESCRIBE TABLE i_objtxt LINES v_lines_txt.
    
      w_doc_chng-obj_name = 'Smartform'.
      w_doc_chng-expiry_dat = sy-datum + 10 .
      w_doc_chng-obj_descr  = 'Smart form output'.
      w_doc_chng-sensitivty = 'F'.
      w_doc_chng-doc_size = v_lines_txt * 255.
    
      CLEAR i_objpack-transf_bin.
    
      i_objpack-head_start = 1.
      i_objpack-head_num = 0.
      i_objpack-body_start = 1.
      i_objpack-body_num = v_lines_txt.
      i_objpack-doc_type = 'RAW'.
      APPEND i_objpack.
    
      i_objpack-transf_bin = 'X'.
      i_objpack-head_start = 1.
      i_objpack-head_num = 1.
      i_objpack-body_start = 1.
      i_objpack-body_num = v_lines_bin.
      i_objpack-doc_type  = 'PDF'.
      i_objpack-obj_name = 'Smartform'.
      CONCATENATE 'smartform output' 'pdf'
      INTO i_objpack-obj_descr.
      i_objpack-doc_size = v_lines_bin * 255.
      APPEND i_objpack.
    
      CLEAR i_reclist.
    
      i_reclist-receiver = 'myMAIL'.
      i_reclist-express  = 'X'.
      i_reclist-rec_type  = 'U'.
      APPEND i_reclist.
    
      CALL FUNCTION 'SO_NEW_DOCUMENT_ATT_SEND_API1'
        EXPORTING
          document_data              = w_doc_chng
          put_in_outbox              = 'X'
          commit_work                = 'X'
        TABLES
          packing_list               = i_objpack
          contents_bin               = i_objbin
          object_header              = w_objhead
          contents_txt               = i_objtxt
          receivers                  = i_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 NE 0.
        WRITE:/ 'Error When Sending the File', sy-subrc.
      ELSE.
        WRITE:/ 'Mail sent'.
      ENDIF.
    ENDFORM.

    Regards,

    Jacko

    Add comment
    10|10000 characters needed characters exceeded