Mail mit PDF versenden

Dieses Programm versendet ein Mail mit einem Spool im PDF Format

*&---------------------------------------------------------------------*
*& Report  Z_MAIL_PDF
*&
*&---------------------------------------------------------------------*
*& Regotz Michel
*& Dieses Programm versendet ein Mail mit einem Spool im PDF Format
*&---------------------------------------------------------------------*
REPORT z_mail_pdf.

*&---------------------------------------------------------------------*
*&      Datendeklaration PDF Konvertieren
*&---------------------------------------------------------------------*
"Datendeklaration um ein PDF aus einem Spool zu erzeugen
TYPES:
BEGIN OF zpdf,
tdformat TYPE     tdformat,
tdline TYPE tdline,
END OF zpdf.

DATAgt_pdf TYPE TABLE OF zpdf,
      gf_pdf_string TYPE string.

FIELD-SYMBOLS<fs_pdf> TYPE zpdf.

*&---------------------------------------------------------------------*
*&      Datendekleration Spoolnummer holen
*&---------------------------------------------------------------------*
DATA:      gf_spoolname TYPE rspo2name,
           gf_spoolnr LIKE tsp01-rqident,
           gs_tsp01 TYPE tsp01.

*&---------------------------------------------------------------------*
*&      Datendekleration E-Mail Versand
*&---------------------------------------------------------------------*
TYPES:
BEGIN OF zmail_attach,
line TYPE so_text255,
END OF zmail_attach.
"Felder
DATA:   gf_objektnummer         TYPE c LENGTH 17,
        gf_receiver             TYPE c LENGTH 241.
"Zeilen Und Tabellen
DATA:   gs_pdf_second           TYPE zmail_attach,
        gt_pdf_second           TYPE TABLE OF zmail_attach,
        gs_packing_list         TYPE sopcklsti1,
        gt_packing_list         TYPE sopcklsti1_t,
        gs_receivers            TYPE somlreci1,
        gt_receivers            TYPE somlreci1_t,
        gs_message              TYPE solisti1,
        gt_message              TYPE srm_t_solisti1,
        gs_doc_data             TYPE sodocchgi1,
        gs_verteiler            TYPE somlreci1,
        gt_verteiler            TYPE somlreci1_t.

DATA:
gt_member TYPE TABLE OF sodm1,
gs_member TYPE sodm1,
gt_objpara TYPE TABLE OF selc,
gt_objparb TYPE TABLE OF soop1,
gt_dli_display_tab TYPE TABLE OF soxdl.

FIELD-SYMBOLS<fs_verteiler> TYPE somlreci1,
               <fs_member> TYPE sodm1.

