专栏首页Jerry的SAP技术分享使用SAP connect发送HTML格式的邮件

使用SAP connect发送HTML格式的邮件

*&---------------------------------------------------------------------*
*& Report  ZTEST_SEND_MAIL_HTML
*&---------------------------------------------------------------------*
*& This report will show how to send the Formatted Emails using 
*&   SAPConnect 
*&---------------------------------------------------------------------*
REPORT  ztest_np_send_mail.

DATA:
t_objbin   TYPE STANDARD TABLE OF solisti1,   " Attachment data
t_objtxt   TYPE STANDARD TABLE OF solisti1,   " Message body
t_objpack  TYPE STANDARD TABLE OF sopcklsti1, " Packing list
t_reclist  TYPE STANDARD TABLE OF somlreci1,  " Receipient list
t_objhead  TYPE STANDARD TABLE OF solisti1.   " Header

DATA: wa_docdata TYPE sodocchgi1,   " Document data
      wa_objtxt  TYPE solisti1,     " Message body
      wa_objbin  TYPE solisti1,     " Attachment data
      wa_objpack TYPE sopcklsti1,   " Packing list
      wa_reclist TYPE somlreci1.    " Receipient list

DATA: w_tab_lines TYPE i.           " Table lines

* Selection Screen
PARAMETERS: p_email TYPE char120 obligatory
                VISIBLE LENGTH 40
                LOWER CASE.

* Start-of-selection
START-OF-SELECTION.

* Creating message
  PERFORM create_message.

* Sending Message
  PERFORM send_message.

*&---------------------------------------------------------------------*
*&      Form  create_message
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM create_message .

**1 Title, Description & Body
  PERFORM create_title_desc_body.

**2 Receivers
  PERFORM fill_receivers.

ENDFORM.                    " create_message

*&---------------------------------------------------------------------*
*&      Form  CREATE_TITLE_DESC_BODY
*&---------------------------------------------------------------------*
*       Title, Description and body
*----------------------------------------------------------------------*
FORM create_title_desc_body.

*...Title
  wa_docdata-obj_name  = 'Email notification'.

*...Description
  wa_docdata-obj_descr = 'Email body in HTML'.

*...Message Body in HMTL
  wa_objtxt-line = '<html> <body style="background-color:#FFE4C4;">'.
  APPEND wa_objtxt TO t_objtxt.

  wa_objtxt-line = '<p> List of Test materials </p>'.
  APPEND wa_objtxt TO t_objtxt.

*   table display
  wa_objtxt-line = '<table style="MARGIN: 10px" bordercolor="#90EE90" '.
  APPEND wa_objtxt TO t_objtxt.
  wa_objtxt-line = ' cellspacing="0" cellpadding="3" width="400"'.
  APPEND wa_objtxt TO t_objtxt.
  wa_objtxt-line = ' border="1"><tbody><tr>'.
  APPEND wa_objtxt TO t_objtxt.

*   table header
  wa_objtxt-line = '<th bgcolor="#90EE90">Material</th>'.
  APPEND wa_objtxt TO t_objtxt.
  wa_objtxt-line = '<th bgcolor="#90EE90">Description</th></tr>'.
  APPEND wa_objtxt TO t_objtxt.

*   table Contents
  DO 5 TIMES.
    wa_objtxt-line = '<tr style="background-color:#eeeeee;"><td>TEST</td>'.
    APPEND wa_objtxt TO t_objtxt.
    CONCATENATE '<td>' sy-abcde '</td> </tr>' INTO wa_objtxt-line.
    APPEND wa_objtxt TO t_objtxt.
  ENDDO.

*   table close
  wa_objtxt-line = '</tbody> </table>'.
  APPEND wa_objtxt TO t_objtxt.

*   Hyperlink
  wa_objtxt-line = '<br> <br>'.
  APPEND wa_objtxt TO t_objtxt.
  wa_objtxt-line = '<p><a href="http://help-abap.blogspot.com">'.
  APPEND wa_objtxt TO t_objtxt.
  wa_objtxt-line = 'Click here to check the latest post</a></p>'.
  APPEND wa_objtxt TO t_objtxt.

