Change the design of email body - email

I have written the code for sending email in email.js as follows:
Accounts.emailTemplates.siteName = "xyz";
Accounts.emailTemplates.from = "xyz <admin#xyz.com>";
Accounts.emailTemplates.verifyEmail = {
subject() {
return "[xyz] Verify Your Email Address";
}
};
Accounts.emailTemplates.verifyEmail.text = function( user, url) {
let emailAddress = user.emails[0].address,
urlWithoutHash = url.replace( '', '' ),
supportEmail = "support#xyz.com",
emailBody = `To verify your email address (${emailAddress}) visit the following link:\n\n${urlWithoutHash}\n\n If you did not request this verification, please ignore this email. If you feel something is wrong, please contact our support team: ${supportEmail}.`;
return emailBody;
}
The email is working and all I want is to change the Design. How to design the email body? Can I insert the html code inside the email body so that I can have a proper responsive email design? I have tried in many ways. Can anyone please help me out?
I have used mail gun API for sending emails is there anyway to use template.
I have tried with grunt email template and am struck with that I need help to get complete my task.

You can create an email template using SSR package.
Accounts.emailTemplates.verifyEmail.html = function (user, url) {
SSR.compileTemplate( 'registartionEmail', Assets.getText( 'email_templates/registration_confirm.html' ) );
var emailData = {
x: y;
};
return SSR.render( 'registartionEmail', emailData );
};

To handle the process of converting templates into raw HTML on the server, you need to add a package to your application called meteorhacks:ssr. Once you have the package installed, you can store plain HTML files inside your /private directory and then convert them later, passing any data to replace handlebars helpers like {{name}} in the process.
For example, here is some code I have used to send a welcome email when new users register:
import { SSR } from 'meteor/meteorhacks:ssr';
const getHTMLForEmail = (templateName, data) => {
SSR.compileTemplate(templateName, Assets.getText(`email/templates/${templateName}.html`));
return SSR.render(templateName, data);
};
const sendEmail = (emailAddress, html) => {
if (emailAddress.includes('#')) {
const emailData = {
to: emailAddress,
from: 'Test Email <hello#test.io>',
subject: 'Welcome aboard, team matey!',
html,
};
Meteor.defer(() => Email.send(emailData));
}
};
export const sendWelcomeEmail = (user, profile) => {
let email;
if (user.services.facebook) {
email = user.services.facebook.email;
} else if (user.services.google) {
email = user.services.google.email;
}
const data = {
email,
name: profile && profile.name ? profile.name : '',
url: 'www.google.com',
};
const html = getHTMLForEmail( 'welcome-email', data );
sendEmail(data.email, html);
};
You will find the following two articles from Meteor Chef very useful (also shows how the html email template looks like):
Using the email package
Sign up with email verification

Related

Strapi custom email template

I'm using nodemailer for email submission and running from my localhost. I have email services created manually in the following dir /api/email/services/Email.js
const nodemailer = require('nodemailer');
const transporter = nodemailer.createTransport({
host: 'example.com',
port: 587,
secure: false,
auth: {
user: 'user',
pass: 'password',
},
});
module.exports = {
send: (from, to, subject, html) => {
const options = {
from,
to,
subject,
html
};
return transporter.sendMail(options);
},
};
So then I can use it like strapi.services.email.send(from, email, subject, html);
Usually, I write my html template in the code line const html = '<p>Email testing</p>' to be passed in the email services. But I don't want to do this for every email submission from different controllers.
So, I created a html template in /config/email-templates/custom-email.html and tried to call it like const html = path.join(__dirname + '/../../../config/email-templates/custom-email.html');.
When I run it, the email can be sent successfully but it cannot render the html. Instead of a rendered html, it's showing the full path of the custom-email.html as the email message. Is this method possible to achieve in strapi?
Instead of passing the path to the file, you need to pass the actual content. In the first case const html = '<p>Email testing</p>' , you are actually passing the content , but in the second case you are passing the file path.
Modified send method could look something like below:
send: (from, to, subject, htmlpath) => {
const readHTMLFile = (path, callback)=> {
fs.readFile(path, {encoding: "utf-8"}, function (err, html) {
if (err)
return callback(err);
else
return callback(null, html);
});
}
readHTMLFile(htmlpath, function(err, html) {
const options = {
from,
to,
subject,
html
};
return transporter.sendMail(options);
}); }

