DEV Community

Cover image for Send email in Node.JS with Nodemailer using Gmail account

Send email in Node.JS with Nodemailer using Gmail account

Bentil Shadrack on October 21, 2022

Environment & Dependencies Nodemailer is a Node.JS npm module for sending emails. This has been the go to solution to most NodeJS ...
Collapse
 
hirenprajapatiitpath profile image
Hiren Prajapati

Not working internal style in html page

Collapse
 
qbentil profile image
Bentil Shadrack

Really?
Let me see a snippet of your code

Collapse
 
hirenprajapatiitpath profile image
Hiren Prajapati • Edited

const welcomeEmailTemplate = (fullName) => {

return `

<!DOCTYPE html>









Social Space

<br>
@import url(&quot;<a href="https://fonts.googleapis.com/css2?family=Signika&amp;display=swap%22" rel="nofollow">https://fonts.googleapis.com/css2?family=Signika&amp;amp;display=swap&amp;quot;&lt;/a&gt;);&lt;/p>
<div class="highlight"><pre class="highlight plaintext"><code> :root {
--primary-color: #8344ff;
}

body {
margin: 0;
padding: 0;
font-family: "Signika", sans-serif;
}

h1 {
margin: 0;
color: var(--primary-color);
}

p {
margin: 0;
text-align: justify;
}

a {
text-decoration: none;
color: black;
}
a:hover {
color: var(--primary-color);
}

li::marker {
margin: 0;
}

img {
width: 100%;
max-height: 300px;
margin: 20px 0px;
}

.container {
padding: 25px;
border: 2px solid var(--primary-color);
border-radius: 15px;
}
&lt;/style&gt;
</code></pre></div>
<p></head><br>
<body><br>
<div class="container"><br>
<h1>Your Space for Connecting and Sharing !</h1><br>
<br /><br>
<p>Hey, ${fullName} 👋🏻</p><br>
<img<br>
src="https://res.cloudinary.com/socialspace/image/upload/v1690739392/email-vectors/welcomeVector.svg"&lt;br>
alt="welcome"<br>
/><br>
<p><br>
We are thrilled to extend a warm welcome to you as a new member of our<br>
ever-growing community at<br>
<a style="color: var(--primary-color)" href="">Social Space</a> !<br>
</p><br>
<br /><br>
<p><br>
Your decision to join Social Space brings us one step closer to building<br>
a vibrant online space where users like you can connect with friends,<br>
family, and like-minded individuals, sharing your thoughts, memories,<br>
and experiences with the world.<br>
</p><br>
<br /><br>
<p><br>
Here at Social Space, our mission is to create an inclusive environment<br>
that fosters meaningful interactions, celebrates diversity, and respects<br>
the privacy and security of all our users.<br>
</p><br>
<br /><br>
<p><br>
If you have any questions, concerns, or suggestions, please don&#39;t<br>
hesitate to reach out to our friendly support team at<br>
<a<br>
style="color: var(--primary-color)"<br>
href=""<br>
></a<br>
><br>
We&#39;re here to ensure that your experience on Social Space is as<br>
enjoyable as possible.<br>
</p><br>
<br /><br>
<p><br>
Once again, thank you for choosing <a style="color: var(--primary-color)" href="">Social Space</a>. We are genuinely<br>
excited to have you on board and cannot wait to see the positive impact<br>
you will make within our community.<br>
</p><br>
<br /><br>
<p>Best regards,</p><br>
<br /><br>
<p>The Social Space Team</p><br>
<li><a href="">Yogesh Zala</a></li><br>
<li><a href="">Hiren Prajapati</a></li><br>
</div><br>
</body><br>
</html><br>
`;<br>
};</p>

<p>module.exports = welcomeEmailTemplate;</p>

<p>async function sendWelcomeEmail(email, fullName) {<br>
const mailOptions = {<br>
from: <code>Social Space &lt;${process.env.EMAIL}&gt;</code>,<br>
to: email,<br>
subject: &quot;Welcome to Social Space ! 🖐🏻&quot;,<br>
html: welcomeEmailTemplate(fullName),<br>
};</p>

<p>await sendEmail(mailOptions);<br>
}</p>

Thread Thread
 
qbentil profile image
Bentil Shadrack

The welcomeEmailTemplate looks fine and sould work.

Perhaps the issue will be your implementation. What error do you get?
Send me a mail let's take a close look at it😁

Thread Thread
 
hirenprajapatiitpath profile image
Hiren Prajapati

Thank You for response!
error not show any but styling is not show when i have sent email to someone only show content without styling....

Thread Thread
 
qbentil profile image
Bentil Shadrack • Edited

Oh I see.

Did you use this👇👇

Image description

Thread Thread
 
hirenprajapatiitpath profile image
Hiren Prajapati

Yes, I have used!

Thread Thread
 
hirenprajapatiitpath profile image
Hiren Prajapati

support.google.com/mail/thread/476...
Please check the above Google support link! I think have you idea.

Thread Thread
 
qbentil profile image
Bentil Shadrack

Okay sure

Collapse
 
cyklonehateka profile image
Cyklone Hateka

Very informative

Collapse
 
qbentil profile image
Bentil Shadrack

Thank you

Collapse
 
simvolick profile image
Simvolick

Am I crazy but this doesn't work without getting gmail api key?

Collapse
 
qbentil profile image
Bentil Shadrack

This works fine without any API key.
How are you implementing it?

Collapse
 
amithbhagat profile image
Amit H Bhagat

Thanks for the "Application-specific password required" error resolution

Collapse
 
qbentil profile image
Bentil Shadrack

I am glad it helped

Collapse
 
junaid975a profile image
Junaid Ansari

Hey, i'm using my actual emailId and emailPassword to send mails using nodemailer, with smpt.gmail.com as host, still i'm not able to send the mails and getting the error of false credentials. can anyone please explain me why i'm getting such error?

Collapse
 
junaid975a profile image
Junaid Ansari

`const nodemailer = require("nodemailer");
require("dotenv").config();

const mailSender = async (email, title, body) => {
try{
// mail send krne ke liye transporter use hota h
let transporter = nodemailer.createTransport({
host: process.env.MAIL_HOST,
auth:{
user: process.env.MAIL_USER,
pass: process.env.MAIL_PASS,
}
});
// console.log(body);
// console.log(transporter);

    // prepare the email and send
    let info = await transporter.sendMail({
        from: "StudyNotion --by Junaid",
        to: `${email}`,
        subject: `${title}`,
        html: `${body}`
    });

    return info;

} catch(err) {
    console.log("Mail Sender failed.")
    console.log(err.message);
}
Enter fullscreen mode Exit fullscreen mode

}

module.exports = mailSender;`

above is my code snippet for mailSender
please can anyone tell me, where i'm doing it wrong?

Collapse
 
qbentil profile image
Bentil Shadrack

You are not suppose to use your actual gmail password here.

Generate an APP password from your google account and use that instead

Thread Thread
 
vibhatia profile image
Vibhatia

Bro my original mail is sent to the user. Not the 'from' one. Why is it so?

Thread Thread
 
qbentil profile image
Bentil Shadrack

You original email is used for the SMTP config so it is what Gmail uses.

Thread Thread
 
vibhatia profile image
Vibhatia

Is there any way to send an email via the from one?

Collapse
 
vibhatia profile image
Vibhatia

You have to modify the password by going to the security section of gmail.