template literals formatting when sending mail - javascript

im sending emails using nodemail and sendgrid. ive structured my email message as the code below but when it is received it looks like that attached image, how can i make the text all align to the left and look normal.
my code
var mailOptions = {
to: admin.email,
from: 'noreply#123.co.uk',
subject: 'Admin Account set up',
text: `Dear ${admin.name} ${admin.surname},
You are receiving this email because your organisation has added you as an admin. To complete the set up, please
use the link below to set a new password and sign in.
https://${req.headers.host}/reset/${token}
Sincerely,
HR Team
This email was automatically generated by 123
`
};
the output:

var mailOptions = {
to: admin.email,
from: 'noreply#123.co.uk',
subject: 'Admin Account set up',
text:
`Dear ${admin.name} ${admin.surname},
You are receiving this email because your organisation has added you as an admin. To complete the set up, please
use the link below to set a new password and sign in.
https://${req.headers.host}/reset/${token}
Sincerely,
HR Team
This email was automatically generated by 123`
}

Related

How to set "to" field (in an email header) to a group email address?

I am using SendGrid in Node.js to send emails to a group of people. THe list is something we maintain on our website. I want the to (aka recipient) field in the email header to show the group email address (e.g., my_group#my_website.com), instead of the recipient email address (e.g., bob#gmail.com). This allow us to separate direct emails from group emails.
In other words, if I receive an email from the group, it should be addressed to the group (e.g., my_group#my_website.com) instead of me in the email header.
When we used Google Groups, we would get the email sent to [group_email]#googlegroups.com, instead of my email address. Below are examples of what happens when an email is sent through Google Groups. I would like some help on replicating that.
This image shows the information associated with a Google Groups email I received. The to field is set to the Google Groups email address, instead of my Gmail address.
I thought it might be just a Google thing, but my Aol email also displays the Google Groups email as for the to field.
This is how we are currently sending the email. I understand I can set the to field to anything. However, if I change that to the group email (e.g., my_group#my_website.com), where would we be putting the email addresses of the recipients?
return sgMail.send({
to: recipient,
from: DEFAULT_SENDER,
subject: renderedMessage.subject,
html: renderedMessage.html,
text: renderedMessage.text,
mailSettings: MAIL_SETTINGS
});
I figured it out. You just need to BCC (or CC) all the recipients and set the to field to the group email address.
It would look like:
return sgMail.send({
to: "my_group#mywebsite.com",
bcc: recipients,
from: DEFAULT_SENDER,
subject: renderedMessage.subject,
html: renderedMessage.html,
text: renderedMessage.text,
mailSettings: MAIL_SETTINGS
});

nodemailer sender is empty

I am trying to use nodemailer with an app that doesn't require any kind of verification. The server the app sits on has been whitelisted, so no username or password is required.
I setup my mail config as follows:
let mailConfig = {
pool: true,
maxConnections: maxConnections,
maxMessages: maxMessages,
rateDelta: rateDelta * 1000,
rateLimit: maxMessages * maxConnections,
secure: false,
ignoreTLS: true,
logger:true,
host: config.SMTP_Host,
port: config.SMTP_Port
};
However I keep bumping into a 550 error because the sender of the email isn't present. I found another stackoverflow question that states when you have a different host domain than the domain of the user (the user in this case being undefined) the sender automatically gets set to that. Which makes sense as to why my sender is being set to blank, even though I have in fact set it.
Is there a way to prevent nodemailer from changing the sender to match the user domain, if the user domain isn't present?
Or am I completely misunderstanding the error message?
So far I've tried to manually set these fields by doing this in the message object but I have to admit I'm not familiar with this type of protocol.
sender:config.SMTP_Host,
envelope: {
from: `"Sender Name" <${config.SMTP_Host}>`, // used as MAIL FROM: address for SMTP
to: `${email}, "${first_name}" <${email}>` // used as RCPT TO: address for SMTP
}
The from field must contain an email address not a hostname.
If we use gmail as an example what you're putting in the from field is
From: "Sender Name" <smtp.gmail.com>
When it should be:
From: "Sender Name" <sender#gmail.com>
The sending server is searching for "smtp.gmail.com" as a email and isn't finding it so you get a message saying the email address doesn't exist.

get email from user in nodemailer

I have a contact form using Nodemailer.
This is my code:
let mailOptions = {
from: '<xxx#xxx.com>', // sender address
to: 'xxx#xxx.com', // list of receivers
subject: 'Kontaktanfrage', // Subject line
html: output // html body
};
In the from I have set up my email but I actually want to see email from the user in my inbox.
Can someone help me to do it?
I would appreciate your help!
Node-mailer is used to send the messages you can also check this reference
there are other packages that works for you, you can check these, it might be your workaround
1.node-mailin
2. mailin

Have a Microsoft Azure web app chatbot proactively contact a user using e-mail or Twilio from a table/list of preset users

I have a simple web app chatbot made with Azure. It is very simple, it will ask a user 3 questions, record the answers to a storage table and repeat the answers back to the user.
I have connected it to Twilio and e-mail using the Channels tab in azure portal. I wonder, is it possible to have a table or database with e-mail addresses or phonenumbers that the bot will contact, preferably if perhaps a variable for a certain address switched from false to true that address would be contacted.
This is the closest example I have found however it uses Skype. Not e-mail or Twilio. https://github.com/Microsoft/BotBuilder-Samples/tree/master/Node/core-CreateNewConversation
I would like something like this part
// Every 5 seconds, check for new registered users and start a new dialog
setInterval(function() {
var newAddresses = userStore.splice(0);
newAddresses.forEach(function(address) {
console.log('Starting survey for address:', address);
// new conversation address, copy without conversationId
var newConversationAddress = Object.assign({}, address);
delete newConversationAddress.conversation;
// start survey dialog
bot.beginDialog(newConversationAddress, 'survey', null, function(err) {
if (err) {
// error ocurred while starting new conversation. Channel not supported?
bot.send(new builder.Message()
.text('This channel does not support this operation: ' + err.message)
.address(address));
}
});
});
to scan a table and if a variable switches it will extract the address from the table and set it as a address variable.
I believe I can figure that out myself however I require help to learn how to make the bot contact a user without the user contacting the bot first, instead using a list of preset users.
If there is any documentation regarding the e-mail channel for a web app bot I have not been able to find it, I would like to know how to have the bot send a e-mail to a user and if there are any JavaScript classes I could use to use the e-mail channel in my bots app.js code.
I hope I made myself understandable, many thanks for any help
With regard to contacting a user without them having first contacted the bot on the Email and Twilio channels you can do the following.
Set up the addresses for each like so:
Email
var emailAddress =
{
channelId: 'email',
user: { id: <userEmail> },
bot: { id: <botEmail>, name: <botName> },
serviceUrl: 'https://email.botframework.com/'
};
SMS
var sms =
{
user: { id: '<userphone>' },
bot: { id: '<botphone>' },
conversation: {id: '<userphone>'},
channelId: 'sms',
serviceUrl: 'https://sms.botframework.com'
};
Note that SMS will not work with a trial twilio number, but will work fine with a purchased number.
After you have your addresses set up, you can route each of them through something like the following code.
var message = new builder.Message()
.address(address)
.text(<messageText>);
try {
console.log(message);
bot.send(message);
} catch (error) {
console.log(error);
}
Will edit this to include loading the user list later if needed.

Sending HTML emails from Node.JS using MailGun

I send email notifications to my users from my app but currently I only send it as a text. I would like to send it HTML emails which are styled.
Currently I tried this:
var data = {
from: 'my app',
to: user.email,
subject: 'Welcome',
html: '<div style="width: 500px; height: 400px: background: #ebebeb; color: #ddd"><p>Hi + "user.firstName" + \n ,this email is to inform you that has added their bio to the knowledge Base \n</p></div>'
};
Compiling the above code does not work, it does not like the styles I have put in.
I have created a separate HTML file within my local directory for each type of email I want send and I would like to be able to attach that html file to my email.
Something like this:
var data = {
from: 'my app',
to: user.email,
subject: 'Welcome',
html: welcomeToSiteEmail.html
};
Is the above possible? Any help will be greatly appreciated.
You can use mailgun-js together with mailcomposer to send HTML formatted emails.
The mailgun-js docs include an example:
var domain = 'mydomain.mailgun.org';
var mailgun = require('mailgun-js')({ apiKey: "YOUR API KEY", domain: domain });
var mailcomposer = require('mailcomposer');
var mail = mailcomposer({
from: 'you#samples.mailgun.org',
to: 'mm#samples.mailgun.org',
subject: 'Test email subject',
body: 'Test email text',
html: '<b> Test email text </b>'
});
mail.build(function(mailBuildError, message) {
var dataToSend = {
to: 'mm#samples.mailgun.org',
message: message.toString('ascii')
};
mailgun.messages().sendMime(dataToSend, function (sendError, body) {
if (sendError) {
console.log(sendError);
return;
}
});
});
Alternately, you could check out nodemailer on npm. It's a great package: easy to use and extensive documentation. With nodemailer, you can do something like this
var nodemailer = require('nodemailer');
var transport = nodemailer.createTransport({
host: 'smtp.mailgun.org',
port: 587,
secure: false,
tls: { ciphers: 'SSLv3' },
auth: {
user: '<Mailgun SMTP login>',
pass: '<Mailgun SMTP password>'
}
});
transport.sendMail({
from: '<Mailgun SMTP login>',
to: ['bob#example.com', 'bill#foobarbaz.com', /*etc*/],
subject: 'Fancy Email',
text: 'still send some text to be on the safe side',
html: { path: 'path/to/email.html' }
}, callback)
// also returns a promise.
However, I would suggest being very thorough in your design of html emails. Writing html email is very different from html in the web. There's a much wider variety of email clients that do will render your html differently, and some, Outlook for Windows and gmail web for example, will not treat your html very nicely. Litmus has some nice resources regarding best practices for designing html emails.
My suggestion would be to use foundation for emails for your styling, use inky to simplify the semantics of writing html email, and inline-css to inline all of your styles. Even if you use alternate methods of sending the mail, check out these resources for designing it. They will save you lots of headache.

Categories