ABAP: Bir tablonun html biçiminde mail olarak gönderilmesi.

Send internal table in html format to mail.

*---------------------------------------------------------------------*
* V A R I A B L E S *
*---------------------------------------------------------------------*
DATA:
t_header TYPE STANDARD TABLE OF w3head WITH HEADER LINE, "Header
t_fields TYPE STANDARD TABLE OF w3fields WITH HEADER LINE, "Fields
t_html TYPE STANDARD TABLE OF w3html, "Html
wa_header TYPE w3head,
w_head TYPE w3head.

DATA:BEGIN OF it_data OCCURS 0,
UPDKZ LIKE ZPP_T_PRODORD_L-UPDKZ, " Icron güncelleme göst.
AUFNR LIKE ZPP_T_PRODORD_L-AUFNR, " Sipariş
MATNR LIKE ZPP_T_PRODORD_L-MATNR, " Malzeme
MENGE LIKE ZPP_T_PRODORD_L-MENGE, " Miktar
MEINS LIKE ZPP_T_PRODORD_L-MEINS, " ÖB
BEDEN LIKE ZPP_T_PRODORD_L-BEDEN, " Matris
ZZMAKINA LIKE ZPP_T_PRODORD_L-ZZMAKINA, " Makina Kodu
TYPE LIKE ZPP_T_PRODORD_L-TYPE, " Mesaj Tipi
MESSAGE LIKE ZPP_T_PRODORD_L-MESSAGE, " Mesaj
CRNAM LIKE ZPP_T_PRODORD_L-CRNAM, " Yaratan
CRDAT LIKE ZPP_T_PRODORD_L-CRDAT, " Y.Tarihi
CRTIM LIKE ZPP_T_PRODORD_L-CRTIM, " Y.Saati
END OF it_data,
wa_data like LINE OF it_data.

* Data Declarations
DATA: LT_MAILSUBJECT TYPE SODOCCHGI1.
DATA: LT_MAILRECIPIENTS TYPE STANDARD TABLE OF SOMLREC90 WITH HEADER
LINE.
DATA: LT_MAILTXT TYPE STANDARD TABLE OF SOLI WITH HEADER
LINE.

DATA: BEGIN OF t_mail OCCURS 0,
receiver TYPE ZPP_T_MAILGRUP-SMTP_ADDR,
END OF t_mail,
ls_mail LIKE LINE OF t_mail.
*---------------------------------------------------------------------*
* S E L E C T I O N S C R E E N *
*---------------------------------------------------------------------*
*SELECTION-SCREEN BEGIN OF BLOCK BL1 WITH FRAME TITLE TEXT-000.
*PARAMETERS : P_WERKS LIKE T001W-WERKS OBLIGATORY.
*SELECT-OPTIONS : S_MATNR FOR MARA-MATNR.
*SELECTION-SCREEN END OF BLOCK BL1 .

*---------------------------------------------------------------------*
* E V E N T S *
*---------------------------------------------------------------------*
*---------------------------------------------------------------------*
INITIALIZATION.
*---------------------------------------------------------------------*
*---------------------------------------------------------------------*
AT SELECTION-SCREEN OUTPUT.
*---------------------------------------------------------------------*
*---------------------------------------------------------------------*
AT SELECTION-SCREEN.
*---------------------------------------------------------------------*
* Validate Input At Selection Screen
*PERFORM screen_validation.

*---------------------------------------------------------------------*
START-OF-SELECTION.
*---------------------------------------------------------------------*
PERFORM build_data.
PERFORM send_mail.
*---------------------------------------------------------------------*
END-OF-SELECTION.
*&---------------------------------------------------------------------*
*& Form BUILD_DATA
*&---------------------------------------------------------------------*

FORM BUILD_DATA .
DATA: LV_DATE1 TYPE SY-DATUM.
DATA: LV_DATE2 TYPE SY-DATUM.

LV_DATE1 = SY-DATUM .
LV_DATE2 = SY-DATUM - 1.

* başarılı olmayan üretim siparişi işlemleri
SELECT log~UPDKZ
log~AUFNR
log~MATNR
log~MENGE
log~MEINS
log~BEDEN
log~ZZMAKINA
log~TYPE
log~MESSAGE
log~CRNAM
log~CRDAT
log~CRTIM
FROM ZPP_T_PRODORD_L as log
INTO TABLE it_data
where TYPE ne 'S'
AND ( ( log~CRDAT EQ LV_DATE1 AND log~CRTIM LE SY-UZEIT )
OR ( log~CRDAT eq LV_DATE2 AND log~CRTIM GE SY-UZEIT ) ).

