To send emails programmatically from a JourneyApps application, you will need to make use of a third party service such as Nodemailer or SendGrid.
I have included a basic CloudCode implementation below, which can be extended on to generate dynamic emails to send to end users.
const sgHelper = require('@sendgrid/mail');
const handlebars = require('handlebars');
sgHelper.setApiKey(""); // Add SendGrid API Key here
export async function run() {
let recipient = ''; // Add email here
let message = 'This is a test email.';
await sendEmail(recipient, message)
}
async function sendEmail(recipient, message){
const emailTemplate = handlebars.compile(`
<!DOCTYPE html>
<html>
<body>
<p>Hi {{recipient}}</p>
<p>{{message}}</p>
<p>From,</p>
<p>Test User</p>
</body>
</html>
`);
let _data = {
recipient: recipient,
message: message
}
// Generate an HTML email, using handlebars again
let html = emailTemplate(_data);
let email = {
from: '', // Add the from email here
to: recipient,
subject: 'Your subject here',
html: html
}
try {
await sgHelper.send(email);
} catch(error){
throw error;
}
}