Although Gadget provides a default email/password authentication method, there is a lot of flexibility to configure or build on top of the user emails sent through emails in the action context.
Custom email templates
Whether you want to customize the copy and style of the emails you send on the sendVerifyEmail/sendResetPassword action or send any other transactional/marketing emails within your app, you can easily do so.
Create a constant value representing the desired custom email template (CustomTemplate) and declare an expression containing an HTML 5 document.
Structure this document however you want your email copy and style to be.
Now to render your custom template, when sending an email (via emails.sendMail()) within the html parameter you will pass the custom template like below:
Important to note when passing the CustomTemplate through emails.sendMail():
There is no text parameter to pass as that is replaced by the content within your CustomTemplate
Email parameters
The sendMail function takes in a MailData object. The MailData object has the following params:
Name
Type
Description
to
string | Address | Address[]
The address(es) that the email will be sent to. The to param can be an email address or Address object, or an array of either. For example: [email protected] or { address: "[email protected]", name: "John Smith" }
from
string | Address
The from address displayed to users as to who the email came from. This param is optional. If not provided, the from address will be set to noreply@{your-app-slug}.gadget.app.
sender
string | Address
The sender address used to identify the agent responsible for the actual transmission of the email. Can only be set when using a custom transport.
html
string
The HTML body of the email.
attachments
Attachment[]
An array of files to be attached to the email. The attachment is an object with either a path param which represents the string path to the file, or a content param which represents the Buffer or string raw data of the file, and an optional filename param which represents the name of the file. If a filename is not provided, the original name of the file will be used.
When using GadgetMailer, users are restricted from sending emails from anything other than their app's approved subdomain such as: {
anything
}@your-app-slug.gadget.app. No restrictions are set when users are using their own custom transport.
Adding attachments to emails with GadgetMailer
Let's say we have a model called csvFile with a field link which is a file type and we want to dynamically create files to store in our model. To attach a generated csv file to an email we can do the following:
example of sending an email with attachment path in an action
If you decide to use an external email transport like Amazon SES, SendGrid, etc, you do have the ability to configure that within Gadget.
Create a boot file and declare a value representing the new transport. With this value you'll run emails.setTransport(), for those familiar with Nodemailer this is exactly how createTransport() operates
The parameters passed through setTransport() will be entirely dependent upon the type of transport you wish to use with your app. Refer to the Nodemailer or external transport service documentation for more details on parameters passed.
For example if we wanted to use Mandrill (Mailchimp's transactional email service) as our email transporter we would do the following below:
JavaScript
1import{Server, emails }from"gadget-server";
2// import emails within the file
3
4// Create the transporter object
5const transporter ={
6host:"smtp.mandrillapp.com",
7port:587,
8auth:{
9user:"YOUR_MANDRILL_USERNAME",
10pass:"YOUR_MANDRILL_API_KEY",
11},
12};
13
14// Set the transporter to the new configuration
15emails.setTransport(transporter);
In summary to configure an external transporter:
Create a boot file
Declare the transporter (transport) along with any parameters in a separate code file or within the action.
Use emails.setTransport() and pass the transport value.