Using nodemailer with G suite

I have tried using nodemailer with gmail and it works. I tried to use the same code with my G suite name#mydomain.com email and there's bad credentials error. Is there an easy way to use nodemailer with G suite? because I want my business mail to be seen as sender.
this is the code I am using:
let transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: 'name#business.com',
pass: 'password'
}
});
exports.sendMail = functions.https.onRequest((req, res) => {
cors(req, res, () => {
// getting dest email by query string
const dest = req.query.dest;
const mailOptions = {
from: 'Your Account Name <aegegwgwg#gmail.com>', // Something like: Jane Doe <janedoe#gmail.com>
to: dest,
subject: 'I\'M A PICKLE!!!', // email subject
html: `<p style="font-size: 16px;">Pickle Riiiiiiiiiiiiiiiick!!</p>
<br />
<img src="https://images.prod.meredith.com/product/fc8754735c8a9b4aebb786278e7265a5/1538025388228/l/rick-and-morty-pickle-rick-sticker" />
` // email content in HTML
};
// returning result
return transporter.sendMail(mailOptions, (erro, info) => {
if(erro){
return res.send(erro.toString());
}
return res.send('Sended');
});
});
});

How to send mail with attachment in lambda nodeJs using SES service

I am trying to send a mail with attachment.
I have already used SES service for sending simple and HTML content mail. but now I want to send a mail with attachments.
I am using amazon SES service for sending emails and I am using the 'sendRawEmail' method for send mail with attachments.
I am getting an error message like this.InvalidParameterValue: Nested group
I didn't find any Node examples for this type of error.
I am having a very frustrating time trying to send emails with NodeJS SES API.
I found the issue and i fixed that with using mailcomposer npm package.
Now I an able to send a Mails with attachments.
Install AWS SDK
npm i aws-sdk
Now Install manilcomposer
npm i mailcomposer
Full code below
Request body:
if you want to send mail With Base64 content.
{
email: ['jen****#gmail.com', 'Aa****#gmail.com'],
from: 'no-reply#*****.com',
subject: 'Sending emails with attachments',
text: 'please find attachments',
attachments: [{
filename: 'sample.pdf',
content: {{file}}, // file content base64 staring
encoding: 'base64',
}]
}
if you want to send a mail with the file path.
{
email: ['jen****#gmail.com', 'Aa****#gmail.com'],
from: 'no-reply#*****.com',
subject: 'Sending emails with attachments',
text: 'please find attachments',
attachments: [{
filename: 'sample.pdf',
path: './home/sample,pdf'
}]
}
Business logic Code:
const ses = new AWS.SES({ region: 'us-east-1' }); //specify region for the particular see service.
const mailcomposer = require('mailcomposer');
exports.handler = function (event, context) {
console.log('Event: ',JSON.stringify(event));
if (!event.email) {
context.fail('Missing argument: email');
return;
}
if (!event.subject) {
context.fail('Missing argument: subject');
}
if (!event.from) {
context.fail('Missing argument: from');
}
if (!event.html && !event.text) {
context.fail('Missing argument: html|text');
}
const to = event.email;
const from = event.from;
const subject = event.subject;
const htmlBody = event.html;
const textBody = event.text;
const attachments = event.attachments;
const mailOptions = {
from: from,
subject: subject,
text: textBody,
html: htmlBody,
to: to,
attachments: attachments ? attachments : []
};
const mail = mailcomposer(mailOptions);
mail.build(function (err, message) {
const req = ses.sendRawEmail({ RawMessage: { Data: message } });
req.on('build', function () {
req.httpRequest.headers['Force-headers'] = '1';
});
req.send(function (err, data) {
if (err) {
console.log(err, err.stack);
context.fail('Internal Error: The email could not be sent.');
} else {
console.log(message);
console.log('The email was successfully sent')
context.succeed('The email was successfully sent');
}
});
});
};
if you have and any query regarding mailcomposer then check [here][1] this link below
[1]: https://nodemailer.com/extras/mailcomposer/

sending email through webpack vuejs