*&---------------------------------------------------------------------*
*&      Datendekleration für den E-Mail Text
*&---------------------------------------------------------------------*
DATAgf_mail_text(255TYPE c,
      gf_linebreak TYPE abap_cr_lf VALUE cl_abap_char_utilities=>cr_lf.

*&---------------------------------------------------------------------*
*&      Selectionscreen
*&---------------------------------------------------------------------*
SELECTION-SCREEN BEGIN OF BLOCK b01 WITH FRAME.
SELECT-OPTIONS:
s_mails FOR gs_receivers-receiver.

PARAMETERS:
p_dil  TYPE  so_obj_nam,
p_header TYPE so_obj_des"max. 50 Zeichen
p_spooln TYPE rspoid.
SELECTION-SCREEN END OF BLOCK b01.

SELECTION-SCREEN BEGIN OF BLOCK b02 WITH FRAME.
SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN COMMENT 1(79comm1.
SELECTION-SCREEN END OF LINE.
SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN COMMENT 1(79comm2.
SELECTION-SCREEN END OF LINE.
SELECTION-SCREEN END OF BLOCK b02.

*&---------------------------------------------------------------------*
*&      AT SELECTION-SCREEN OUTPUT
*&---------------------------------------------------------------------*
AT SELECTION-SCREEN OUTPUT.
  
comm1 'Mit diesem Programm kann man E-Mails versenden'.
  comm2 'Diesem Mail wird ein Spoolausdruck als PDF anhängen '.

*&---------------------------------------------------------------------*
*&      Spoolnummer holen
*&---------------------------------------------------------------------*
  "Lesen der aktuellsten Spoolnummer des angemeldeten Users
  SELECT FROM tsp01 INTO gs_tsp01
    WHERE rqowner sy-uname
    ORDER BY rqcretime ASCENDING.
    gf_spoolnr gs_tsp01-rqident.
  ENDSELECT.
  "Spoolnummer im Slelektionscreen eintragen
  p_spooln gf_spoolnr.

*&---------------------------------------------------------------------*
*&      START-OF-SELECTION
*&---------------------------------------------------------------------*
*&---------------------------------------------------------------------*
*&      Spoolauftragkonvertieren
*&---------------------------------------------------------------------*
  "Mit diesem FUBA Konvertieren Sie den Spool ins PDF Format

START-OF-SELECTION.
  
CALL FUNCTION 'CONVERT_ABAPSPOOLJOB_2_PDF'
  EXPORTING
   src_spoolid                    p_spooln
   no_dialog                      ''
   dst_device                     'LOCL'
*     PDF_DESTINATION                =
*     NO_BACKGROUND                  =
*     GET_SIZE_FROM_FORMAT           =
*    IMPORTING
*     pdf_bytecount                  =
*     PDF_SPOOLID                    =
*     LIST_PAGECOUNT                 =
*     BTC_JOBNAME                    =
*     BTC_JOBCOUNT                   =
*     BIN_FILE                       =
  TABLES
   pdf                            gt_pdf
  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.
  
IF sy-subrc <> 0.
    MESSAGE 'Spool konnte nich in eine PDF konvertiert werden' TYPE 'S'DISPLAY LIKE 'W'.
  ENDIF.

*&---------------------------------------------------------------------*
*&      Einen String aus dem Spoolauftrag erstellen
*&---------------------------------------------------------------------*
  LOOP AT gt_pdf ASSIGNING <fs_pdf>.
    
TRANSLATE <fs_pdf> USING ' ~'.
    CONCATENATE gf_pdf_string <fs_pdf> INTO gf_pdf_string.
  ENDLOOP.

*&---------------------------------------------------------------------*
*&      String des PDF-Quelltexts in itab konvertieren
*&---------------------------------------------------------------------*
  TRANSLATE gf_pdf_string USING '~ '.
  DO.
    MOVE gf_pdf_string  TO gs_pdf_second.
    APPEND gs_pdf_second TO gt_pdf_second.
    SHIFT gf_pdf_string LEFT BY 255 PLACES.
    
IF gf_pdf_string IS INITIAL.
      EXIT.
    ENDIF.
  ENDDO.

*&---------------------------------------------------------------------*
*&      Nachrichtenext des Mails erstellen (in HTML)
*&---------------------------------------------------------------------*
  "HTML CODING kann hier hiterlegt werden, falls der Parameten gs_packing_list-doc_type = 'HTML' befüllt wird ;-) .
  "Ansonsten nur wenn er RAW ist nur Text hinterlegen.
  CONCATENATE '<body><br><br><span style ="font-weight: bold;">' sy-cprog '</span><br><br>'
              '<b> Hier könnte ganz viel Text stehen <br>'
              '<a href="regotz.jimdo.com">regotz.jimdo.com</a> <br>'
              '<br>'
              'Freundliche Grüsse <br>'
              '<br> '
              'Michel'
              '<body>'
  "Text in Ausgabetabelle speichern
    INTO gf_mail_text SEPARATED BY space.
  APPEND gf_mail_text TO gt_message.

*&---------------------------------------------------------------------*
*&      Dokumentdaten festlegen
*&---------------------------------------------------------------------*
  "Mail Parameter
  gs_doc_data-obj_name        'SAPRPT'.                          "Name des Dokuments
  gs_doc_data-obj_descr       p_header.                          "Betreff Titel kurze Bescheibung des Dokuments
  gs_doc_data-obj_langu       sy-langu.                          "Sprache des Dokuments
  gs_doc_data-sensitivty      'F'.                               "Sensivität O = Standard F = Funktional, kann funktional witergeleitet werden,  P = Vertraulich, für Vertreter nicht sichtbar
  "Text Parameter
  gs_packing_list-transf_bin  space.                             "Bei nicht gesetzem Flag liegt der Objektinhalt in der tabelle CONTENTS_TXT im ASCII Format vor
  gs_packing_list-head_start  1.                                 
"Startzeile Objekt Header
  gs_packing_list-head_num    1.                                 "Anzahl Zeilen des Headers
  gs_packing_list-body_start  1.                                 "Startzeile des Bodys
  gs_packing_list-body_num    LINESgt_message ).               "Anzahl Zeilen des Bodys
  gs_packing_list-doc_type    'HTML'.                            "Mail Format Typ (Typ der Anlage)
  "In der Parameter Tabelle aufnehmen
  APPEND gs_packing_list    TO gt_packing_list.
  CLEAR gs_packing_list.

  
"Anhang (Parameter des angehängten Dokumentes (Anhangsdaten))
  gs_packing_list-transf_bin  'X'.                                "Wenn dieses Flag (X) gesetzt wird, bezeichnet der Tabelleneintrag ein im Binärformat hintelegtes Objekt. Der zugehörenden Objektinhalt befindet sich ind der TABELLE CONTENTS_BIN
  gs_packing_list-head_start  1.                                  
"Startzeile Objekt Header
  gs_packing_list-head_num    1.                                  "Anzahl Zeilen des Headers
  gs_packing_list-body_start  1.                                  "Startziele des Bodys
  gs_packing_list-body_num    LINESgt_pdf_second ).             
"Anzahl Zeilen des Bodys
  gs_packing_list-doc_type    'PDF'.                              "Format
  gs_packing_list-obj_name    'Name_PDF'.                         "Dokumentname. Name der ANlage In der ersten Zeile der Tabbelle wird dieses Feld nicht verwendet
  gs_packing_list-obj_descr   'Attachment'.                       "Name/Beschriftung im Mail. Titel (kurz Beschreibung dr Anlage. In der ersten Zeile der Tabelle wird diesse feld nicht verwendet
  gs_packing_list-doc_size    =  gs_packing_list-body_num * 255.    "Grösse des Dokuments Anzal Zeilen * 255
  "In der Parameter Tabelle aufnehmen
  APPEND gs_packing_list    TO gt_packing_list.
  
CLEAR gs_packing_list.

*&---------------------------------------------------------------------*
*&      Empfängerliste aufbauen
*&---------------------------------------------------------------------*
  "Lesen einer Verteiler Liste
  CALL FUNCTION 'SO_DLI_READ'
   EXPORTING
     distributionlist                 p_dil
*   DLI_ID                           = ' '
*   OWNER                            = ' '
     system_dli                       'X'
* IMPORTING
*   OBJECT_FL_DISPLAY                =
*   OBJECT_HD_DISPLAY                =
*   OBJECT_ID                        =
*   OBJECT_SD_DISPLAY                =
    TABLES
      member                           gt_member
      objpara                          gt_objpara
      objparb                          gt_objparb
    EXCEPTIONS
      active_user_not_exist            1
      communication_failure            2
      component_not_available          3
      dl_name_not_exist                4
      folder_not_exist                 5
      folder_no_authorization          6
      forwarder_not_exist              7
      object_not_exist                 8
      object_no_authorization          9
      operation_no_authorization       10
      owner_not_exist                  11
      parameter_error                  12
      substitute_not_active            13
      substitute_not_defined           14
      system_failure                   15
      user_not_exist                   16
      x_error                          17
      OTHERS                           18
            .
  
IF sy-subrc <> 0.
    MESSAGE 'Es wurde keine Verteilerliste gelesen' TYPE 'S'DISPLAY LIKE 'W'.
  
ENDIF.

  "Mailadressen der Verteilerliste hinzufügen
  LOOP AT gt_member  ASSIGNING <fs_member>.
    gs_receivers-receiver       <fs_member>-address.               "Mailadresse
    gs_receivers-rec_type       'U'.                               "Typ des Receiver Eintrags U: Intenetadresse
    gs_receivers-com_type       'INT'.                             
"Kommunikationsart INT = Senden übers Interent
    gs_receivers-notif_del      'X'.                               "Wenn dieses Flag gesetzt (X) ist, wird der Sender davon in Kenntnis gesetzt, sobald der Empfänger das Dokument gelesen hat. Nur sinvoll Bei Externer Sendung.
    gs_receivers-notif_ndel     'X'.
    
" In der Parameter Tabelle aufnemen
    APPEND gs_receivers      TO gt_receivers.
    CLEAR gs_receivers.
  ENDLOOP.
  "Manuell eingebene Mailadressen hinzufügen
  LOOP AT s_mails.
    gs_receivers-receiver       s_mails-low.                       "Mailadresse
    gs_receivers-rec_type       'U'.                               "Typ des Receiver Eintrags U: Intenetadresse
    gs_receivers-com_type       'INT'.                             
"Kommunikationsart INT = Senden übers Interent
    gs_receivers-notif_del      'X'.                               "Wenn dieses Flag gesetzt (X) ist, wird der Sender davon in Kenntnis gesetzt, sobald der Empfänger das Dokument gelesen hat. Nur sinvoll Bei Externer Sendung.
    gs_receivers-notif_ndel     'X'.
    
" In der Parameter Tabelle aufnemen
    APPEND gs_receivers      TO gt_receivers.
    CLEAR gs_receivers.
  ENDLOOP.

*&---------------------------------------------------------------------*
*&      E-Mail versenden
*&---------------------------------------------------------------------*
  CALL FUNCTION 'SO_DOCUMENT_SEND_API1'
    EXPORTING
     document_data                    gs_doc_data
     put_in_outbox                    '' "ABU HIER EVTL STEUERBAR, ob versendet werden soll!!!
     sender_address                   'michel@regotz.ch' "Absender Andresse
     sender_address_type              'INT'
     commit_work                      'X'
    IMPORTING
*     SENT_TO_ALL                      = w_sent_all
     new_object_id                    gf_objektnummer
*     SENDER_ID                        =
    TABLES
     packing_list                     gt_packing_list  "Dokument Eigenschaften
*     OBJECT_HEADER                    =
     contents_bin                     gt_pdf_second "PDF Inhalt
     contents_txt                     gt_message   "E-Mail Text
*     CONTENTS_HEX                     =
*     OBJECT_PARA                      =
*     OBJECT_PARB                      =
      receivers                        gt_receivers "Empfängerliste
  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.
    MESSAGE 'Es konnte kein Mail gesendet werden' TYPE 'S'DISPLAY LIKE 'W'.
  ENDIF.

*&---------------------------------------------------------------------*
*&      Alternativer FUBA und Parameter listen
*&---------------------------------------------------------------------*
  "Alternativer FUBA
* CALL FUNCTION 'SO_NEW_DOCUMENT_SEND_API1'

  "Dokumentdaten festlegen
*  MOVE ''                    TO gs_doc_data-obj_name.
*  MOVE ''                    TO gs_doc_data-obj_descr.
*  MOVE ''                    TO gs_doc_data-obj_langu.
*  MOVE ''                    TO ls_doc_data-obj_sort.
*  MOVE ''                    TO ls_doc_data-obj_expdat.
*  MOVE ''                    TO gs_doc_data-sensitivty.
*  MOVE ''                    TO ls_doc_data-obj_prio.
*  MOVE ''                    TO ls_doc_data-no_change.
*  MOVE ''                    TO ls_doc_data-priority.
*  MOVE ''                    TO ls_doc_data-expiry_dat.
*  MOVE ''                    TO ls_doc_data-proc_type.
*  MOVE ''                    TO ls_doc_data-proc_name.
*  MOVE ''                    TO ls_doc_data-proc_syst.
*  MOVE ''                    TO ls_doc_data-proc_clint.
*  MOVE ''                    TO ls_doc_data-skip_scren.
*  MOVE ''                    TO ls_doc_data-to_do_out.
*  MOVE ''                    TO ls_doc_data-free_del.
*  MOVE ''                    TO ls_doc_data-doc_size.

  "Text-Parameter
*  MOVE ''                    TO gs_packing_list-transf_bin.
*  MOVE ''                    TO gs_packing_list-head_start.
*  MOVE ''                    TO gs_packing_list-head_num.
*  MOVE ''                    TO gs_packing_list-body_start.
*  MOVE ''                    TO gs_packing_list-body_num.
*  MOVE ''                    TO gs_packing_list-doc_type.
*  MOVE ''                    TO ls_packing_list-obj_name.
*  MOVE ''                    TO ls_packing_list-obj_descr.
*  MOVE ''                    TO ls_packing_list-obj_langu.
*  MOVE ''                    TO ls_packing_list-doc_size.
*  MOVE ''                    TO ls_packing_list-mess_type.

  "Anhangsdaten
*  MOVE ''                    TO gs_packing_list-transf_bin.
*  MOVE ''                    TO gs_packing_list-head_start.
*  MOVE ''                    TO gs_packing_list-head_num.
*  MOVE ''                    TO gs_packing_list-body_start.
*  MOVE ''                    TO gs_packing_list-body_num.
*  MOVE ''                    TO gs_packing_list-doc_type.
*  MOVE ''                    TO gs_packing_list-obj_name.
*  MOVE ''                    TO gs_packing_list-obj_descr.
*  MOVE ''                    TO ls_packing_list-obj_langu.
*  MOVE ''                    TO gs_packing_list-body_num * 255.
*  MOVE ''                    TO ls_packing_list-mess_type.

*"Empfängerdaten
*  MOVE ''                    TO gs_receivers-receiver.
*  MOVE ''                    TO gs_receivers-rec_type.
*  MOVE ''                    TO ls_receivers-rec_id.
*  MOVE ''                    TO ls_receivers-reply_doc.
*  MOVE ''                    TO ls_receivers-rec_date.
*  MOVE ''                    TO ls_receivers-proxy_id.
*  MOVE ''                    TO ls_receivers-retrn_code.
*  MOVE ''                    TO ls_receivers-express.
*  MOVE ''                    TO ls_receivers-copy.
*  MOVE ''                    TO ls_receivers-blind_copy.
*  MOVE ''                    TO ls_receivers-no_forward.
*  MOVE ''                    TO ls_receivers-no_print.
*  MOVE ''                    TO ls_receivers-to_answer.
*  MOVE ''                    TO ls_receivers-to_do_expl.
*  MOVE ''                    TO ls_receivers-to_do_grp.
*  MOVE ''                    TO gs_receivers-com_type.
*  MOVE ''                    TO ls_receivers-fax.
*  MOVE ''                    TO ls_receivers-country.
*  MOVE ''                    TO ls_receivers-spool_id.
*  MOVE ''                    TO gs_receivers-notif_del.
*  MOVE ''                    TO ls_receivers-notif_read.
*  MOVE ''                    TO gs_receivers-notif_ndel.
*  MOVE ''                    TO ls_receivers-sap_body.

        search this website

Regotz Michel