parse-smtp-unison-template
v1.0.6
Published
Parse Server Module to send emails via SMTP with individual templates
Downloads
7
Maintainers
Readme
About
Original Author and Project: https://github.com/macarthuror/parse-smtp-template
Customize Parse Servers default Emails
In order to use Parse Servers default Emails and customize them, you need to add the Adapter to your server configuration file like so:
var api = new ParseServer({
appId: 'myParseHosting',
masterKey: 'master123',
...
emailAdapter: {
module: 'parse-smtp-unison-template',
options: {
port: 587,
host: "smtp.mail.com",
user: "[email protected]",
password: "SecurePassword",
fromAddress: '[email protected]',
secure: true,
templates: [
{ name: "default", path: "/views/templates/template.html", variables: { subject: "Email von Parse", username: "[email protected]", btn: "Klicke hier", link: "https://www.google.com", appName: "default appname" } },
{ name: "verificationEmail", path: "/views/templates/verificationEmail.html", variables: { subject: "Email Adresse bestätigen", btn: "Jetzt bestätigen" } },
{ name: "passwordResetEmail", path: "/views/templates/passwordResetEmail.html", variables: { subject: "Passwort zurücksetzen", btn: "Jetzt zurücksetzen" } }
]
}
}
})
Then you need to put your .html Email Template file to the correct path. Feel free to use more templates added the same way.
The variables Object can contain any data you want to use in your email template. Once you've added it in the templates-Array, they become available as ${option.variableName} in the .html Email template file.
Send custom Emails through Parse Clode Code
You can also send emails through Parse Cloud Code by adding the adapter and then passing in variables and template names. Example:
// Define a cloud function in Parse Cloud Code named testmail
Parse.Cloud.define('testmail', async (request) => {
// Load some variables to use in your mailings
const courseName = 'Learn sending mails from parse';
const courseCode = 'SM-1337';
const semester = request.params.semester; // Variable used from the Parse Cloud request
// Define the sendMail Function when we need it and set up server settings and template
const { sendMail } = require('parse-smtp-unison-template')({
user: '[email protected]',
password: "SecurePassword",
fromAddress: '[email protected]',
host: "smtp.mail.com",
port: 465,
secure: true,
templates: [
{ name: 'invoice', path: '/views/templates/invoiceEmail.html', 'variables': { courseName, courseCode, semester } }
// Add more Templates if you like. Careful with path to the template relative to cloud code file
]
});
// Set up more variables, error handling and so on, then send the email
return sendMail({
to: '[email protected]',
subject: 'Invoice for course ' + courseName,
template: 'invoice',
// attachments are also an option as array
});
});
Then use Parse.Cloud.run('testmail') to send your mail in your SDK or other Cloud Code.
All data that you pass in through the sendMail Object overrides the ones in the template. If your template is not found, the one named "default" is used.
All data passed into the variables Object when instantiating the sendMail function overrides already set static data of the template. This is relevant when using password reset or email verify emails from parse.
You can use any variables you wish in your template, but make sure they match the ones you send in your function. E.g. the passed variable courseCode becomes available in our .html Email Template as ${option.courseCode}