Sending an E-mail in HTML Format

  Sending an E-mail in HTML Format:

*&---------------------------------------------------------------------*
*& Report ZVB_HTML_MAIL
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT zvb_html_mail.

*DATA DECLARATIONS
DATAmaildata LIKE sodocchgi1,
      mailtxt  LIKE solisti1 OCCURS 10 WITH HEADER LINE,
      mailrec  LIKE somlrec90 OCCURS WITH HEADER LINE.

DATA:lt_emp TYPE TABLE OF zvbemp WITH HEADER LINE,
     lw_emp TYPE zvbemp.

START-OF-SELECTION.

  CLEAR:maildata,mailtxt,mailrec.
  REFRESH:mailtxt,mailrec.

*Selection parameters
  PARAMETERS:p_empdep TYPE zvbemp-emp_department.
  SELECT *
   FROM zvbemp
    INTO TABLE lt_emp
     WHERE emp_department p_empdep.
  SORT lt_emp BY emp_id ASCENDING.

*READ TABLE lt_emp INTO lw_emp INDEX 1.

  maildata-obj_descr | Employee details of Department{ p_empdep } |.

  mailtxt '<!DOCTYPE html>'.
  APPEND mailtxt.
  mailtxt '<html style="background-color:red">'.
  APPEND mailtxt.
  mailtxt '<style>table, th, td {border:1px solid black; border-collapse:collapse}</style>'.
  APPEND mailtxt.
  mailtxt '<body style="background-color:blue">'.
  APPEND mailtxt.
  mailtxt '<p>Dear User,</p>'.
  APPEND mailtxt.
  mailtxt '<p>Please find the below details of Employees.</p>'.
  APPEND mailtxt.
  mailtxt-line |<p> Employee details of Department { p_empdep }</p> |.
  APPEND mailtxt.
  mailtxt '<table>'." style="width:100%".</p>'.
  APPEND mailtxt.
  mailtxt '<tr>'.
  APPEND mailtxt.
  mailtxt '<th style = "border:1px solid black; border-collapse:collapse;">Employee ID</th>'.
  APPEND mailtxt.
  mailtxt '<th style = "border:1px solid black; border-collapse:collapse;">Employee First Name</th>'.
  APPEND mailtxt.
  mailtxt '<th style = "border:1px solid black">Employee Last Name</th>'.
  APPEND mailtxt.
  mailtxt '<th style = "border:1px solid black">Employee Department</th>'.
  APPEND mailtxt.
  mailtxt '<th style = "border:1px solid black">Employee Address</th>'.
  APPEND mailtxt.
  mailtxt '<th style = "border:1px solid black">Employee Job Location</th>'.
  APPEND mailtxt.
  mailtxt '</tr>'.
  APPEND mailtxt.
  LOOP AT lt_emp INTO lw_emp.
    mailtxt '<tr>'.
    APPEND mailtxt.
    mailtxt-line | <td style "border:1px solid black">{ lw_emp-emp_id }</td> |.
    APPEND mailtxt.
    mailtxt-line | <td style "border:1px solid black">{ lw_emp-emp_firstname }</td> |.
    APPEND mailtxt.
    mailtxt-line | <td style "border:1px solid black">{ lw_emp-emp_lastname }</td>|.
    APPEND mailtxt.
    mailtxt-line | <td style "border:1px solid black">{ lw_emp-emp_department }</td> |.
    APPEND mailtxt.
    mailtxt-line | <td style "border:1px solid black">{ lw_emp-emp_address }</td> |.
    APPEND mailtxt.
    mailtxt-line | <td style "border:1px solid black">{ lw_emp-emp_job_location }</td> |.
    APPEND mailtxt.
    mailtxt '</tr>'.
    APPEND mailtxt.
  ENDLOOP.
  mailtxt '</table>'.
  APPEND mailtxt.
  mailtxt '</body>'.
  APPEND mailtxt.
  mailtxt '<p style = "color:red"><b>This is a system generated mail.Please do not reply</b></p>'.
  APPEND mailtxt.
  mailtxt '</html>'.
  APPEND mailtxt.

  mailrec-receiver 'abc@gmail.com'.
  mailrec-rec_type 'U'.
  APPEND mailrec.
*  mailrec-receiver = 'def@gmail.com'.
*  mailrec-rec_type = 'U'.
*  APPEND mailrec.

  CALL FUNCTION 'SO_NEW_DOCUMENT_SEND_API1'
    EXPORTING
      document_data              maildata
      document_type              'HTM'
*     PUT_IN_OUTBOX              = ' '
*     COMMIT_WORK                = ' '
*     IP_ENCRYPT                 =
*     IP_SIGN                    =
* IMPORTING
*     SENT_TO_ALL                =
*     NEW_OBJECT_ID              =
    TABLES
      object_header              mailtxt
      object_content             mailtxt
*     CONTENTS_HEX               =
*     OBJECT_PARA                =
*     OBJECT_PARB                =
      receivers                  mailrec
    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.
* Implement suitable error handling here
    MESSAGE 'Mail not sent' TYPE 'E'.
  ELSE.
    WRITE:'Mail sent successfully'.
  ENDIF.
  COMMIT WORK.

Output:


Comments

Popular posts from this blog

Passing Dynamic Variables to SO10 TEXT

Implicit Enhancement with Example

TMG Events