I want to send a mail for every new user who registers at my website. For that purpose, I want to have a text with specific words bold and a logo of the company at the end. I'm using Nodemailer (https://nodemailer.com/about/) by fetching a string from a JSON file that will be sent to the new user. But I'm struggling by formatting the text and including the picture. Is there an easy way to do this?
I would create an html template, populate your data in that template, and then feed it to Nodemailer.
Here is a snippet from the Nodemailer site:
let info = await transporter.sendMail({
from: '"Fred Foo 👻" <foo#example.com>', // sender address
to: "bar#example.com, baz#example.com", // list of receivers
subject: "Hello ✔", // Subject line
text: "Hello world?", // plain text body
html: "<b>Hello world?</b>", // html body
});
Also, your image can be embedded in your html as a base 64 string.
Related
I'm sending some emails via sendgrid and nodejs. I have a content to sent, but i'm unable to add a good body like below
Body:
Hi
You are receiving this email as a reminder to enter time for the day.
Best Regards, Operations Team
I'm able to send mail with the message only and couldn't find a way to add the "Best Regards,
Operations Team" lines. Please give an insight.
my code,
sgmail.setApiKey(process.env.API_KEY);
const msg = {
to: '########', // Change to your recipient
from: "######", // Change to your verified sender
subject: `Reminder for Time Entry`,
text: "Hi You are recieving this email as a reminder to enter time for the day.",
html: "<h1> Hi You are recieving this email as a reminder to enter time for the day.</h1>",
}
sgmail.send(msg);
In plain text you can add line breaks and they will display in the email. Note that you can use backticks (`) to write multiline strings in JavaScript. For HTML emails, you should wrap different lines in different paragraph (<p>) tags. Try the following:
sgmail.setApiKey(process.env.API_KEY);
const textMessage = `Hi
You are receiving this email as a reminder to enter time for the day.
Best Regards, Operations Team`;
const htmlMessages = `<p>Hi</p>
<p>You are receiving this email as a reminder to enter time for the day.</p>
<p>Best Regards, Operations Team</p>`;
const msg = {
to: '########', // Change to your recipient
from: "######", // Change to your verified sender
subject: `Reminder for Time Entry`,
text: textMessage,
html: htmlMessage,
}
sgmail.send(msg);
Could be what I want to do is totally Impossible but I am a newbie so please understand, thanks.
Basically as you can see in my code, that replyTo and subject have a variable from where the information will be pulled from rather than plain text. I would like to know if there is any way I can do that to the from field. I want it to pull the information from the email name input like the subject does but haven't found a way. Cam anyone tell me if there is a way of achieving that?
Thank you guys.
let mailOptions = {
from: `sender <sender#example.com>`,
to: 'me#gmail.com',
replyTo: `<${req.body.Email}>`,
subject: `${req.body.fname} ${req.body.lname} requested a quote`,
text: 'Hello world?',
html: output
};
You want to make sure you have ${variable} in your template strings ` `
// req.body.from = 'sender <sender#example.com>';
let mailOptions = {
from: `${req.body.from}`,
to: 'me#gmail.com',
replyTo: `<${req.body.Email}>`,
subject: `${req.body.fname} ${req.body.lname} requested a quote`,
text: 'Hello world?',
html: output
};
Here is a link to MDN with another example:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals#Expression_interpolation
I set a macro for my html email template like so:
message = message.Replace("\n", "<br>"); //tried with and without
message = Server.HtmlEncode(message); //tried with and without
MacroContext.GlobalResolver.SetNamedSourceData("Message", message);
But the email just renders the <br> tag as text.
I am definitely receiving the html email and not the plain text one.
If I don't manipulate the text and leave it go through with the \n intact and check the email in the sent queue, it displays as it should!
How do I get the newlines to the email template?
This is Example code that is sending the Email and works for me:
var emailTemplate = EmailTemplateProvider.GetEmailTemplate(EmailName, SiteContext.CurrentSiteID);
var message = "<br/>";
MacroResolver resolver = MacroResolver.GetInstance();
resolver.SetNamedSourceData("Message", message);
EmailMessage message = new EmailMessage();
message.From = resolver.ResolveMacros(emailTemplate.TemplateFrom);
message.Recipients = user.Email;
message.Body = resolver.ResolveMacros(emailTemplate.TemplateText);
message.Subject = resolver.ResolveMacros(emailTemplate.TemplateSubject);
EmailSender.SendEmail(SiteContext.CurrentSiteName, message, emailTemplate.TemplateName, resolver,
false);
Macro in the Email Template:
{%Message%}
Well the HtmlEncode will make your <br> look like text. At least you need to to reverse 1st and 2nd line.
I wish to have a template render some data which needs to be passed back to the caller of render. For example, I am using a template to generate emails, for which I need a subject as well as the body. I would like to do something like this:
app.render( 'email', function(err,html) {
subject = ?get from template somehow?
postEmail( subject, html, user_addr );
});
That is, I wish for the template to decide what should appear in the subject (preferably without creating another template just for the subject line).
Not sure if you figured this out yet, but you can send back information from Jade by altering the value of the arguments.
email.jade:
- subject.text = "Hi " + user + ", welcome to the site.";
| Subject: #{subject.text}
app.js:
args = { user: 'Test User', subject: { text: '' } };
app.render( 'email', args, function(err,html) {
subject = args.subject.text;
postEmail( subject, html, user_addr );
});
It has to be a nested object (i.e. subject.text instead of simply subject), otherwise you won't get the modified data. Although, if you call templates created with jade.compile() directly, then the nesting appears to be unnecessary. I think express must make a shallow copy of the arguments before sending it to the view engine.
I'm trying to send an email from a FirefoxOS App to share content generated by it.
Currently I'm using:
var createEmail = new MozActivity({
name: "new",
data: {
type : "mail",
}
});
But I haven't been able to find any way of appending or attaching content to this email
Thanks to #sebasmagri answer I learnt that the "mailto" URI accepts many more fields than I knew about. Specially interesting is the body and subject:
mailto:someone#example.com?
cc=someone_else#example.com
&subject=This%20is%20the%20subject
&body=This%20is%20the%20body
This allows me to set the different parts of the email as I wanted to.
The final code looks like:
var body = encodeURIComponent(JSON.stringify(event.target.result));
var createEmail = new MozActivity({
name: "new",
data: {
type : "mail",
url: "mailto:?subject=FiREST%20Request&body=" + body,
}
});
It looks like you can set attachments through data.blobs and data.filenames, and misc content (to, subject, content) through data.URI.
Detauls about the mailto: syntax can be found in the MDN entry on Email links.
Regards,
Edit May 2014
As the mail app was refactored, I've dropped the old broken code link in favour of MDN docs.