* mak no boş olan siparişler
SELECT log~UPDKZ
log~AUFNR
log~MATNR
log~MENGE
log~MEINS
log~BEDEN
log~ZZMAKINA
log~TYPE
log~MESSAGE
log~CRNAM
log~CRDAT
log~CRTIM FROM ZPP_T_PRODORD_L as log
INNER JOIN aufk
on log~aufnr eq aufk~aufnr
APPENDING TABLE it_data
where ( ( log~CRDAT eq LV_DATE1 AND log~CRTIM LE SY-UZEIT )
OR ( log~CRDAT eq LV_DATE2 AND log~CRTIM ge SY-UZEIT ) )
and aufk~LOEKZ eq ''
and aufk~ZZMAKINA eq ''.

LOOP AT it_data INTO wa_data.
IF wa_data-TYPE eq 'S'.
wa_data-TYPE = 'E'.
wa_data-MESSAGE = 'SAP üretim siparişi Makine No boş olamaz!'.
modify it_data from wa_data.
ENDIF.
ENDLOOP.
IF it_data[] is INITIAL.
write: / 'Kayıt bulunamadı!'.
ENDIF.
ENDFORM. " BUILD_DATA
*&---------------------------------------------------------------------*
*& Form SEND_MAIL
*&---------------------------------------------------------------------*
FORM SEND_MAIL .
CHECK it_data[] is not INITIAL.
*-Fill the Column headings and Properties
DATA: GT_FIELDCAT TYPE SLIS_T_FIELDCAT_ALV WITH HEADER LINE.
*Merge işlemi
CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'
EXPORTING
I_PROGRAM_NAME = SY-REPID
I_INTERNAL_TABNAME = 'IT_DATA'
I_INCLNAME = SY-REPID
CHANGING
CT_FIELDCAT = GT_FIELDCAT[]
EXCEPTIONS
INCONSISTENT_INTERFACE = 1
PROGRAM_ERROR = 2
OTHERS = 3.
LOOP AT GT_FIELDCAT.
w_head-text = GT_FIELDCAT-SELTEXT_S.
IF GT_FIELDCAT-FIELDNAME = 'MESSAGE'.
w_head-text = 'Hata Mesajı'.
ENDIF.
*-Populate the Column Headings
CALL FUNCTION 'WWW_ITAB_TO_HTML_HEADERS'
EXPORTING
field_nr = sy-tabix
text = w_head-text
fgcolor = 'black'
bgcolor = 'orange'
TABLES
header = t_header.
*-Populate Column Properties
CALL FUNCTION 'WWW_ITAB_TO_HTML_LAYOUT'
EXPORTING
field_nr = sy-tabix
fgcolor = 'black'
size = '3'
TABLES
fields = t_fields.
ENDLOOP.
*-Title of the Display
wa_header-text = 'SAP Icron RFC hata detayları.' .
wa_header-font = 'Arial'.
wa_header-size = '2'.
*-Preparing the HTML from Intenal Table
REFRESH t_html.
CALL FUNCTION 'WWW_ITAB_TO_HTML'
EXPORTING
table_header = wa_header
TABLES
html = t_html
fields = t_fields
row_header = t_header
itable = it_data.

* Recipients
select SMTP_ADDR as receiver
from ZPP_T_MAILGRUP into TABLE t_mail
where PGMNA eq 'ZPP_R_ICRON_MAIL_HATA'.

IF T_MAIL[] IS INITIAL.
LT_MAILRECIPIENTS-receiver = 'user@domain.com.tr'.
LT_MAILRECIPIENTS-rec_type = 'U'.
APPEND LT_MAILRECIPIENTS .
CLEAR LT_MAILRECIPIENTS .
ELSE.
loop at t_mail INTO ls_mail.
LT_MAILRECIPIENTS-receiver = ls_mail-receiver.
LT_MAILRECIPIENTS-rec_type = 'U'.
APPEND LT_MAILRECIPIENTS .
CLEAR LT_MAILRECIPIENTS .
ENDLOOP.
ENDIF.

* Subject.
LT_MAILSUBJECT-OBJ_NAME = 'PENTI FABRIKA'.
LT_MAILSUBJECT-OBJ_LANGU = SY-LANGU.
LT_MAILSUBJECT-OBJ_DESCR = 'SAP Icron Örgü Üretim Siparişi Hata'.

* Mail Contents
LT_MAILTXT[] = t_html[].

* Send Mail
CALL FUNCTION 'SO_NEW_DOCUMENT_SEND_API1'
EXPORTING
COMMIT_WORK = ''
PUT_IN_OUTBOX = 'X'
DOCUMENT_DATA = LT_MAILSUBJECT
DOCUMENT_TYPE = 'HTM'
TABLES
OBJECT_CONTENT = LT_MAILTXT
RECEIVERS = LT_MAILRECIPIENTS
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 EQ 0.
COMMIT WORK AND WAIT.
SUBMIT rsconn01 WITH mode = 'INT' AND RETURN.
write: / 'Mail gönderildi...'.
else.
write: / 'hata:',sy-subrc.
ENDIF.

ENDFORM. " SEND_MAIL

Bir cevap yazın

E-posta hesabınız yayımlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir

This site uses Akismet to reduce spam. Learn how your comment data is processed.