Custom Function module to send Email with attachment
FUNCTION ZSEND_MAIL_OLIMMS.
*"----------------------------------------------------------------------
*"*"Local Interface:
*" TABLES
*" LT_MAIL STRUCTURE ZFI_MINEII_MAIL
*"----------------------------------------------------------------------
FIELD-SYMBOLS: <FIELD> TYPE ANY.
DATA :
BEGIN OF I_DLOAD OCCURS 0 ,
DLOAD(1000) ,
END OF I_DLOAD ,
IT_RECEIVERS LIKE SOMLRECI1 OCCURS 1 WITH HEADER LINE,
W_OBJECT_CONTENT LIKE SOLISTI1 OCCURS 1 WITH HEADER LINE,
W_DOC_DATA LIKE SODOCCHGI1 OCCURS 0 WITH HEADER LINE,
W_RSNUM(10),
W_BDMNG(14),
W_MEINS(4),
*---for e_mail address
WS_EMAIL1 LIKE ADR6-SMTP_ADDR,
WS_EMAIL2 LIKE ADR6-SMTP_ADDR,
WS_EMAIL3 LIKE ADR6-SMTP_ADDR,
WS_EMAIL4 LIKE ADR6-SMTP_ADDR,
WS_EMAIL5 LIKE ADR6-SMTP_ADDR,
*--Internal table declaration
IT_OBJPACK LIKE SOPCKLSTI1 OCCURS 2 WITH HEADER LINE,
IT_OBJHEAD LIKE SOLISTI1 OCCURS 1 WITH HEADER LINE,
IT_OBJBIN LIKE SOLISTI1 OCCURS 10 WITH HEADER LINE,
IT_OBJTXT LIKE SOLISTI1 OCCURS 10 WITH HEADER LINE,
IT_RECLIST LIKE SOMLRECI1 OCCURS 5 WITH HEADER LINE,
DOC_CHNG LIKE SODOCCHGI1,
TAB_LINES LIKE SY-TABIX,
W_LONGTEXT(255) TYPE C,
V_DATE(10) TYPE C,
TEXT(200) TYPE C,
W_TEXT(50) TYPE C,
*---declaration for diaplaying msg in the body of mail.
W_MSGA(18) TYPE C,
W_MSGB(22) TYPE C,
W_MSGC(27) TYPE C.
data :
lv_string type string,
lv_date type string,
lv_fname(27) type c.
*--Get receipient address
CLEAR IT_RECLIST.
*---passing e_mail address
** WS_EMAIL1 = 'vipin.saraika@wipro.com'. "E-mail
** WS_EMAIL2 = 'dilli.teegala@wipro.com'. "E-mail
** WS_EMAIL3 = 'raviteja.bodi@wipro.com'. "E-mail
** WS_EMAIL4 = 'vijayasai.basavaraju@wipro.com'. "E-mail
** WS_EMAIL5 = 'govinda.pothuraju@wipro.com'.
WS_EMAIL1 = 'raviteja.bodi@wipro.com'. "E-mail
WS_EMAIL2 = 'kasifshameem@wipro.com'. "E-mail "E-mail
concatenate sy-datum+6(2)'.' sy-datum+4(2) '.'sy-datum+0(4) into lv_date.
concatenate 'Olimms' lv_date into lv_string SEPARATED BY space.
lv_fname = lv_string.
*---setting name of file for csv
W_FILENAME = lv_fname. "filenmae of attachment
* IF p_email IS NOT INITIAL.
* build body of message
CONCATENATE 'Please find the attachment to check the OLIMMS Interface status' ' ' INTO IT_OBJTXT "main Body of mail
SEPARATED BY SPACE.
APPEND IT_OBJTXT.
CLEAR IT_OBJTXT.
IT_RECLIST-RECEIVER = WS_EMAIL1.
IT_RECLIST-REC_TYPE = 'U'.
IT_RECLIST-EXPRESS = ' '.
IT_RECLIST-COM_TYPE = 'INT'.
APPEND IT_RECLIST.
IT_RECLIST-RECEIVER = WS_EMAIL2.
IT_RECLIST-REC_TYPE = 'U'.
IT_RECLIST-EXPRESS = ' '.
IT_RECLIST-COM_TYPE = 'INT'.
APPEND IT_RECLIST.
IT_RECLIST-RECEIVER = WS_EMAIL3.
IT_RECLIST-REC_TYPE = 'U'.
IT_RECLIST-EXPRESS = ' '.
IT_RECLIST-COM_TYPE = 'INT'.
APPEND IT_RECLIST.
IT_RECLIST-RECEIVER = WS_EMAIL4.
IT_RECLIST-REC_TYPE = 'U'.
IT_RECLIST-EXPRESS = ' '.
IT_RECLIST-COM_TYPE = 'INT'.
APPEND IT_RECLIST.
IT_RECLIST-RECEIVER = WS_EMAIL5.
IT_RECLIST-REC_TYPE = 'U'.
IT_RECLIST-EXPRESS = ' '.
IT_RECLIST-COM_TYPE = 'INT'.
APPEND IT_RECLIST.
*--Set title of object and email
CLEAR: W_TEXT.
DOC_CHNG-OBJ_DESCR = 'Olimms Interface'. "Subject of mail
DOC_CHNG-OBJ_NAME = 'INBOUND'.
DESCRIBE TABLE IT_OBJTXT LINES TAB_LINES.
READ TABLE IT_OBJTXT INDEX TAB_LINES.
DOC_CHNG-DOC_SIZE = ( TAB_LINES - 1 ) * 255 + STRLEN( IT_OBJTXT ).
*--Move the Report Name as the Heading line for email file
CLEAR: W_LONGTEXT.
W_LONGTEXT = 'Olimms Interface Status Report'. "heading inside the attachemnt file
IT_OBJBIN = W_LONGTEXT.
APPEND IT_OBJBIN. CLEAR IT_OBJBIN.
*--Append Blank Lines
APPEND IT_OBJBIN. CLEAR IT_OBJBIN.
APPEND IT_OBJBIN. CLEAR IT_OBJBIN.
**--User name
CLEAR TEXT.
MOVE: 'User:' TO TEXT, "user inside attachment
SY-UNAME TO TEXT+6.
IT_OBJBIN = TEXT.
APPEND IT_OBJBIN. CLEAR IT_OBJBIN.
* ---appending heading to Internal table of Email
CLEAR: W_L_M_STRING.
W_L_M_STRING = 'Messages'. "sub-heading inside attachment
* CONCATENATE w_l_m_string
* INTO w_longtext SEPARATED BY ','.
IT_OBJBIN = W_L_M_STRING.
APPEND IT_OBJBIN. CLEAR IT_OBJBIN.
LOOP AT LT_MAIL.
DO.
ASSIGN COMPONENT SY-INDEX OF STRUCTURE LT_MAIL TO <FIELD>.
IF SY-SUBRC <> 0.
EXIT.
ENDIF.
* Look for Commas in the field value. If it exists, put quotes around
* value so that the file opens correctly with all the columns aligned
* in Excel.
SEARCH <FIELD> FOR ',' IN CHARACTER MODE.
* If search for commas are successful.
IF SY-SUBRC = 0.
CONCATENATE '"' <FIELD> '"' INTO <FIELD> IN CHARACTER MODE.
ENDIF.
IF SY-INDEX = 1.
I_DLOAD-DLOAD = <FIELD>.
SHIFT: I_DLOAD-DLOAD LEFT DELETING LEADING SPACE.
ELSE.
* Put Comma as a separator for values in IT_DLOAD internal table
SHIFT: I_DLOAD-DLOAD LEFT DELETING LEADING SPACE.
CONCATENATE I_DLOAD-DLOAD <FIELD> INTO I_DLOAD-DLOAD
SEPARATED BY ',' IN CHARACTER MODE.
ENDIF.
ENDDO.
IT_OBJBIN = I_DLOAD-DLOAD.
APPEND IT_OBJBIN. CLEAR IT_OBJBIN.
ENDLOOP.
* ENDIF.
* ENDIF.
*Convert to correct format
CALL FUNCTION 'SO_RAW_TO_RTF'
TABLES
OBJCONT_OLD = IT_OBJBIN
OBJCONT_NEW = IT_OBJBIN.
* create the control table entry for the main email
DESCRIBE TABLE IT_OBJTXT LINES TAB_LINES.
CLEAR IT_OBJPACK-TRANSF_BIN.
IT_OBJPACK-HEAD_START = 1.
IT_OBJPACK-HEAD_NUM = 0.
IT_OBJPACK-BODY_START = 1.
IT_OBJPACK-BODY_NUM = TAB_LINES.
IT_OBJPACK-DOC_TYPE = 'RAW'.
APPEND IT_OBJPACK.
DESCRIBE TABLE IT_OBJBIN LINES TAB_LINES.
IT_OBJPACK-HEAD_START = 1.
IT_OBJPACK-HEAD_NUM = 0.
IT_OBJPACK-BODY_START = 1.
IT_OBJPACK-BODY_NUM = TAB_LINES.
IT_OBJPACK-TRANSF_BIN = 'X'."'C'.
IT_OBJPACK-DOC_TYPE = 'CSV'.
IT_OBJPACK-OBJ_DESCR = W_FILENAME.
IT_OBJPACK-OBJ_NAME = 'BILLING'.
IT_OBJPACK-DOC_SIZE = TAB_LINES * 255.
APPEND IT_OBJPACK.
CALL FUNCTION 'SO_NEW_DOCUMENT_ATT_SEND_API1'
EXPORTING
DOCUMENT_DATA = DOC_CHNG
* PUT_IN_OUTBOX = c_x
COMMIT_WORK = 'X'
TABLES
PACKING_LIST = IT_OBJPACK
* object_header = it_objhead
CONTENTS_BIN = IT_OBJBIN
CONTENTS_TXT = IT_OBJTXT
RECEIVERS = IT_RECLIST
EXCEPTIONS
TOO_MANY_RECEIVERS = 1
DOCUMENT_NOT_SENT = 2
OPERATION_NO_AUTHORIZATION = 4
OTHERS = 99.
IF SY-SUBRC <> 0.
WRITE: / 'failure in sending mail'.
ELSE.
WRITE: / 'Success in sending mail'.
ENDIF.
ENDFUNCTION.
*"----------------------------------------------------------------------
*"*"Local Interface:
*" TABLES
*" LT_MAIL STRUCTURE ZFI_MINEII_MAIL
*"----------------------------------------------------------------------
FIELD-SYMBOLS: <FIELD> TYPE ANY.
DATA :
BEGIN OF I_DLOAD OCCURS 0 ,
DLOAD(1000) ,
END OF I_DLOAD ,
IT_RECEIVERS LIKE SOMLRECI1 OCCURS 1 WITH HEADER LINE,
W_OBJECT_CONTENT LIKE SOLISTI1 OCCURS 1 WITH HEADER LINE,
W_DOC_DATA LIKE SODOCCHGI1 OCCURS 0 WITH HEADER LINE,
W_RSNUM(10),
W_BDMNG(14),
W_MEINS(4),
*---for e_mail address
WS_EMAIL1 LIKE ADR6-SMTP_ADDR,
WS_EMAIL2 LIKE ADR6-SMTP_ADDR,
WS_EMAIL3 LIKE ADR6-SMTP_ADDR,
WS_EMAIL4 LIKE ADR6-SMTP_ADDR,
WS_EMAIL5 LIKE ADR6-SMTP_ADDR,
*--Internal table declaration
IT_OBJPACK LIKE SOPCKLSTI1 OCCURS 2 WITH HEADER LINE,
IT_OBJHEAD LIKE SOLISTI1 OCCURS 1 WITH HEADER LINE,
IT_OBJBIN LIKE SOLISTI1 OCCURS 10 WITH HEADER LINE,
IT_OBJTXT LIKE SOLISTI1 OCCURS 10 WITH HEADER LINE,
IT_RECLIST LIKE SOMLRECI1 OCCURS 5 WITH HEADER LINE,
DOC_CHNG LIKE SODOCCHGI1,
TAB_LINES LIKE SY-TABIX,
W_LONGTEXT(255) TYPE C,
V_DATE(10) TYPE C,
TEXT(200) TYPE C,
W_TEXT(50) TYPE C,
*---declaration for diaplaying msg in the body of mail.
W_MSGA(18) TYPE C,
W_MSGB(22) TYPE C,
W_MSGC(27) TYPE C.
data :
lv_string type string,
lv_date type string,
lv_fname(27) type c.
*--Get receipient address
CLEAR IT_RECLIST.
*---passing e_mail address
** WS_EMAIL1 = 'vipin.saraika@wipro.com'. "E-mail
** WS_EMAIL2 = 'dilli.teegala@wipro.com'. "E-mail
** WS_EMAIL3 = 'raviteja.bodi@wipro.com'. "E-mail
** WS_EMAIL4 = 'vijayasai.basavaraju@wipro.com'. "E-mail
** WS_EMAIL5 = 'govinda.pothuraju@wipro.com'.
WS_EMAIL1 = 'raviteja.bodi@wipro.com'. "E-mail
WS_EMAIL2 = 'kasifshameem@wipro.com'. "E-mail "E-mail
concatenate sy-datum+6(2)'.' sy-datum+4(2) '.'sy-datum+0(4) into lv_date.
concatenate 'Olimms' lv_date into lv_string SEPARATED BY space.
lv_fname = lv_string.
*---setting name of file for csv
W_FILENAME = lv_fname. "filenmae of attachment
* IF p_email IS NOT INITIAL.
* build body of message
CONCATENATE 'Please find the attachment to check the OLIMMS Interface status' ' ' INTO IT_OBJTXT "main Body of mail
SEPARATED BY SPACE.
APPEND IT_OBJTXT.
CLEAR IT_OBJTXT.
IT_RECLIST-RECEIVER = WS_EMAIL1.
IT_RECLIST-REC_TYPE = 'U'.
IT_RECLIST-EXPRESS = ' '.
IT_RECLIST-COM_TYPE = 'INT'.
APPEND IT_RECLIST.
IT_RECLIST-RECEIVER = WS_EMAIL2.
IT_RECLIST-REC_TYPE = 'U'.
IT_RECLIST-EXPRESS = ' '.
IT_RECLIST-COM_TYPE = 'INT'.
APPEND IT_RECLIST.
IT_RECLIST-RECEIVER = WS_EMAIL3.
IT_RECLIST-REC_TYPE = 'U'.
IT_RECLIST-EXPRESS = ' '.
IT_RECLIST-COM_TYPE = 'INT'.
APPEND IT_RECLIST.
IT_RECLIST-RECEIVER = WS_EMAIL4.
IT_RECLIST-REC_TYPE = 'U'.
IT_RECLIST-EXPRESS = ' '.
IT_RECLIST-COM_TYPE = 'INT'.
APPEND IT_RECLIST.
IT_RECLIST-RECEIVER = WS_EMAIL5.
IT_RECLIST-REC_TYPE = 'U'.
IT_RECLIST-EXPRESS = ' '.
IT_RECLIST-COM_TYPE = 'INT'.
APPEND IT_RECLIST.
*--Set title of object and email
CLEAR: W_TEXT.
DOC_CHNG-OBJ_DESCR = 'Olimms Interface'. "Subject of mail
DOC_CHNG-OBJ_NAME = 'INBOUND'.
DESCRIBE TABLE IT_OBJTXT LINES TAB_LINES.
READ TABLE IT_OBJTXT INDEX TAB_LINES.
DOC_CHNG-DOC_SIZE = ( TAB_LINES - 1 ) * 255 + STRLEN( IT_OBJTXT ).
*--Move the Report Name as the Heading line for email file
CLEAR: W_LONGTEXT.
W_LONGTEXT = 'Olimms Interface Status Report'. "heading inside the attachemnt file
IT_OBJBIN = W_LONGTEXT.
APPEND IT_OBJBIN. CLEAR IT_OBJBIN.
*--Append Blank Lines
APPEND IT_OBJBIN. CLEAR IT_OBJBIN.
APPEND IT_OBJBIN. CLEAR IT_OBJBIN.
**--User name
CLEAR TEXT.
MOVE: 'User:' TO TEXT, "user inside attachment
SY-UNAME TO TEXT+6.
IT_OBJBIN = TEXT.
APPEND IT_OBJBIN. CLEAR IT_OBJBIN.
* ---appending heading to Internal table of Email
CLEAR: W_L_M_STRING.
W_L_M_STRING = 'Messages'. "sub-heading inside attachment
* CONCATENATE w_l_m_string
* INTO w_longtext SEPARATED BY ','.
IT_OBJBIN = W_L_M_STRING.
APPEND IT_OBJBIN. CLEAR IT_OBJBIN.
LOOP AT LT_MAIL.
DO.
ASSIGN COMPONENT SY-INDEX OF STRUCTURE LT_MAIL TO <FIELD>.
IF SY-SUBRC <> 0.
EXIT.
ENDIF.
* Look for Commas in the field value. If it exists, put quotes around
* value so that the file opens correctly with all the columns aligned
* in Excel.
SEARCH <FIELD> FOR ',' IN CHARACTER MODE.
* If search for commas are successful.
IF SY-SUBRC = 0.
CONCATENATE '"' <FIELD> '"' INTO <FIELD> IN CHARACTER MODE.
ENDIF.
IF SY-INDEX = 1.
I_DLOAD-DLOAD = <FIELD>.
SHIFT: I_DLOAD-DLOAD LEFT DELETING LEADING SPACE.
ELSE.
* Put Comma as a separator for values in IT_DLOAD internal table
SHIFT: I_DLOAD-DLOAD LEFT DELETING LEADING SPACE.
CONCATENATE I_DLOAD-DLOAD <FIELD> INTO I_DLOAD-DLOAD
SEPARATED BY ',' IN CHARACTER MODE.
ENDIF.
ENDDO.
IT_OBJBIN = I_DLOAD-DLOAD.
APPEND IT_OBJBIN. CLEAR IT_OBJBIN.
ENDLOOP.
* ENDIF.
* ENDIF.
*Convert to correct format
CALL FUNCTION 'SO_RAW_TO_RTF'
TABLES
OBJCONT_OLD = IT_OBJBIN
OBJCONT_NEW = IT_OBJBIN.
* create the control table entry for the main email
DESCRIBE TABLE IT_OBJTXT LINES TAB_LINES.
CLEAR IT_OBJPACK-TRANSF_BIN.
IT_OBJPACK-HEAD_START = 1.
IT_OBJPACK-HEAD_NUM = 0.
IT_OBJPACK-BODY_START = 1.
IT_OBJPACK-BODY_NUM = TAB_LINES.
IT_OBJPACK-DOC_TYPE = 'RAW'.
APPEND IT_OBJPACK.
DESCRIBE TABLE IT_OBJBIN LINES TAB_LINES.
IT_OBJPACK-HEAD_START = 1.
IT_OBJPACK-HEAD_NUM = 0.
IT_OBJPACK-BODY_START = 1.
IT_OBJPACK-BODY_NUM = TAB_LINES.
IT_OBJPACK-TRANSF_BIN = 'X'."'C'.
IT_OBJPACK-DOC_TYPE = 'CSV'.
IT_OBJPACK-OBJ_DESCR = W_FILENAME.
IT_OBJPACK-OBJ_NAME = 'BILLING'.
IT_OBJPACK-DOC_SIZE = TAB_LINES * 255.
APPEND IT_OBJPACK.
CALL FUNCTION 'SO_NEW_DOCUMENT_ATT_SEND_API1'
EXPORTING
DOCUMENT_DATA = DOC_CHNG
* PUT_IN_OUTBOX = c_x
COMMIT_WORK = 'X'
TABLES
PACKING_LIST = IT_OBJPACK
* object_header = it_objhead
CONTENTS_BIN = IT_OBJBIN
CONTENTS_TXT = IT_OBJTXT
RECEIVERS = IT_RECLIST
EXCEPTIONS
TOO_MANY_RECEIVERS = 1
DOCUMENT_NOT_SENT = 2
OPERATION_NO_AUTHORIZATION = 4
OTHERS = 99.
IF SY-SUBRC <> 0.
WRITE: / 'failure in sending mail'.
ELSE.
WRITE: / 'Success in sending mail'.
ENDIF.
ENDFUNCTION.
No comments:
Post a Comment