ABAP-邮件发送程序

ABAP-邮件发送程序

说明: IT_ITAB 是要传送的EXCEL表格中的数据


data: IT_DOCUMENT_DATA TYPE SODOCCHGI1,
     IT_CONTENT_TEXT TYPE STANDARD TABLE OF SOLISTI1 WITH HEADER LINE,
      IT_PACKING_LIST TYPE TABLE OF SOPCKLSTI1 WITH HEADER LINE,
      IT_RECEIVERS TYPE STANDARD TABLE OF SOMLRECI1 WITH HEADER LINE,
      IT_ZPROGRAM_EMAIL TYPE TABLE OF ZPROGRAM_EMAIL WITH HEADER LINE,
      LC_MAIL_ATTACH TYPE STRING,
      LT_CONTENT_HEX TYPE STANDARD TABLE OF SOLIX WITH HEADER LINE,
    LT_OBJECT_HEADER TYPE STANDARD TABLE OF SOLISTI1 WITH HEADER LINE,
       LC_MAIL_XATTACH TYPE XSTRING,
       LV_SEND_ALL TYPE C,
       it_makt like TABLE OF makt WITH HEADER LINE,
       fir_date type sy-datum,
       MIMETYPE TYPE CHAR64.
DATA: IT_ITAB TYPE TABLE OF ITAB WITH HEADER LINE,
      wa_itab type itab.

CONSTANTS:LC_TAB TYPE C VALUE CL_BCS_CONVERT=>GC_TAB,
          LC_CTRL TYPE C VALUE CL_BCS_CONVERT=>GC_CRLF.

PARAMETER E_FLAG TYPE C AS CHECKBOX.

IF E_FLAG = 'X'.
perform SEND_EMAIL.
ENDIF.

*&---------------------------------------------------------------------*
*&      Form  SEND_EMAIL
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM SEND_EMAIL.
  data SY_VLINE TYPE I.
  DATA:
       MBLNR TYPE STRING,   "物料凭证
       MATNR TYPE STRING,      "物料
       MJAHR TYPE STRING,    "年度
       LGORT TYPE STRING,      "库位
       BWART TYPE STRING,     "移动类型
       ZEILE TYPE STRING,     "项目
       SOBKZ TYPE STRING,      "特许库存
       BUDAT TYPE STRING,      "过账日期
       MENGE TYPE STRING,      "数量
       MEINS TYPE STRING,       "单位

       CPUDT TYPE STRING,
       CPUTM TYPE STRING,
       BLDAT TYPE STRING,
       AUFNR TYPE STRING,
       WEMPF TYPE STRING,
       UMLGO TYPE STRING,
       GRUND TYPE STRING,
       LIFNR TYPE STRING,
       EBELN TYPE STRING,
       EBELP TYPE STRING,
       BKTXT TYPE STRING,
       CHARG TYPE STRING,
       XBLNR TYPE STRING,
       KOSTL TYPE STRING,

       DMBTR TYPE STRING,
       USNAM TYPE STRING,
       maktx TYPE STRING,    "物料描述
       MTART TYPE STRING    "物料类型
        .


  SELECT * INTO CORRESPONDING FIELDS OF TABLE IT_ZPROGRAM_EMAIL FROM ZPROGRAM_EMAIL WHERE ZPRO = SY-CPROG.

  " ZPROGRAM_EMAIL 表

  IT_DOCUMENT_DATA-OBJ_DESCR = 'DG1M物料凭证清单' .
  IT_DOCUMENT_DATA-PRIORITY = '1'.
  IT_DOCUMENT_DATA-OBJ_NAME = 'OFFER'.
  IT_DOCUMENT_DATA-OBJ_LANGU = SY-LANGU.

*  ****邮件正文信息
  IT_CONTENT_TEXT = '各位好:'.
  APPEND IT_CONTENT_TEXT.
  IT_CONTENT_TEXT = '               '.
  APPEND IT_CONTENT_TEXT.
  IT_CONTENT_TEXT = 'DG1M物料凭证清单请见附件!'.
  APPEND IT_CONTENT_TEXT.

  DESCRIBE TABLE IT_CONTENT_TEXT LINES SY_VLINE.
  IT_DOCUMENT_DATA-DOC_SIZE = 255 * ( SY_VLINE - 1 ) + STRLEN( IT_CONTENT_TEXT ).

  IT_PACKING_LIST-TRANSF_BIN = SPACE.
  IT_PACKING_LIST-HEAD_START = 1.
  IT_PACKING_LIST-HEAD_NUM = 0 .
  IT_PACKING_LIST-BODY_NUM = SY_VLINE.
  IT_PACKING_LIST-BODY_START = 1.
  IT_PACKING_LIST-DOC_TYPE = 'RAW'.
  APPEND IT_PACKING_LIST.


