Wednesday, August 23, 2017

Report Output via Email html format

Sometimes we need to send a mail to vendor for pending materials. In our system we have a list of that and we are reading that data set from the application server. Now most of the time that list doesn’t contain proper Vendor name. So we need to call vendor master table to get proper vendor name and then prepare an internal table for that. Now the list may contain multiple vendors as well. The mail will be shooting to a particular vendor and the list of the pending materials will be for that particular vendor only. Hence the function module for mail sending will be inside the loop of vendor list.

In the following program we have developed a mail sending scenario by which a mail will be sent to vendors for pending materials. The mail contains a specific format where we have created a table to show the list of pending materials. Here we need to incorporate HTML code inside the internal table.


The input file inside the application server is as follows:


The required Code:

REPORT zsr_test NO STANDARD PAGE HEADING.

TYPES: BEGIN OF ty_inp,
         vend_code TYPE lfa1-lifnr,
         vend_name TYPE string,
         mat_code  TYPE string,
         mat_desc  TYPE string,
         qty       TYPE string,
         color     TYPE string,
       END OF ty_inp.
DATA: wa_inp TYPE ty_inp,
      it_inp TYPE TABLE OF ty_inp.

DATA: wa_out TYPE solisti1,
      it_out TYPE TABLE OF solisti1.

TYPES: BEGIN OF ty_lfa1,
         lifnr TYPE lfa1-lifnr,
         name1 TYPE lfa1-name1,
         name2 TYPE lfa1-name2,
         adrnr TYPE lfa1-adrnr,
       END OF ty_lfa1.
DATA: wa_lfa1 TYPE ty_lfa1,
      it_lfa1 TYPE TABLE OF ty_lfa1.

TYPES: BEGIN OF ty_adr6,
         addrnumber TYPE adr6-addrnumber,
         smtp_addr  TYPE adr6-smtp_addr,
       END OF ty_adr6.
DATA: wa_adr6 TYPE ty_adr6,
      it_adr6 TYPE TABLE OF ty_adr6.

DATA: file_name TYPE char100 VALUE 'D:\SUM\abap\Vend_req.TXT',
      text      TYPE string,
      wa_rec    TYPE somlreci1,
      it_rec    TYPE TABLE OF somlreci1,
      subject   TYPE sodocchgi1.

START-OF-SELECTION.
  PERFORM read_dataset.
  PERFORM get_vendor_details.
  PERFORM mail_output.

*&---------------------------------------------------------------------*
*&      Form  READ_DATASET
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM read_dataset .

  OPEN DATASET file_name FOR INPUT IN TEXT MODE ENCODING DEFAULT.
  DO.
    READ DATASET file_name INTO text.
    IF sy-subrc <> 0.
      EXIT.
    ENDIF.

    IF sy-index <> 1.
      SPLIT text AT '|' INTO wa_inp-vend_code
                             wa_inp-vend_name
                             wa_inp-mat_code
                             wa_inp-mat_desc
                             wa_inp-qty
                             wa_inp-color.

      CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
        EXPORTING
          input  = wa_inp-vend_code
        IMPORTING
          output = wa_inp-vend_code.

      APPEND wa_inp TO it_inp.
      CLEAR: wa_inp, text.
    ENDIF.
  ENDDO.
  CLOSE DATASET file_name.

ENDFORM.
*&---------------------------------------------------------------------*
*&      Form  GET_VENDOR_DETAILS
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM get_vendor_details .

  IF it_inp IS NOT INITIAL.
    SELECT lifnr name1 name2 adrnr
      FROM lfa1 INTO TABLE it_lfa1
      FOR ALL ENTRIES IN it_inp
      WHERE lifnr = it_inp-vend_code.

    IF sy-subrc = 0.
      SORT it_lfa1 BY lifnr.
    ENDIF.
  ENDIF.