I'm a beginner in vuejs2 and I'm trying to make a simple contact form (using webpack and vuejs2).
I've created my form with the send button pointing to the following method:
<button #click.prevent="sendemail" class="btn btn-xl">Send</button>
And the method:
methods: {
sendemail () {
var mailgun = require('mailgun.js')
var mg = mailgun.client({username: 'MYUSERNAME', key: MYAPIKEY})
mg.messages.create('MYDOMAIN', {
from: 'FROMEMAIL',
to: ['TOEMAIL'],
subject: 'SUBJECT',
text: 'TEXT'
})
.then(msg => console.log(msg)) // logs response data
.catch(err => console.log(err)) // logs any error
}
}
When I press send button I get the following error:
XMLHttpRequest cannot load https://api.mailgun.net/v3/MYDOMAIN/messages. Request header field Authorization is not allowed by Access-Control-Allow-Headers in preflight response.
Any suggestions or any other way to do it?
The mailgun-js docs specify a way on how can we do this:
var api_key = 'key-XXXXXXXXXXXXXXXXXXXXXXX';
var domain = 'www.mydomain.com';
var mailgun = require('mailgun-js')({apiKey: api_key, domain: domain});
var data = {
from: 'Excited User <me#samples.mailgun.org>',
to: 'serobnic#mail.ru',
subject: 'Hello',
text: 'Testing some Mailgun awesomness!'
};
mailgun.messages().send(data, function (error, body) {
console.log(body);
});
Also a word of caution, I would not put the api_key in my frontend as anyone can use it and send emails. Instead, try opting for a backend which will send emails for you.

facebook messenger bot encoding error

I have written sample echo message bot using facebook messenger api and wit.ai actions.
My message from facebook page is received and the proper action function defined using wit api's is also getting called. However
while returning the response, i am getting followin error as -
Oops! An error occurred while forwarding the response to : Error: (#100) Param message[text] must be a UTF-8 encoded string
at fetch.then.then.json (/app/index.js:106:13)
at process._tickCallback (internal/process/next_tick.js:103:7)
Here is the function which is used to return the response -
const fbMessage = (id, text) => {
const body = JSON.stringify({
recipient: { id },
message: { text },
});
const qs = 'access_token=' + encodeURIComponent(FB_PAGE_ACCESS_TOKEN);
return fetch('https://graph.facebook.com/v2.6/me/messages?' + qs, {
method: 'POST',
headers: {'Content-Type': 'application/json; charset=UTF-8'},
body
})
.then(rsp => rsp.json())
.then(json => {
if (json.error && json.error.message) {
throw new Error(json.error.message);`enter code here`
}
return json;
});
};
I have copied this function from the messenger.js file from the documentation since i am just trying the POC.
I checked the values for text and id in this function and verified using console.log statements and those are coming properly.
Can some experts help me to solve this error?
Note - I tried encoding the text using text.toString("utf8"); but it returns the encoding string as [object object] and thats the
response i get from bot. so it doesnt work.
Get the latest code from node-wit, there is a change in facebook id usage,
According to Facebook:
On Tue May 17 format of user and page ids delivered via webhooks will
change from an int to a string to better support default json encoder
in js (that trims long ints). Please make sure your app works with
string ids returned from webhooks as well as with ints.
Still you are getting issue with the api try to add if(event.message && !event.message.is_echo) condition as shown in below code.
// Message handler
app.post('/webhook', (req, res) => {
const data = req.body;
if (data.object === 'page') {
data.entry.forEach(entry => {
entry.messaging.forEach(event => {
if (event.message && !event.message.is_echo) {
const sender = event.sender.id;
const sessionId = findOrCreateSession(sender);
const {text, attachments} = event.message;
if (attachments) {
fbMessage(sender, 'Sorry I can only process text messages for now.')
.catch(console.error);
} else if (text) {
wit.runActions(
sessionId, // the user's current session
text, // the user's message
sessions[sessionId].context // the user's current session state
).then((context) => {
console.log('Waiting for next user messages');
sessions[sessionId].context = context;
})
.catch((err) => {
console.error('Oops! Got an error from Wit: ', err.stack || err);
})
}
} else {
console.log('received event', JSON.stringify(event));
}
});
});
}
res.sendStatus(200);
});
Reference:
no matching user bug
no matching user fix