***收件人信息
  LOOP AT IT_ZPROGRAM_EMAIL.
    IT_RECEIVERS-RECEIVER = IT_ZPROGRAM_EMAIL-EMAIL.
    IT_RECEIVERS-REC_TYPE = 'U'.
    IT_RECEIVERS-COM_TYPE = 'INT'.
    IT_RECEIVERS-NOTIF_DEL =  'X'.
    IT_RECEIVERS-NOTIF_NDEL = 'X'.
    APPEND IT_RECEIVERS.
  ENDLOOP.

  LOOP AT IT_itab.
    AT FIRST.
      CONCATENATE '物料' LC_TAB
                  '物料凭证' LC_TAB
                  '物料类型' LC_TAB
                  '物料描述' LC_tab
                  '年度'  LC_TAB
                  '库位' LC_TAB
                  '移动类型' LC_TAB
                  '项目' LC_TAB
                  '特许库存' LC_TAB
                  '过账日期' LC_TAB
                  '数量' LC_TAB
                  '单位'  LC_TAB

                  '输入日期'   LC_TAB
                  '输入时间' LC_TAB
                  '凭证日期' LC_TAB
                  '订单' LC_TAB
                  '收货方' LC_TAB
                  '收货'  LC_TAB
                  '原因'  LC_TAB
                  '供应商'  LC_TAB
                  '采购订单'  LC_TAB
                  '项目'  LC_TAB
                  '凭证抬头文本'  LC_TAB
                  '批次'  LC_TAB
                  '参照'  LC_TAB
                  '成本中心'  LC_TAB
                  '本位币金额'  LC_TAB
                  '用户名称'  LC_CTRL
       INTO LC_MAIL_ATTACH.
    ENDAT.

    CLEAR:    MBLNR ,   MATNR ,  MJAHR , LGORT  ,  BWART  , ZEILE ,
    SOBKZ , BUDAT  , CPUDT , CPUDT , BLDAT , AUFNR ,WEMPF , UMLGO , GRUND , LIFNR , EBELN ,
    EBELP , BKTXT ,CHARG , XBLNR , KOSTL , DMBTR,  USNAM , MTART .

    MBLNR  = IT_ITAB-MBLNR .
    MATNR  = IT_ITAB-MATNR .
    MJAHR  = IT_ITAB-MJAHR .
    LGORT  = IT_ITAB-LGORT .
    BWART  = IT_ITAB-BWART .
    ZEILE  = IT_ITAB-ZEILE .
    SOBKZ  = IT_ITAB-SOBKZ .
    BUDAT  = IT_ITAB-BUDAT .

    CPUDT = IT_ITAB-CPUDT .
    CPUDT = IT_ITAB-CPUDT.
    BLDAT = IT_ITAB-BLDAT.
    AUFNR = IT_ITAB-AUFNR.
    WEMPF = IT_ITAB-WEMPF.
    UMLGO = IT_ITAB-UMLGO.
    GRUND = IT_ITAB-GRUND.
    LIFNR = IT_ITAB-LIFNR .
    EBELN = IT_ITAB-EBELN.
    EBELP = IT_ITAB-EBELP.
    BKTXT = IT_ITAB-BKTXT.
    CHARG = IT_ITAB-CHARG.
    XBLNR = IT_ITAB-XBLNR.
    KOSTL = IT_ITAB-KOSTL.

    DMBTR = IT_ITAB-DMBTR.
    USNAM = IT_ITAB-USNAM.
    maktx = it_itab-maktx.
    MTART = IT_ITAB-MTART .   "物料类型

    IF IT_ITAB-SHKZG = 'H'.
      MENGE  = IT_ITAB-MENGE .
      CONCATENATE '-' MENGE INTO MENGE.
    ELSE.
      MENGE  = IT_ITAB-MENGE .
    ENDIF.
    MEINS  = IT_ITAB-MEINS .

    CONCATENATE LC_MAIL_ATTACH
            MATNR  LC_TAB
            MBLNR  LC_TAB
            MTART  LC_TAB
            maktx  LC_TAB
            MJAHR  LC_TAB
            LGORT  LC_TAB
            BWART  LC_TAB
            ZEILE  LC_TAB
            SOBKZ  LC_TAB
            BUDAT  LC_TAB
            MENGE  LC_TAB
            MEINS  LC_TAB

            CPUDT   LC_TAB
            CPUDT   LC_TAB
            BLDAT  LC_TAB
            AUFNR   LC_TAB
            WEMPF   LC_TAB
            UMLGO   LC_TAB
            GRUND  LC_TAB
            LIFNR  LC_TAB
            EBELN  LC_TAB
            EBELP   LC_TAB
            BKTXT  LC_TAB
            CHARG   LC_TAB
            XBLNR  LC_TAB
            KOSTL  LC_TAB

            DMBTR   LC_TAB
            USNAM   LC_CTRL

       INTO LC_MAIL_ATTACH.
  ENDLOOP.

  MIMETYPE = 'APPLICATION/MSEXCEL;CHARSET=UTF-16LE'.
  CALL FUNCTION 'SCMS_STRING_TO_XSTRING'
    EXPORTING
      TEXT           =  LC_MAIL_ATTACH
     MIMETYPE       = MIMETYPE
