How to add a good email in sendgrid api in nodejs - node.js

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);

Related

How to format a JSON string and include a picture?

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.

Changing nodemailer "from" filed to a variable

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

Kentico Email Template Macros with HTML code

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.

Send email from FirefoxOS app with content

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.

PHPMailer Body gets truncated

I'm having some strange issues with PHPMailer. I'm trying to send some content which I generate with PHP in HTML and plain text, but the body gets truncated. What's even stranger is that this happens only to the email I generate, if I put in there some generic content in much greater length, it gets sent properly. I must also mention, I did echo the content of both $content and $nohtmlcontent variables and everything is there like it should be, but when I receive email into my mailbox, it's truncated.
My PHP code for creating plain text and HTML email body:
$content="<BODY bgColor=\"#ffffff\"><FONT face=\"Verdana\" size=\"2\">";
$content.="Hello $name.<br /><br />Administrator of $url has created a new account for you.<br /><br />Your new account details:<br />";
$content.=$message."<br /><br />";
$content.="If you see something wrong, please reply with correct details and we will update your account.<br /><br />";
$content.="Have a nice day,<br />$url</FONT></FONT></BODY>";
$nohtmlcontent="Hello $name.\n\nAdministrator of $url has created a new account for you.\n\nYour new account details:\n\n";
$nohtmlcontent.=$usrEmail."\n\n";
$nohtmlcontent.="If you see something wrong, please reply with correct details and we will update your account.\n\n";
$nohtmlcontent.="Have a nice day,\n$url";
All variables are populated with proper data.
My PHPMailer code for sending email:
require_once("class.phpmailer.php");
$mail=new PHPMailer(true);
try {
$mail->AddAddress($email);
$mail->SetFrom('admin#example.com', 'example.com');
$mail->CharSet = 'UTF-8';
$mail->Subject = "New account for you";
$mail->IsHTML(true);
$mail->AltBody = $nohtmlcontent;
$mail->Body = $content;
$mail->Send();
return true;
}catch(phpmailerException $e){
trigger_error("PHPMailer failed: ".$e->errorMessage());
return false;
}
Result:
Hello 12 23.
Administrator of admin.localhost.dev has created a new account for you.
Your new account details:
Username: user1
Password: 123456
E-Mail Address: info#tourazore.com
Subscription Status: Not Verified (you must verify your email address before you can use your account)
Package: Free (limitations: 1 tour, 5 items)
First Name: 12
Last Name: 23
City 34
Country 45
Your verification link: http://admin.localhost.dev/verify-account/882672636ce2ad8c498f75a9b836ff055aecf573/
If you see something wrong, please reply with correct details and we will update you
Expected result:
Hello 12 23.
Administrator of admin.localhost.dev has created a new account for you.
Your new account details:
Username: user1
Password: 123456
E-Mail Address: info#tourazore.com
Subscription Status: Not Verified (you must verify your email address before you can use your account)
Package: Free (limitations: 1 tour, 5 items)
First Name: 12
Last Name: 23
City 34
Country 45
Your verification link: http://admin.localhost.dev/verify-account/882672636ce2ad8c498f75a9b836ff055aecf573/
If you see something wrong, please reply with correct details and we will update your account.
Have a nice day,
admin.localhost.dev
Please notice the extra content in the end.
I have also tried using PHP's function mail() to send the same content, it also gets truncated.
Any ideas?
SOLUTION: The PHP code generated really long line, after adding a few newline characters, the complete content got through.
Also this may not be related but you're using Body and not MsgHTML
$mail->Body = $content;
But it looks like your using HTML expressions in your content.
I'm fairly new to this but from what I've read to use HTML in PHPMailer you should use
$mail->MsgHTML = $content;
Although your text looks likes it is displaying fine and you solved your problem. But I thought I'd share incase it helps.
Some useful info here
https://phpbestpractices.org/
(scroll down to email info)
and here:
https://github.com/PHPMailer/PHPMailer

Resources