Emailversand eines PDF Spool in Update Task

Nützliche Abap Codes und Code Fragmente

Emailversand eines PDF Spool in Update Task

Postby Tron » Wed Jan 29, 2020 2:40 pm

Y_BC_EMAIL_POST.png
Y_BC_EMAIL_POST.png (164.03 KiB) Viewed 871 times

Code: [Select all] [Expand/Collapse] [Download] (Untitled.txt)
  1. FUNCTION y_bc_email_post.
  2. *"----------------------------------------------------------------------
  3. *"*"Update Function Module:
  4. *"
  5. *"*"Local Interface:
  6. *"  IMPORTING
  7. *"     VALUE(I_RQIDENT) TYPE  RSPOID
  8. *"     VALUE(I_SMTP_ADDR) TYPE  AD_SMTPADR
  9. *"     VALUE(I_SENDER) TYPE  AD_SMTPADR OPTIONAL
  10. *"     VALUE(TT_TEXT) TYPE  BCSY_TEXT OPTIONAL
  11. *"     VALUE(I_SUBJECT) TYPE  SO_OBJ_DES
  12. *"     VALUE(I_FILENAME) TYPE  SO_OBJ_DES OPTIONAL
  13. *"     VALUE(I_USE_NOREPLY) TYPE  XFELD OPTIONAL
  14. *"----------------------------------------------------------------------
  15.  
  16.   DATA lv_len TYPE i.
  17.   DATA pdf_xstring TYPE xstring.
  18. *     -------- convert document -------------------------------
  19.   DATA pdf_content TYPE TABLE OF solix.
  20. * for email contents
  21.   DATA lv_internetadr TYPE adr6-smtp_addr.
  22.   DATA lv_attname TYPE sood-objdes.
  23.  
  24.   DATA  send_request       TYPE REF TO cl_bcs.
  25.   DATA  ls_soli            TYPE soli.
  26.   DATA  document           TYPE REF TO cl_document_bcs.
  27.   DATA  recipient          TYPE REF TO if_recipient_bcs.
  28.   DATA  bcs_exception      TYPE REF TO cx_bcs.
  29.   DATA  sent_to_all        TYPE os_boolean.
  30.   DATA  sender             TYPE REF TO if_sender_bcs.
  31.   DATA  l_sendadr          TYPE adr6-smtp_addr.
  32.  
  33.   CALL FUNCTION 'CONVERT_OTFSPOOLJOB_2_PDF'
  34.     EXPORTING
  35.       src_spoolid              = i_rqident
  36.       no_dialog                = 'X'
  37. *     DST_DEVICE               =
  38.       pdf_destination          = 'X'
  39. *     NO_BACKGROUND            =
  40. *     USE_CASCADING            = ' '
  41.     IMPORTING
  42.       pdf_bytecount            = lv_len
  43. *     PDF_SPOOLID              =
  44. *     OTF_PAGECOUNT            =
  45. *     BTC_JOBNAME              =
  46. *     BTC_JOBCOUNT             =
  47.       bin_file                 = pdf_xstring
  48. * TABLES
  49. *     PDF                      =
  50.     EXCEPTIONS
  51.       err_no_otf_spooljob      = 1
  52.       err_no_spooljob          = 2
  53.       err_no_permission        = 3
  54.       err_conv_not_possible    = 4
  55.       err_bad_dstdevice        = 5
  56.       user_cancelled           = 6
  57.       err_spoolerror           = 7
  58.       err_temseerror           = 8
  59.       err_btcjob_open_failed   = 9
  60.       err_btcjob_submit_failed = 10
  61.       err_btcjob_close_failed  = 11
  62.       OTHERS                   = 12.
  63.  
  64.   IF sy-subrc <> 0.
  65.   ENDIF.
  66.  
  67.  
  68.   pdf_content = cl_document_bcs=>xstring_to_solix( pdf_xstring ).
  69.  
  70.   TRY.
  71. *     create the send request
  72.       send_request = cl_bcs=>create_persistent( ).
  73.  
  74. *     email subject
  75.       document = cl_document_bcs=>create_document(
  76.                                 i_type    = 'RAW'
  77.                                 i_text    = tt_text
  78.                                 i_subject = i_subject ).
  79.  
  80. *     add list attachment to document
  81.       IF i_filename IS INITIAL.
  82.         CONCATENATE i_subject '.pdf' INTO lv_attname.
  83.       ELSE.
  84.         CONCATENATE i_filename '.pdf' INTO lv_attname.
  85.       ENDIF.
  86.       document->add_attachment( i_attachment_type    = 'PDF'
  87.                                 i_attachment_subject = lv_attname
  88.                                 i_att_content_hex    = pdf_content ).
  89.  
  90. *=============== prep send
  91.  
  92. *     add document to send request
  93.       send_request->set_document( document ).
  94.  
  95. *     create recipient and add to send request
  96.  
  97.       recipient = cl_cam_address_bcs=>create_internet_address( i_smtp_addr ).
  98.  
  99.       send_request->add_recipient( i_recipient = recipient ).
  100.  
  101. *    create sender and add to send request
  102.       IF NOT i_use_noreply IS INITIAL.
  103.         CONCATENATE 'Noreply@' sy-sysid '-' sy-mandt '.de' INTO l_sendadr.
  104.         sender = cl_cam_address_bcs=>create_internet_address( l_sendadr ).
  105.         send_request->set_sender( EXPORTING i_sender = sender ).
  106.       ELSE.
  107.         IF NOT i_sender IS INITIAL.
  108.           l_sendadr = i_sender.
  109.           sender = cl_cam_address_bcs=>create_internet_address( l_sendadr ).
  110.           send_request->set_sender( EXPORTING i_sender = sender ).
  111.         ENDIF.
  112.       ENDIF.
  113.  
  114. *     send
  115.       send_request->set_send_immediately( i_send_immediately = 'X' ).
  116. *      sent_to_all = send_request->send( i_with_error_screen = 'X' ).
  117.  
  118.     CATCH cx_bcs INTO bcs_exception.
  119.       MESSAGE s865(so) WITH bcs_exception->error_type DISPLAY LIKE 'E'.
  120.   ENDTRY.
  121.  
  122. *    commit work and wait.
  123.  
  124.  