*                     ENCODING       =
   IMPORTING
     BUFFER         =  LC_MAIL_XATTACH
                   EXCEPTIONS
                    FAILED         = 1
                     OTHERS         = 2 .

  IF SY-SUBRC = 0.
    CONCATENATE CL_ABAP_CHAR_UTILITIES=>BYTE_ORDER_MARK_LITTLE LC_MAIL_XATTACH INTO LC_MAIL_XATTACH IN BYTE MODE.
  ENDIF.

  CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
     EXPORTING
       BUFFER                = LC_MAIL_XATTACH
*     APPEND_TO_TABLE       = ' '
*   IMPORTING
*     OUTPUT_LENGTH         =
     TABLES
       BINARY_TAB            = LT_CONTENT_HEX         .

  LT_OBJECT_HEADER = 'DG1M物料凭证清单'.
  APPEND LT_OBJECT_HEADER.

  DESCRIBE TABLE LT_CONTENT_HEX LINES SY_VLINE.

  IT_PACKING_LIST-TRANSF_BIN = 'X'.
  IT_PACKING_LIST-HEAD_START = 1 .
  IT_PACKING_LIST-HEAD_NUM =  0 .
  IT_PACKING_LIST-BODY_START = 1.
  IT_PACKING_LIST-BODY_NUM = SY_VLINE.
  IT_PACKING_LIST-DOC_TYPE = 'XLS'.
  IT_PACKING_LIST-DOC_SIZE = 255 * SY_VLINE.
  IT_PACKING_LIST-OBJ_NAME = 'DG1M物料凭证清单'.
  IT_PACKING_LIST-OBJ_DESCR = IT_PACKING_LIST-OBJ_NAME.
  APPEND IT_PACKING_LIST.


  CALL FUNCTION 'SO_NEW_DOCUMENT_ATT_SEND_API1'
    EXPORTING
      DOCUMENT_DATA                    = IT_DOCUMENT_DATA
     PUT_IN_OUTBOX                    = 'X'
     COMMIT_WORK                      = 'X'
   IMPORTING
     SENT_TO_ALL                      = LV_SEND_ALL
*       NEW_OBJECT_ID                    =
    TABLES
      PACKING_LIST                     = IT_PACKING_LIST[]
     OBJECT_HEADER                    = LT_OBJECT_HEADER[]
*       CONTENTS_BIN                     =
     CONTENTS_TXT                     = IT_CONTENT_TEXT[]
     CONTENTS_HEX                     = LT_CONTENT_HEX[]
*       OBJECT_PARA                      =
*       OBJECT_PARB                      =
      RECEIVERS                        = IT_RECEIVERS[]
   EXCEPTIONS
*       TOO_MANY_RECEIVERS               = 1
     DOCUMENT_NOT_SENT                = 1
*       DOCUMENT_TYPE_NOT_EXIST          = 3
     OPERATION_NO_AUTHORIZATION       = 2
     PARAMETER_ERROR                  = 3
     X_ERROR                          = 4
     ENQUEUE_ERROR                    = 5
     OTHERS                           = 6 .
ENDFORM.                    "SEND_EMAIL

你可能感兴趣的:(ABAP)