ENDFORM.
*&---------------------------------------------------------------------*
*&      Form  MAIL_OUTPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM mail_output .

  IF it_inp IS NOT INITIAL.
    LOOP AT it_inp INTO wa_inp.

      ON CHANGE OF wa_inp-vend_code.
        wa_out-line = '<html><body><p><strong><u>'.
        APPEND wa_out TO it_out.
        CLEAR: wa_out.

        READ TABLE it_lfa1 INTO wa_lfa1
        WITH KEY lifnr = wa_inp-vend_code
        BINARY SEARCH.

        IF sy-subrc = 0.
          CONCATENATE 'Following materials are pending from Vendor:'
                      wa_lfa1-name1 wa_lfa1-name2
                      INTO wa_out-line SEPARATED BY space.
          APPEND wa_out TO it_out.
          CLEAR: wa_out.

          wa_out-line = '</u></strong></p>'.
          APPEND wa_out TO it_out.
          CLEAR: wa_out.
        ENDIF.
      ENDON.

      wa_out-line = '<table border="1" cellspacing="0" cellpadding="0"><tbody>'.
      APPEND wa_out TO it_out.
      CLEAR: wa_out.

      ON CHANGE OF wa_inp-vend_code.
        wa_out-line = '<tr><td><p><strong>Material</strong></p></td>'.
        APPEND wa_out TO it_out.
        CLEAR: wa_out.

        wa_out-line = '<td><p><strong>Description</strong></p></td>'.
        APPEND wa_out TO it_out.
        CLEAR: wa_out.

        wa_out-line = '<td width="100"><p><strong>Qty</strong></p></td></tr>'.
        APPEND wa_out TO it_out.
        CLEAR: wa_out.
      ENDON.


      SHIFT wa_inp-mat_code LEFT DELETING LEADING '0'.
      CONCATENATE '<tr><td><p align="right">'
                  wa_inp-mat_code
                  '</p></td>'
                  INTO wa_out-line.
      APPEND wa_out TO it_out.
      CLEAR: wa_out.

      CONCATENATE '<td>' wa_inp-mat_desc '</td>'
                  INTO wa_out-line.
      APPEND wa_out TO it_out.
      CLEAR: wa_out.

      CONCATENATE '<td width="100"><p align="right">'
                  wa_inp-qty
                  '</p></td></tr></tbody></table>'
                  INTO wa_out-line.
      APPEND wa_out TO it_out.
      CLEAR: wa_out.

      AT END OF vend_code.
        APPEND INITIAL LINE TO it_out.
        wa_out-line = '<br>This is auto generated mail so don''t reply.</body></html>'.
        APPEND wa_out TO it_out.
        CLEAR: wa_out.

        subject-obj_descr = 'Pending Material'.
        wa_rec-receiver = 'sandip.aim@gmail.com'.
        wa_rec-rec_type = 'U'.
        wa_rec-express  = 'X'.
        APPEND wa_rec TO it_rec.
        CLEAR: wa_rec.

        CALL FUNCTION 'SO_NEW_DOCUMENT_SEND_API1'
          EXPORTING
            document_data              = subject
            document_type              = 'HTM'
            put_in_outbox              = 'X'
            commit_work                = 'X'
          TABLES
            object_content             = it_out
            receivers                  = it_rec
          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.


        REFRESH: it_out, it_rec.
        CLEAR: wa_lfa1, wa_inp.
      ENDAT.
    ENDLOOP.
  ENDIF.

ENDFORM.

The Mail Output:


28 comments:

ERP Training said...

It was so nice article.I was really satisified by seeing this article sap videos.

ERP Training said...

Thank you. It is such a wonderful post. it has great information it is very useful for sap video training.

Anonymous said...

Thankyou for Sharing Great Information. It is Very Helpful Information on sap hana videos.

Anonymous said...

Fabulous..!! Thank you.The information you provided is much usful information on sap videos.

Anonymous said...

It was so nice article.I was really satisified by seeing this article sap tutorials videos.

Neethu S. said...

"ON CHANGE OF" is an obsolete statement and is not supported if we are using classes/ Object-oriented concepts. Is there any other statements/way to check whether a value has been changed or not(other than AT-NEW).

Unknown said...

Nice post, Problem solved. But can tell me how to attach PDF in mail??

Anonymous said...

Hi

I have used CL_BCS, in SOST I am able to view the HTML Body and XLS attachment but after processing it to external email (outlook) HTML Body message not received its coming as blank only XLS attachment present. Could you pls share your thoughts.

jabashree said...

Thanks for sharing this valuable information.I have read your blog and i got a very useful and knowledgeable information from your blog.You have done a great job.


SAP HR Training in Chennai


SAP SD Training in Chennai


BEST SAP BASIS Training in Chennai

Anonymous said...

Thankyou for Sharing Great Information. It is Very Helpful Information on sap learning videos.

ERP Training said...

Thank you. It is such a wonderful post. it has great information it is very useful for sap learning videos.

ERP Training said...

Thankyou for Sharing Great Information. It is Very Helpful Information on sap videos.

Anonymous said...

Thankyou for Sharing Great Information. It is Very Helpful Information on sap training videos for sale.

ERP Training said...

Thank you. It is such a wonderful post. it has great information it is very useful for sap training videos for sale.

Anonymous said...

Thank you. sir, Really I like your post on sap workflow training videos.

Anonymous said...

Thank you ,
Well it was nice post and very helpful information on sap workflow training videos.

logistic-solutions said...


Thank you for your post. This is excellent information. It is amazing and wonderful to visit your site.
sap strategic consulting and services

Anonymous said...

Thank you ,
Well it was nice post and very helpful information on sap hana training videos.

Anonymous said...

Thankyou for Sharing Great Information. It is Very Helpful Information on sap video.

Unknown said...

I like your post its very useful for me and I follow your all post .I am waiting for your next essential article.sap-mm training

Anonymous said...

If someone wish to become a pro in SAP ABAP , visit here

ERP Training said...

Thank you ,
Well it was nice post and very helpful information on sapvideos.

Anonymous said...

Great information. the information provided is of great

use as i got to learn new things. If you are looking for

real time training by industry experts with live projects

and placement support.kindly Visit THE CREATING EXPERTS.

http://thecreatingexperts.com/sap-successfactors-

training-in-chennai/


Reach our Technical Experts @ 8122241286 / 9003085882.

Anonymous said...

the creating experts

APTRON Delhi said...

Good Post. I like your blog. Thanks for Sharing good information.
SAP Training in Delhi

Azure DevOps said...

Your post is very useful for learners. Thanks for providing such a great post...
SAP HANA online training

rakesh said...

The information you have provided is very good, your content motivated us to learn some more things on the given topic SAP ABAP Online Training

Piloggroup said...

Very nice article.....Thanks for sharing the post....
We are providing the best master data services around the world....visit our site for more information....
Master Data Governance
Data Cleansing Services
data classification tools
Master Data Management Solutions
data transformation service
Material Master Data Management
Master Data Dictionary