GeSHi ©

001.png
001.png (19.76 KiB) Viewed 560 times

Code: [Select all] [Expand/Collapse] [Download] (Untitled.txt)
  1. *&---------------------------------------------------------------------*
  2. *& Program  ZMEDRUCK_EMAIL
  3. *&
  4. *&---------------------------------------------------------------------*
  5. *&
  6. *&
  7. *&---------------------------------------------------------------------*
  8.  
  9. PROGRAM zmedruck_email_bds.
  10.  
  11. PARAMETERS p_email TYPE text80 LOWER CASE.
  12. PARAMETERS p_sender TYPE text80 LOWER CASE.
  13. PARAMETERS p_norep AS CHECKBOX.
  14.  
  15. PARAMETERS p_spool TYPE itcpp-tdspoolid."Spool number for tesing
  16. PARAMETERS g_debug AS CHECKBOX.         "Sapscript Debugger active
  17.  
  18. PERFORM test.
  19.  
  20. * WARNING : NO POPUPS - NO COMMIT WORK IN HERE !!!!!!!!!!!!!!!!!!!!!!!!!
  21.  
  22.  
  23. * How to do (i left some TODO-comments)
  24. * 1.) Change/verify the "PERFORM" so the "TRUE" Print Routine is called
  25. *     (only needed if you have an own ME_DRUCK) just look table TNAPR
  26. * 2.) Change the Receiver Email-adress.
  27. * 3.) Activate ZMEDRUCK_EMAIL :-)
  28. * 4.) Customize (Tc NACE) the message-Type "NEU" and enter
  29. *     "ZMEDRUCK_EMAIL" as Program and "ENTRY_NEU" as FORM
  30.  
  31. * How it is working (short version)
  32. * by customizing, the original Print routine is substituted by this one
  33. * to retrieve the SpoolId, which we need for PDF generation and email.
  34. * ZMEDRUCK_EMAIL_BDS calls the "Original" Print routine as a subroutine.
  35. * After this step the Program retrieves the Spool created.
  36. * Then the OTF is converted into PDF and attached to the mail
  37.  
  38.  
  39. DATA: retcode   LIKE sy-subrc.         "Returncode
  40. DATA: xscreen(1) TYPE c.               "Output on printer or screen
  41. TABLES: nast,                          "Messages
  42.         tnapr.                         "Programs & Forms
  43.  
  44. *&---------------------------------------------------------------------*
  45. *&      Form  ENTRY
  46. *&---------------------------------------------------------------------*
  47. *       text
  48. *----------------------------------------------------------------------*
  49. *      -->RETURN_CODE  text
  50. *      -->US_SCREEN    text
  51. *----------------------------------------------------------------------*
  52. FORM entry_neu USING return_code TYPE i
  53.                  us_screen TYPE c.
  54.  
  55.   FIELD-SYMBOLS: <f> TYPE itcpp,
  56.                  <p> TYPE  ssfcrescl.
  57.  
  58.   DATA pgnam TYPE na_pgnam." Programname
  59.   DATA ronam TYPE na_ronam."Formname
  60.   DATA fm_name TYPE rs38l_fnam. "genr.Druckbaustein
  61.   DATA lv_vec(80).
  62.   DATA spoolid TYPE rspoid.
  63.  
  64.   CLEAR retcode.
  65.   xscreen = us_screen.
  66.  
  67. *              Call the "Original" Printroutine
  68. ************************
  69. * @@@@ TODO !!!!!
  70. ************************ /SAMFORMS/POORDER
  71. *-real program---------+
  72. *                      |
  73. *-real Form---+        |
  74. *             |        |
  75.   CASE nast-kschl.
  76.     WHEN 'NEU'.
  77.       PERFORM entry_neu(sapfm06p) USING return_code us_screen.
  78.     WHEN 'YNCS'.
  79.       PERFORM entry_neu(sapfm06p) USING return_code us_screen.
  80.     WHEN 'ZMME'.
  81.       PERFORM entry_neu(zmm_po_email_nast) USING return_code us_screen.
  82.     WHEN OTHERS.
  83.  
  84.   PERFORM read_variant.
  85.  
  86. * use Test case in Preview mode and if SAPscript debugger is active
  87.   IF NOT us_screen IS INITIAL AND NOT g_debug IS INITIAL.
  88.  
  89.     spoolid = p_spool.
  90.  
  91.     PERFORM processing USING spoolid.
  92.     EXIT.
  93.   ENDIF.
  94.  
  95. * Try Sapscript
  96.   ASSIGN ('(SAPLSTXC)ITCPP') TO <f>.
  97.   IF sy-subrc = 0 AND
  98.      NOT <f>-tdspoolid IS INITIAL AND
  99.      us_screen IS INITIAL.
  100.  
  101.     PERFORM processing USING <f>-tdspoolid.
  102.     UNASSIGN <f>.
  103.  
  104.   ELSE.
  105. * Try Smartforms
  106.     CALL FUNCTION 'SSF_FUNCTION_MODULE_NAME'
  107.       EXPORTING
  108.         formname           = tnapr-sform
  109.       IMPORTING
  110.         fm_name            = fm_name
  111.       EXCEPTIONS
  112.         no_form            = 1
  113.         no_function_module = 2
  114.         OTHERS             = 3.
  115.  
  116.     IF sy-subrc <> 0.
  117.       retcode = 1.
  118.       EXIT.
  119.     ENDIF.
  120.  
  121.     SELECT SINGLE pname FROM tfdir INTO lv_vec
  122.     WHERE funcname = fm_name.
  123.     IF sy-subrc = 0.
  124.  
  125.       CONCATENATE '(' lv_vec ')JOB_OUTPUT_INFO' INTO lv_vec.
  126.       ASSIGN (lv_vec) TO <p>.
  127.  
  128.       IF sy-subrc = 0 AND
  129.       NOT <p>-spoolids[] IS INITIAL AND
  130.       us_screen IS INITIAL.
  131.  
  132.         LOOP AT <p>-spoolids INTO spoolid.
  133.           PERFORM processing USING spoolid.
  134.           EXIT.
  135.         ENDLOOP.
  136.         UNASSIGN <p>.
  137.  
  138.       ENDIF. " have Ids
  139.  
  140.     ENDIF." tfdir entry
  141.  
  142.   ENDIF." sapscript/smartforms
  143.  
  144.  
  145.   IF retcode NE 0.
  146.     return_code = 1.
  147.   ELSE.
  148.     return_code = 0.
  149.   ENDIF.
  150.  
  151.  
  152. ENDFORM.                    "ENTRY
  153.  
  154. *&---------------------------------------------------------------------*
  155. *&      Form  PROCESSING
  156. *&---------------------------------------------------------------------*
  157. *       text
  158. *----------------------------------------------------------------------*
  159. FORM processing USING spoolid TYPE itcpp-tdspoolid.
  160.  
  161.   DATA l_upd TYPE sy-subrc.
  162.   DATA l_spnr TYPE rspoid.
  163.   DATA lsmtp_addr TYPE  ad_smtpadr .
  164.   DATA lsubject TYPE so_obj_des.
  165.   DATA l_sender TYPE ad_smtpadr VALUE 'Sender@xy.sap'.
  166.   DATA l_use_noreply TYPE xfeld VALUE 'X'.
  167.  
  168.   DATA l_ebeln TYPE ekpa-ebeln.
  169.   DATA l_lifnr TYPE lfa1-lifnr.
  170.   DATA l_adrnr TYPE lfa1-adrnr.
  171.  
  172.   DATA tt_soli TYPE bcsy_text.
  173.   DATA s_soli TYPE soli.
  174. *----------------------------------------------------------------------*
  175. * read vendor email
  176. *----------------------------------------------------------------------*
  177.   CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
  178.     EXPORTING
  179.       input  = nast-parnr
  180.     IMPORTING
  181.       output = l_lifnr.
  182.  
  183.   SELECT SINGLE adrnr FROM lfa1 INTO l_adrnr WHERE
  184.     lifnr = l_lifnr.
  185.   IF NOT l_adrnr IS INITIAL.
  186.     SELECT SINGLE smtp_addr FROM adr6 INTO lsmtp_addr WHERE
  187.       addrnumber = l_adrnr AND flgdefault = 'X'.
  188.   ENDIF.
  189.  
  190. * no email found - exit
  191.   IF lsmtp_addr IS INITIAL.
  192.     lsmtp_addr = p_email. "use Fallback receiver email
  193.   ENDIF.
  194.  
  195.   IF NOT g_debug IS INITIAL.
  196.     lsmtp_addr = p_email. "use Fallback receiver email
  197.   ENDIF.
  198.  
  199.   IF lsmtp_addr IS INITIAL.
  200.     EXIT."no receiver email
  201.   ENDIF.
  202.  
  203. *----------------------------------------------------------------------*
  204. * create Email subject
  205. *----------------------------------------------------------------------*
  206.   lsubject = 'Purchase Order'.
  207.   IF nast-spras = 'D'.
  208.     lsubject = 'Bestellung'.
  209.   ENDIF.
  210.  
  211.   IF NOT g_debug IS INITIAL.
  212.     CONCATENATE 'TEST:' lsubject INTO lsubject SEPARATED BY space..
  213.   ENDIF.
  214.  
  215.   CONCATENATE lsubject nast-objky INTO lsubject SEPARATED BY space.
  216.  
  217. *----------------------------------------------------------------------*
  218. * create Email body
  219. *----------------------------------------------------------------------*
  220.   DATA s_tk TYPE stxh_key.
  221.   DATA t_text TYPE TABLE OF tline.
  222.   DATA s_text TYPE tline.
  223.  
  224.   s_tk-tdobject = 'EKKO'.
  225.   s_tk-tdid = 'F21'.
  226.   s_tk-tdspras = nast-spras.
  227.   s_tk-tdname = nast-objky.
  228.  
  229.   CALL FUNCTION 'READ_TEXT'
  230.     EXPORTING
  231.       id                      = s_tk-tdid
  232.       language                = s_tk-tdspras
  233.       name                    = s_tk-tdname
  234.       object                  = s_tk-tdobject
  235.     TABLES
  236.       lines                   = t_text
  237.     EXCEPTIONS
  238.       id                      = 1
  239.       language                = 2
  240.       name                    = 3
  241.       not_found               = 4
  242.       object                  = 5
  243.       reference_check         = 6
  244.       wrong_access_to_archive = 7
  245.       OTHERS                  = 8.
  246.   IF sy-subrc <> 0.
  247.   ELSE.
  248.     LOOP AT t_text INTO s_text.
  249.       s_soli = s_text-tdline.
  250.       APPEND s_soli TO tt_soli.
  251.     ENDLOOP.
  252.   ENDIF.
  253.  
  254.   l_spnr = spoolid.
  255.  
  256.   l_sender = p_sender." Sender Email address
  257.  
  258.  
  259. *----------------------------------------------------------------------*
  260. * Post Email
  261. *----------------------------------------------------------------------*
  262. * Verbucher aktiv ?
  263.   CALL FUNCTION 'TH_IN_UPDATE_TASK'
  264.     IMPORTING
  265.       in_update_task = l_upd.
  266.  
  267.   IF l_upd IS INITIAL.
  268.     CALL FUNCTION 'Y_BC_EMAIL_POST'
  269.       EXPORTING
  270.         i_rqident     = l_spnr
  271.         i_smtp_addr   = lsmtp_addr
  272.         i_sender      = l_sender
  273.         tt_text       = tt_soli
  274.         i_subject     = lsubject
  275.         i_use_noreply = p_norep.
  276.  
  277.     COMMIT WORK AND WAIT.
  278.  
  279.   ELSE.
  280.  
  281.     CALL FUNCTION 'Y_BC_EMAIL_POST' IN UPDATE TASK
  282.       EXPORTING
  283.         i_rqident     = l_spnr
  284.         i_smtp_addr   = lsmtp_addr
  285.         i_sender      = l_sender
  286.         tt_text       = tt_soli
  287.         i_subject     = lsubject
  288.         i_use_noreply = p_norep.
  289.  
  290.   ENDIF.
  291.  
  292. ENDFORM.                    "processing
  293. *----------------------------------------------------------------------*
  294. * Read Variant DEFAULT with initialization parameters
  295. *----------------------------------------------------------------------*
  296.  
  297. FORM read_variant.
  298.   TYPES: BEGIN OF ty_info,
  299.            typ(1),
  300.            null(1),
  301.            line(120),
  302.          END OF ty_info.
  303.  
  304. * für autom. Variante TEST laden
  305.   DATA:
  306.     l_report   TYPE raldb_repo ##NEEDED,
  307.     l_variante TYPE raldb_vari ##NEEDED,
  308.     t_info     TYPE TABLE OF ty_info ##NEEDED.
  309.  
  310. ** Load variant sy-uname
  311.   l_report   = sy-repid.
  312.   l_variante = 'DEFAULT'.
  313.  
  314.  
  315.   CALL FUNCTION 'RS_COVERPAGE_SELECTIONS'
  316.     EXPORTING
  317.       report  = l_report
  318.       variant = l_variante
  319.     TABLES
  320.       infotab = t_info
  321.     EXCEPTIONS
  322.       OTHERS  = 3 ##FM_SUBRC_OK.
  323.  
  324.  
  325. FORM test.
  326.   DATA rc TYPE i.
  327.  
  328.   SELECT SINGLE * FROM nast INTO nast WHERE objky = '4712043568' .
  329.   nast-vstat = '0'.
  330.  
  331.   SELECT SINGLE * FROM tnapr INTO tnapr WHERE
  332.     kschl = 'YNCS' AND
  333.     nacha = '1' AND
  334.     kappl = 'EF'.
  335.  
  336.   PERFORM entry_neu USING rc ' '.
  337.  
  338.  
  339.  
GeSHi ©


Email2.png
Email2.png (55.45 KiB) Viewed 1211 times
Tron
.....
.....
 
Posts: 1098
Joined: Sat Aug 04, 2007 10:21 pm

Return to Code Snippets

Who is online

Users browsing this forum: No registered users and 1 guest

cron
  • Latest Topics