In Oracle APEX version 18.1, new feature "Email Templates" has been introduced. Before 18.1, either you have to embed html code in pl/sql code or you have to store HTML templates in one of your tables (or as html files in network share) with "substitution strings" (place holders) and then replace those "substitution strings" with actual values before sending email. If you are using latter approach, then it's easy to migrate your code to new "Email Templates" framework.
When you are creating new email template, you may need to fill data for below fields.
Template Name: Name you want to give to your template. It can't contain special characters. Only characters a-Z, 0-9 and spaces are allowed.
Static Identifier: Unique template name with in the application. This will be used to call the template from APEX_MAIL.SEND
Email Subject: Subject of the email. You can use substitution strings here.
Header: Header of the email. You can use substitution strings here. You can also use HTML to format the content.
Body: Main Body of the email. You can use substitution strings here. You can also use HTML to format the content.
Footer: Any footer notes to the email. You can use substitution strings here. You can also use HTML to format the content.
Advanced > HTML Template: By default, APEX uses HTML template which looks like as below
If you want to modify, you can click on "Load Default HTML" and then you can modify the template. You can also completely replace the HTML template with your own HTML. In these cases, you can use #HEADER#, #BODY# and #FOOTER# substitution strings to refer to "Header", "Body" and "Footer" content.
Plain Text Format > Content: If users are using email clients that does not support HTML content, then this content will be displayed. So if you can show your email content in plain text format, then you can write that content here. If it's not possible, then its better to put some meaningful message here, like "Please open this email from email client which supports HTML content". If you store the emails sent from your system, then you can also give link to the email using which user can see full email content online.
Comments: Developer comments for the email template.
Also, if you go to email template details page, at the bottom, APEX gives you customized "Sample API Usage" code which includes your template static id and all placeholders you have used in the template. (Pretty cool, right?).
To use the template while sending email, just copy this code, replace "some_value" with actual values and replace email_address_of_user with actual email address and then you are good to go.
You may also refer APEX_MAIL.SEND for other supported parameters while using email templates.
Use # with caution: For e.g. if you have below html code in your template, then #000000;color:# will be considered as substitution string and it will be shown in "Sample API Usage".
Creating Email Templates:
You can find email templates at "Shared Components > Other Components > Email Templates" section.When you are creating new email template, you may need to fill data for below fields.
Template Name: Name you want to give to your template. It can't contain special characters. Only characters a-Z, 0-9 and spaces are allowed.
Static Identifier: Unique template name with in the application. This will be used to call the template from APEX_MAIL.SEND
Email Subject: Subject of the email. You can use substitution strings here.
Header: Header of the email. You can use substitution strings here. You can also use HTML to format the content.
Body: Main Body of the email. You can use substitution strings here. You can also use HTML to format the content.
Footer: Any footer notes to the email. You can use substitution strings here. You can also use HTML to format the content.
Advanced > HTML Template: By default, APEX uses HTML template which looks like as below
If you want to modify, you can click on "Load Default HTML" and then you can modify the template. You can also completely replace the HTML template with your own HTML. In these cases, you can use #HEADER#, #BODY# and #FOOTER# substitution strings to refer to "Header", "Body" and "Footer" content.
Plain Text Format > Content: If users are using email clients that does not support HTML content, then this content will be displayed. So if you can show your email content in plain text format, then you can write that content here. If it's not possible, then its better to put some meaningful message here, like "Please open this email from email client which supports HTML content". If you store the emails sent from your system, then you can also give link to the email using which user can see full email content online.
Comments: Developer comments for the email template.
Previewing & Using Email Templates:
Once you have created email template, you can preview your email output using APEX_MAIL.PREPARE_TEMPLATE API.Also, if you go to email template details page, at the bottom, APEX gives you customized "Sample API Usage" code which includes your template static id and all placeholders you have used in the template. (Pretty cool, right?).
To use the template while sending email, just copy this code, replace "some_value" with actual values and replace email_address_of_user with actual email address and then you are good to go.
You may also refer APEX_MAIL.SEND for other supported parameters while using email templates.
Points to Note:
Sample Templates: There are few sample templates provided by APEX Team. You can find them on the right-side of "Template Details" page. Click on the sample template name to load the sample template and then you can modify it as per your requirement.
Use # with caution: For e.g. if you have below html code in your template, then #000000;color:# will be considered as substitution string and it will be shown in "Sample API Usage".
<div style="background-color:#000000;color:#111111;">
As alternative, you can use RGB syntax or color names for specifying colors. Or you can also define your CSS in "Advanced > HTML Template" section. However, you can skip these invalid substitution strings while calling APEX_MAIL.SEND and it does not throw any error and it will not have any impact.
You may refer https://www.campaignmonitor.com/css/ for CSS support in email clients.
Dynamic HTML using substitution Strings: By default, all HTML data is escaped from substitution string values. For e.g. If you pass '<span style="color:red;">data here</span>' for a substitution string, then user will get email with "span" tags. In these cases, if you want HTML to get parsed and show the text in red color, then you can use #SUBSTITUTION_STRING_NAME!RAW# in the email template.
Using Built-in Substitution Strings: Built-in Substitution Strings that support template syntax would also work in email templates. However, looks like not all of them work, but only few. For e.g. APP_IMAGES, IMAGE_PREFIX works in email templates. You no need to pass values for these substitution strings, APEX will do it for you.
Process type "Send E-Mail": This process does not support sending emails using email templates as of APEX version 20.1.
That's it for now and stay tuned for more posts on Email templates topic.
Thank you.
If you like watching videos over reading, you can watch the video here which covers several points on email templates topic.
Comments