Objective
- The requirement is to send internal table data in mail body instead of sending as attachment in mail. Mail body internal table data should display with different color’s based on conditions.
Advantage
- Not required to open mail attachment to see information on it, it displays mail body itself.
- Color indication based on conditions, it helps for better analysis.
Steps for sending internal table data in mail body.
- Getting data to internal table from data base table
- Create HTML mail content.
- Create Mail using SO_NEW_DOCUMENT_ATT_SEND_API1
1. Getting data to internal table from data base table
I am using data base table SFLIGHT for demo purpose
Code:
DATA : it_sflight TYPETABLEOF ty_sflight,
wa_sflight TYPE ty_sflight.
DATA : it_final TYPETABLEOF ty_final,
wa_final TYPE ty_final.
* Start-of-selection
START-OF-SELECTION.
SELECT carrid
connid
fldate
planetype
seatsmax
seatsocc
FROM sflight INTOTABLE it_sflight UPTO10ROWS.
SORT it_sflight BY fldate.
LOOPAT it_sflight INTO wa_sflight.
wa_final-carrid = wa_sflight-carrid.
wa_final-connid = wa_sflight-connid.
wa_final-fldate = wa_sflight-fldate.
wa_final-planetype = wa_sflight-planetype.
wa_final-seatsmax = wa_sflight-seatsmax.
wa_final-seatsocc = wa_sflight-seatsocc.
var = ( wa_sflight-seatsocc / wa_sflight-seatsmax ) * 100.
wa_final-percentage = var.
APPEND wa_final TO it_final.
CLEAR : wa_final, var.
ENDLOOP.
2. Create HTML mail content.
- The HTML content is prepared and filled in table it_final which would be later used to create HTML form.
- <html> tag tells the browser that this is an HTML document.
- <body> tag is used to defines the document's body.
- <font> tag is used to use to give the style to the content.
- <style> tag is used to define style information for an HTML document.
- <th> tag defines a header cell in an HTML table.
- <tr> tag defines a row in an HTML table.
Code:
*...Title
wa_docdata-obj_name = 'Email notification'.
*...Description
wa_docdata-obj_descr = 'Internal Table in Email Body'.
*...Message Body in HMTL
wa_objtxt-line = '<html> <body style="background-color:#FFE4C4;">'.
APPEND wa_objtxt TO it_objtxt.
wa_objtxt-line = '<p> List of Test materials </p>'.
APPEND wa_objtxt TO it_objtxt.
* table display
wa_objtxt-line = '<table style="MARGIN: 10px" bordercolor="NavajoWhite" '.
APPEND wa_objtxt TO it_objtxt.
wa_objtxt-line = ' cellspacing="0" cellpadding="3" width="800"'.
APPEND wa_objtxt TO it_objtxt.
wa_objtxt-line = ' border="1"><tbody><tr>'.
APPEND wa_objtxt TO it_objtxt.
* table header
wa_objtxt-line = '<th><font color="RoyalBlue">Airline Code</font></th>'.
APPEND wa_objtxt TO it_objtxt.
wa_objtxt-line = '<th><font color="RoyalBlue">Flight</font></th>'.
APPEND wa_objtxt TO it_objtxt.
wa_objtxt-line = '<th><font color="RoyalBlue">Flight date</font></th>'.
APPEND wa_objtxt TO it_objtxt.
wa_objtxt-line = '<th><font color="RoyalBlue">Aircraft Type</font></th>'.
APPEND wa_objtxt TO it_objtxt.
wa_objtxt-line = '<th><font color="RoyalBlue">Maximum capacity</font></th>'.
APPEND wa_objtxt TO it_objtxt.
wa_objtxt-line = '<th><font color="RoyalBlue">Occupied seats</font></th>'.
APPEND wa_objtxt TO it_objtxt.
wa_objtxt-line = '<th><font color="RoyalBlue">Occupied %</font></th></tr>'.
APPEND wa_objtxt TO it_objtxt.
* table Contents
LOOPAT it_final INTO wa_final.
wa_objtxt-line = '<tr>'.
APPEND wa_objtxt TO it_objtxt.
var = wa_final-percentage.
IF var GE'50'.
CONCATENATE'<td><center>' wa_final-carrid '</center></td>'INTO wa_objtxt-line.
APPEND wa_objtxt TO it_objtxt.
CONCATENATE'<td><center>' wa_final-connid '</center></td>'INTO wa_objtxt-line.
APPEND wa_objtxt TO it_objtxt.
CONCATENATE'<td><center>' wa_final-fldate '</center></td>'INTO wa_objtxt-line.
APPEND wa_objtxt TO it_objtxt.
CONCATENATE'<td><center>' wa_final-planetype '</center></td>'INTO wa_objtxt-line.
APPEND wa_objtxt TO it_objtxt.
CONCATENATE'<td><center>' wa_final-seatsmax '</center></td>'INTO wa_objtxt-line.
APPEND wa_objtxt TO it_objtxt.
CONCATENATE'<td><center>' wa_final-seatsocc '</center></td>'INTO wa_objtxt-line.
APPEND wa_objtxt TO it_objtxt.
CONCATENATE'<td><center>' wa_final-percentage '%''</center></td></tr>'INTO wa_objtxt-line.
APPEND wa_objtxt TO it_objtxt.
ELSE.
CONCATENATE'<td><center><font color="Red">' wa_final-carrid '</font></center></td>'INTO wa_objtxt-line.
APPEND wa_objtxt TO it_objtxt.
CONCATENATE'<td><center><font color="Red">' wa_final-connid '</font></center></td>'INTO wa_objtxt-line.
APPEND wa_objtxt TO it_objtxt.
CONCATENATE'<td><center><font color="Red">' wa_final-fldate '</font></center></td>'INTO wa_objtxt-line.
APPEND wa_objtxt TO it_objtxt.
CONCATENATE'<td><center><font color="Red">' wa_final-planetype '</font></center></td>'INTO wa_objtxt-line.
APPEND wa_objtxt TO it_objtxt.
CONCATENATE'<td><center><font color="Red">' wa_final-seatsmax '</font></center></td>'INTO wa_objtxt-line.
APPEND wa_objtxt TO it_objtxt.
CONCATENATE'<td><center><font color="Red">' wa_final-seatsocc '</font></center></td>'INTO wa_objtxt-line.
APPEND wa_objtxt TO it_objtxt.
CONCATENATE'<td><center><font color="Red">' wa_final-percentage '%''</font></center></td></tr>'INTO wa_objtxt-line.
APPEND wa_objtxt TO it_objtxt.
ENDIF.
CLEAR : wa_final, var.
ENDLOOP.
* table close
wa_objtxt-line = '</tbody> </table>'.
APPEND wa_objtxt TO it_objtxt.
* Signature color
wa_objtxt-line = '<br><br>'.
APPEND wa_objtxt TO it_objtxt.
wa_objtxt-line = '<p> Regards,</p>'.
APPEND wa_objtxt TO it_objtxt.
wa_objtxt-line = '<p><b> Your Name</b></p>'.
APPEND wa_objtxt TO it_objtxt.
* HTML close
wa_objtxt-line = '</body> </html> '.
APPEND wa_objtxt TO it_objtxt.
* Document data
DESCRIBETABLE it_objtxt LINES w_tab_lines.
READ TABLE it_objtxt INTO wa_objtxt INDEX w_tab_lines.
wa_docdata-doc_size =
( w_tab_lines - 1 ) * 255 + STRLEN( wa_objtxt ).
* Packing data
CLEAR wa_objpack-transf_bin.
wa_objpack-head_start = 1.
wa_objpack-head_num = 0.
wa_objpack-body_start = 1.
wa_objpack-body_num = w_tab_lines.
* we will pass the HTML, since we have created the message
* body in the HTML
wa_objpack-doc_type = 'HTML'.
APPEND wa_objpack TO it_objpack.
ENDFORM. " CREATE_TITLE_DESC_BODY
*&---------------------------------------------------------------------*
*& Form fill_receivers
*&---------------------------------------------------------------------*
* Filling up the Receivers
*----------------------------------------------------------------------*
FORM fill_receivers .
wa_reclist-receiver = 'ramesh@gmail.com'.
wa_reclist-rec_type = 'U'.
APPEND wa_reclist TO it_reclist.
CLEAR wa_reclist.
ENDFORM. " fill_receivers
3. Create Mailusing SO_NEW_DOCUMENT_ATT_SEND_API1
Now using SO_NEW_DOCUMENT_ATT_SEND_API1 to send message to external internet id.
* Send Message to external Internet ID
CALLFUNCTION'SO_NEW_DOCUMENT_ATT_SEND_API1'
EXPORTING
document_data = wa_docdata
put_in_outbox = 'X'
commit_work = 'X'
TABLES
packing_list = it_objpack
object_header = it_objhead
contents_txt = it_objtxt
receivers = it_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 NE0.
WRITE: 'Sending Failed'.
ELSE.
WRITE: 'Sending Successful'.
ENDIF.
Output in SOST.
Out put in Mail.
Source code attached in text file