SAP: ABAP How to send email that can have underline bold text, table and pictures using html via class CL_BCS
8:26 PM
snippet..
FORM SEND_EMAIL_OFFLN TABLES PT_PDF_BIN
USING PV_SENDER
PV_RECIPIENT
PV_SUBJECT.
USING PV_SENDER
PV_RECIPIENT
PV_SUBJECT.
CONSTANTS: LC_TYPE TYPE SO_OBJ_TP VALUE 'HTM'.
DATA: LO_MESSENGER TYPE REF TO CL_BCS,
LO_DOCUMENT TYPE REF TO CL_DOCUMENT_BCS,
LO_SENDER TYPE REF TO CL_SAPUSER_BCS,
LO_RECIPIENT TYPE REF TO IF_RECIPIENT_BCS,
LO_DELIVERY TYPE OS_BOOLEAN,
LO_ERR_MSG TYPE SY-MSGV1.
LO_DOCUMENT TYPE REF TO CL_DOCUMENT_BCS,
LO_SENDER TYPE REF TO CL_SAPUSER_BCS,
LO_RECIPIENT TYPE REF TO IF_RECIPIENT_BCS,
LO_DELIVERY TYPE OS_BOOLEAN,
LO_ERR_MSG TYPE SY-MSGV1.
DATA: LT_SOLIX TYPE SOLIX_TAB,
LS_SOLIX LIKE LINE OF LT_SOLIX.
DATA: LV_ATTCH_SUBJECT TYPE SOOD-OBJDES.
"LOOP AT PT_PDF_BIN.
" CLEAR LS_SOLIX.
" LS_SOLIX-LINE = PT_PDF_BIN.
" APPEND LS_SOLIX TO LT_SOLIX.
"ENDLOOP.
LS_SOLIX LIKE LINE OF LT_SOLIX.
DATA: LV_ATTCH_SUBJECT TYPE SOOD-OBJDES.
"LOOP AT PT_PDF_BIN.
" CLEAR LS_SOLIX.
" LS_SOLIX-LINE = PT_PDF_BIN.
" APPEND LS_SOLIX TO LT_SOLIX.
"ENDLOOP.
"THIS IS THE PART WHICH YOU CAN DO HTML CODINGS ß----------------------------------------------------------------PART TO
PLAY AROUND
CLEAR LS_MESSAGE. LS_MESSAGE = '<html>'. APPEND LS_MESSAGE TO LT_DOCUMENT.
CLEAR LS_MESSAGE. LS_MESSAGE = '<body>'. APPEND LS_MESSAGE TO LT_DOCUMENT.
CLEAR LS_MESSAGE. LS_MESSAGE = '<body>'. APPEND LS_MESSAGE TO LT_DOCUMENT.
CLEAR LS_MESSAGE. LS_MESSAGE = 'Dear Sir/Madam,<br>'. APPEND LS_MESSAGE TO LT_DOCUMENT.
CLEAR LS_MESSAGE. LS_MESSAGE = '<u><b>Underline and bold</b></u> <br><br>'. APPEND LS_MESSAGE TO LT_DOCUMENT. " underline and bold
CLEAR LS_MESSAGE. LS_MESSAGE = '<u><b>Underline and bold</b></u> <br><br>'. APPEND LS_MESSAGE TO LT_DOCUMENT. " underline and bold
CLEAR LS_MESSAGE. LS_MESSAGE = '</body>'. APPEND LS_MESSAGE TO LT_DOCUMENT.
CLEAR LS_MESSAGE. LS_MESSAGE = '</html>'. APPEND LS_MESSAGE TO LT_DOCUMENT.
CLEAR LS_MESSAGE. LS_MESSAGE = '</html>'. APPEND LS_MESSAGE TO LT_DOCUMENT.
"-----PDF RSU-----"
"LV_ATTCH_SUBJECT = 'Notification of Vesting.pdf'.
"LV_ATTCH_SUBJECT = 'Notification of Vesting.pdf'.
TRY .
"-------- create persistent send request ------------------------
LO_MESSENGER = CL_BCS=>CREATE_PERSISTENT( ).
“ Create Document Object
LO_DOCUMENT = CL_DOCUMENT_BCS=>CREATE_DOCUMENT(
I_TYPE = LC_TYPE
I_SUBJECT = PV_SUBJECT
I_TEXT = LT_DOCUMENT ).
" Add attachment
"CALL METHOD LO_DOCUMENT->ADD_ATTACHMENT
" EXPORTING
" I_ATTACHMENT_TYPE = 'BIN'
" I_ATTACHMENT_SUBJECT = LV_ATTCH_SUBJECT
" I_ATT_CONTENT_HEX = LT_SOLIX.
"****** Create Sender
" note: this is necessary only if you want to set the sender
" different from actual user (SY-UNAME). Otherwise sender is
“ set automatically with actual user.
LO_SENDER = CL_SAPUSER_BCS=>CREATE( PV_SENDER ).
"Create Recipient
LO_RECIPIENT = CL_CAM_ADDRESS_BCS=>CREATE_INTERNET_ADDRESS( PV_RECIPIENT ).
"Setup BCS Object
LO_MESSENGER->SET_DOCUMENT( LO_DOCUMENT ).
LO_MESSENGER->SET_SENDER( LO_SENDER ).
LO_MESSENGER->ADD_RECIPIENT( LO_RECIPIENT ).
"Send the Mail via BCS Object
LO_DELIVERY = LO_MESSENGER->SEND( I_WITH_ERROR_SCREEN = 'X' ).
IF NOT LO_DELIVERY IS INITIAL.
LV_MSG = 'SUCCESS: Email successfully send.'.
COMMIT WORK.
ELSE.
LO_ERR_MSG = 'ERROR: Failed to send email.'.
ENDIF.
CATCH CX_DOCUMENT_BCS. " cl_document_bcs=>create_document()
"LV_MSG = 'ERROR: Failed to create BCS document.'.
CATCH CX_ADDRESS_BCS. " cl_sapuser_bcs=>create()
"LV_MSG = 'ERROR: Failed to create BCS sender'.
CATCH CX_SEND_REQ_BCS. " cl_bcs=>create_persistent(), cl_bcs->set_sender(), etc.
"LV_MSG = 'ERROR: Failed while setting up BCS object.'.
ENDTRY.
ENDFORM. " SEND_EMAIL_OFFLN
"-------- create persistent send request ------------------------
LO_MESSENGER = CL_BCS=>CREATE_PERSISTENT( ).
“ Create Document Object
LO_DOCUMENT = CL_DOCUMENT_BCS=>CREATE_DOCUMENT(
I_TYPE = LC_TYPE
I_SUBJECT = PV_SUBJECT
I_TEXT = LT_DOCUMENT ).
" Add attachment
"CALL METHOD LO_DOCUMENT->ADD_ATTACHMENT
" EXPORTING
" I_ATTACHMENT_TYPE = 'BIN'
" I_ATTACHMENT_SUBJECT = LV_ATTCH_SUBJECT
" I_ATT_CONTENT_HEX = LT_SOLIX.
"****** Create Sender
" note: this is necessary only if you want to set the sender
" different from actual user (SY-UNAME). Otherwise sender is
“ set automatically with actual user.
LO_SENDER = CL_SAPUSER_BCS=>CREATE( PV_SENDER ).
"Create Recipient
LO_RECIPIENT = CL_CAM_ADDRESS_BCS=>CREATE_INTERNET_ADDRESS( PV_RECIPIENT ).
"Setup BCS Object
LO_MESSENGER->SET_DOCUMENT( LO_DOCUMENT ).
LO_MESSENGER->SET_SENDER( LO_SENDER ).
LO_MESSENGER->ADD_RECIPIENT( LO_RECIPIENT ).
"Send the Mail via BCS Object
LO_DELIVERY = LO_MESSENGER->SEND( I_WITH_ERROR_SCREEN = 'X' ).
IF NOT LO_DELIVERY IS INITIAL.
LV_MSG = 'SUCCESS: Email successfully send.'.
COMMIT WORK.
ELSE.
LO_ERR_MSG = 'ERROR: Failed to send email.'.
ENDIF.
CATCH CX_DOCUMENT_BCS. " cl_document_bcs=>create_document()
"LV_MSG = 'ERROR: Failed to create BCS document.'.
CATCH CX_ADDRESS_BCS. " cl_sapuser_bcs=>create()
"LV_MSG = 'ERROR: Failed to create BCS sender'.
CATCH CX_SEND_REQ_BCS. " cl_bcs=>create_persistent(), cl_bcs->set_sender(), etc.
"LV_MSG = 'ERROR: Failed while setting up BCS object.'.
ENDTRY.
ENDFORM. " SEND_EMAIL_OFFLN
"---------------------
just incase you want to add pdf
FORM DOWNLOAD_PDF_n_send.
DATA: LS_OUTPUTPARAMS TYPE SFPOUTPUTPARAMS.
DATA: LS_FORMOUTPUT TYPE FPFORMOUTPUT.
DATA: LV_FM_NAME TYPE RS38L_FNAM.
DATA: LS_DOCPARAMS TYPE SFPDOCPARAMS.
DATA: LS_ZESOS_RSUGL_PDF TYPE ZESOS_RSUGL_PDF.
DATA: LV_MSG TYPE STRING.
CLEAR LS_ZESOS_RSUGL_PDF.
LS_ZESOS_RSUGL_PDF-PERNR = PERNR-PERNR.
LS_ZESOS_RSUGL_PDF-BEGDA = PN-BEGDA.
LS_ZESOS_RSUGL_PDF-ENDDA = PN-ENDDA.
LS_ZESOS_RSUGL_PDF-CPLAN = P_CPLAN.
LS_ZESOS_RSUGL_PDF-CITEM = P_CITEM.
"Get name of the generated function module
CALL FUNCTION 'FP_FUNCTION_MODULE_NAME'
EXPORTING
I_NAME = 'Z_PDF_FROM_TCODE_SFP'
IMPORTING
E_FUNCNAME = LV_FM_NAME.
"Set the output parameters.
LS_OUTPUTPARAMS-NODIALOG = 'X'.
LS_OUTPUTPARAMS-PREVIEW = ''.
LS_OUTPUTPARAMS-GETPDF = 'X'.
"Open print job
CALL FUNCTION 'FP_JOB_OPEN'
CHANGING
IE_OUTPUTPARAMS = LS_OUTPUTPARAMS
EXCEPTIONS
OTHERS = 1.
"Set the PDF to be dynamic and interactive.
LS_DOCPARAMS-FILLABLE = ''.
LS_DOCPARAMS-DYNAMIC = 'X'.
"Generate the PDF.
CLEAR LS_FORMOUTPUT.
CALL FUNCTION LV_FM_NAME
EXPORTING
/1BCDWB/DOCPARAMS = LS_DOCPARAMS
ZESOS_RSUGL_PDF = LS_ZESOS_RSUGL_PDF
IMPORTING
/1BCDWB/FORMOUTPUT = LS_FORMOUTPUT
EXCEPTIONS
USAGE_ERROR = 1
SYSTEM_ERROR = 2
INTERNAL_ERROR = 3
OTHERS = 4.
IF SY-SUBRC NE 0.
LV_MSG = 'Failed to generate PDF.'.
PERFORM FILL_MSG USING LV_MSG
CHANGING GS_OUTREP.
MODIFY GT_OUTREP FROM GS_OUTREP INDEX GV_TABIX.
ENDIF.
CHECK SY-SUBRC = 0.
"Close print job.
CALL FUNCTION 'FP_JOB_CLOSE'.
*--------------------------------------------------------------------*
DATA: LT_PDF_BIN TYPE TABLE OF X255.
DATA: LV_PDF_NAME TYPE STRING.
CONCATENATE P_FOLDER '\PDF_TITLE'.pdf' INTO LV_PDF_NAME.
REFRESH: LT_PDF_BIN.
CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
EXPORTING
BUFFER = LS_FORMOUTPUT-PDF
TABLES
BINARY_TAB = LT_PDF_BIN.
IF P_OFFLN IS NOT INITIAL AND P_FOLDER IS NOT INITIAL AND P_TEST IS INITIAL." AND P_ATTCH IS INITIAL.
CALL FUNCTION 'GUI_DOWNLOAD'
EXPORTING
FILENAME = LV_PDF_NAME
FILETYPE = 'BIN'
TABLES
DATA_TAB = LT_PDF_BIN
EXCEPTIONS
FILE_WRITE_ERROR = 1
NO_BATCH = 2
GUI_REFUSE_FILETRANSFER = 3
INVALID_TYPE = 4
NO_AUTHORITY = 5
UNKNOWN_ERROR = 6
HEADER_NOT_ALLOWED = 7
SEPARATOR_NOT_ALLOWED = 8
FILESIZE_NOT_ALLOWED = 9
HEADER_TOO_LONG = 10
DP_ERROR_CREATE = 11
DP_ERROR_SEND = 12
DP_ERROR_WRITE = 13
UNKNOWN_DP_ERROR = 14
ACCESS_DENIED = 15
DP_OUT_OF_MEMORY = 16
DISK_FULL = 17
DP_TIMEOUT = 18
FILE_NOT_FOUND = 19
DATAPROVIDER_EXCEPTION = 20
CONTROL_FLUSH_ERROR = 21
OTHERS = 22.
IF SY-SUBRC NE 0.
LV_MSG = 'Failed to download Vesting PDF.'.
DATA: LS_OUTPUTPARAMS TYPE SFPOUTPUTPARAMS.
DATA: LS_FORMOUTPUT TYPE FPFORMOUTPUT.
DATA: LV_FM_NAME TYPE RS38L_FNAM.
DATA: LS_DOCPARAMS TYPE SFPDOCPARAMS.
DATA: LS_ZESOS_RSUGL_PDF TYPE ZESOS_RSUGL_PDF.
DATA: LV_MSG TYPE STRING.
CLEAR LS_ZESOS_RSUGL_PDF.
LS_ZESOS_RSUGL_PDF-PERNR = PERNR-PERNR.
LS_ZESOS_RSUGL_PDF-BEGDA = PN-BEGDA.
LS_ZESOS_RSUGL_PDF-ENDDA = PN-ENDDA.
LS_ZESOS_RSUGL_PDF-CPLAN = P_CPLAN.
LS_ZESOS_RSUGL_PDF-CITEM = P_CITEM.
"Get name of the generated function module
CALL FUNCTION 'FP_FUNCTION_MODULE_NAME'
EXPORTING
I_NAME = 'Z_PDF_FROM_TCODE_SFP'
IMPORTING
E_FUNCNAME = LV_FM_NAME.
"Set the output parameters.
LS_OUTPUTPARAMS-NODIALOG = 'X'.
LS_OUTPUTPARAMS-PREVIEW = ''.
LS_OUTPUTPARAMS-GETPDF = 'X'.
"Open print job
CALL FUNCTION 'FP_JOB_OPEN'
CHANGING
IE_OUTPUTPARAMS = LS_OUTPUTPARAMS
EXCEPTIONS
OTHERS = 1.
"Set the PDF to be dynamic and interactive.
LS_DOCPARAMS-FILLABLE = ''.
LS_DOCPARAMS-DYNAMIC = 'X'.
"Generate the PDF.
CLEAR LS_FORMOUTPUT.
CALL FUNCTION LV_FM_NAME
EXPORTING
/1BCDWB/DOCPARAMS = LS_DOCPARAMS
ZESOS_RSUGL_PDF = LS_ZESOS_RSUGL_PDF
IMPORTING
/1BCDWB/FORMOUTPUT = LS_FORMOUTPUT
EXCEPTIONS
USAGE_ERROR = 1
SYSTEM_ERROR = 2
INTERNAL_ERROR = 3
OTHERS = 4.
IF SY-SUBRC NE 0.
LV_MSG = 'Failed to generate PDF.'.
PERFORM FILL_MSG USING LV_MSG
CHANGING GS_OUTREP.
MODIFY GT_OUTREP FROM GS_OUTREP INDEX GV_TABIX.
ENDIF.
CHECK SY-SUBRC = 0.
"Close print job.
CALL FUNCTION 'FP_JOB_CLOSE'.
*--------------------------------------------------------------------*
DATA: LT_PDF_BIN TYPE TABLE OF X255.
DATA: LV_PDF_NAME TYPE STRING.
CONCATENATE P_FOLDER '\PDF_TITLE'.pdf' INTO LV_PDF_NAME.
REFRESH: LT_PDF_BIN.
CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
EXPORTING
BUFFER = LS_FORMOUTPUT-PDF
TABLES
BINARY_TAB = LT_PDF_BIN.
IF P_OFFLN IS NOT INITIAL AND P_FOLDER IS NOT INITIAL AND P_TEST IS INITIAL." AND P_ATTCH IS INITIAL.
CALL FUNCTION 'GUI_DOWNLOAD'
EXPORTING
FILENAME = LV_PDF_NAME
FILETYPE = 'BIN'
TABLES
DATA_TAB = LT_PDF_BIN
EXCEPTIONS
FILE_WRITE_ERROR = 1
NO_BATCH = 2
GUI_REFUSE_FILETRANSFER = 3
INVALID_TYPE = 4
NO_AUTHORITY = 5
UNKNOWN_ERROR = 6
HEADER_NOT_ALLOWED = 7
SEPARATOR_NOT_ALLOWED = 8
FILESIZE_NOT_ALLOWED = 9
HEADER_TOO_LONG = 10
DP_ERROR_CREATE = 11
DP_ERROR_SEND = 12
DP_ERROR_WRITE = 13
UNKNOWN_DP_ERROR = 14
ACCESS_DENIED = 15
DP_OUT_OF_MEMORY = 16
DISK_FULL = 17
DP_TIMEOUT = 18
FILE_NOT_FOUND = 19
DATAPROVIDER_EXCEPTION = 20
CONTROL_FLUSH_ERROR = 21
OTHERS = 22.
IF SY-SUBRC NE 0.
LV_MSG = 'Failed to download Vesting PDF.'.
ELSE.
LV_MSG = 'Successfully download RSU Notification of Vesting PDF.'.
LV_MSG = 'Successfully download RSU Notification of Vesting PDF.'.
ENDIF.
ENDIF." IF P_OFFLN IS NOT INITIAL AND P_FOLDER IS NOT INITIAL AND P_TEST IS INITIAL." AND P_ATTCH IS INITIAL.
*--------------------------------------------------------------------*
DATA: LV_RECIPIENT TYPE AD_SMTPADR,
* LV_SUBJECT TYPE SO_OBJ_DES VALUE 'RSU Notification of Vesting',
LV_SUBJECT TYPE SO_OBJ_DES VALUE 'Subject title',
* LV_SUBJECT TYPE SO_OBJ_DES VALUE 'Notification of Vesting of the First Performance-Based Restricted Share Unit Scheme ("RSU")',
LV_SENDER TYPE SY-UNAME VALUE 'USERNAME'.
LV_RECIPIENT = 'email@email.com'.
PERFORM SEND_EMAIL_OFFLN TABLES LT_PDF_BIN USING SY-UNAME LV_RECIPIENT LV_SUBJECT.
ENDFORM. " DOWNLOAD_PDF_RS_VESTING
ENDIF." IF P_OFFLN IS NOT INITIAL AND P_FOLDER IS NOT INITIAL AND P_TEST IS INITIAL." AND P_ATTCH IS INITIAL.
*--------------------------------------------------------------------*
DATA: LV_RECIPIENT TYPE AD_SMTPADR,
* LV_SUBJECT TYPE SO_OBJ_DES VALUE 'RSU Notification of Vesting',
LV_SUBJECT TYPE SO_OBJ_DES VALUE 'Subject title',
* LV_SUBJECT TYPE SO_OBJ_DES VALUE 'Notification of Vesting of the First Performance-Based Restricted Share Unit Scheme ("RSU")',
LV_SENDER TYPE SY-UNAME VALUE 'USERNAME'.
LV_RECIPIENT = 'email@email.com'.
PERFORM SEND_EMAIL_OFFLN TABLES LT_PDF_BIN USING SY-UNAME LV_RECIPIENT LV_SUBJECT.
ENDFORM. " DOWNLOAD_PDF_RS_VESTING