*   Signature with background color
  wa_objtxt-line = '<br><br>'.
  APPEND wa_objtxt TO t_objtxt.
  wa_objtxt-line = '<p> Regards,</p>'.
  APPEND wa_objtxt TO t_objtxt.
  wa_objtxt-line = '<p style="background-color:#1E90FF;"><b>Naimesh Patel</b></p>'.
  APPEND wa_objtxt TO t_objtxt.


*   HTML close
  wa_objtxt-line = '</body> </html> '.
  APPEND wa_objtxt TO t_objtxt.

* Document data
  DESCRIBE TABLE t_objtxt      LINES w_tab_lines.
  READ     TABLE t_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 t_objpack.

ENDFORM.                    " CREATE_TITLE_DESC_BODY

*&---------------------------------------------------------------------*
*&      Form  fill_receivers
*&---------------------------------------------------------------------*
*       Filling up the Receivers
*----------------------------------------------------------------------*
FORM fill_receivers .

  wa_reclist-receiver = p_email.
  wa_reclist-rec_type = 'U'.
  APPEND wa_reclist TO t_reclist.
  CLEAR  wa_reclist.


ENDFORM.                    " fill_receivers
*&---------------------------------------------------------------------*
*&      Form  send_message
*&---------------------------------------------------------------------*
*       Sending Mail
*----------------------------------------------------------------------*
FORM send_message .
	
* Send Message to external Internet ID
  CALL FUNCTION 'SO_NEW_DOCUMENT_ATT_SEND_API1'
    EXPORTING
      document_data              = wa_docdata
      put_in_outbox              = 'X'
      commit_work                = 'X'     "used from rel.6.10
    TABLES
      packing_list               = t_objpack
      object_header              = t_objhead
      contents_txt               = t_objtxt
      receivers                  = t_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 NE 0.
    WRITE: 'Sending Failed'.
  ELSE.
    WRITE: 'Sending Successful'.
  ENDIF.


ENDFORM.                    " send_message

要获取更多Jerry的原创文章,请关注公众号"汪子熙":

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • 使用ABAP代码创建SAP S/4HANA系统的扩展字段

    Besides S4 extension tool in Fiori UI, you can also create extension field in ba...

    Jerry Wang
  • 编程面试题:编写一个会造成数据库死锁的应用

    相信对于”开发一个会产生死锁的Java应用”这类需求,大家都能顺利完成。但是如果题目要求得更具体一些,要求这个死锁发生在数据库层面,应该怎样完成呢?

    Jerry Wang
  • 使用ABAP代码创建SAP S/4HANA系统的扩展字段

    Besides S4 extension tool in Fiori UI, you can also create extension field in ba...

    Jerry Wang
  • python获取Linux信息

      刚开始学习Python,用Python写了一个获取Linux服务器信息的脚本,在debian和centos上测试通过。首先需要安装一个psutil库,在安装...

    py3study
  • 女乔布斯,颠覆血液检测的亿万富翁

    大数据文摘
  • 投不进球都是篮板的锅,这个用蒙特卡罗法做的篮板让你「百发百中」

    图上的这个篮板是一位名叫 Shane Wighton 的小哥做出来的。上个月,他在 YouTube 上放出了这段视频,点击量已经超过 437 万。从视频中可以看...

    机器之心
  • 代码审计 | Ecms定制版代码审计实战思路分享

    朋友买了一套php源码,要做类似于证书查询的功能,让我帮忙审计一下有没有高危的漏洞,仅挖掘getshell,告知我这个系统是由ecms也就是帝国cms简化来的,...

    7089bAt@PowerLi
  • gps卫星信号模拟器的特点介绍

    gps卫星信号模拟器是针对不同的用户机设计开发、生产测试、教学演示、测试验收、故障诊断等应用而推出的导航信号源。gps卫星信号模拟器可以模拟出GPS卫星导航定位...

    时频专家
  • vue.js打包后放到beego项目中,路由的统一设置

    版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

    hotqin888
  • 什么是UML?

    UML是统一建模语言(Unified Modeling Language)的缩写,它发表于1997年,综合了当时已经存在的面向对象的建模语言、方法和过程,是一个...

    唐怀瑟

扫码关注云+社区

领取腾讯云代金券