Send HTML Template Emails

Dear Sciaku Learner you are not logged in or not enrolled in this course.

Please Click on login or enroll now button.

If you have any query feel free to chat us!

Happy Coding! Happy Learning!

Lecture 376:- Send HTML Template Emails

To send HTML template emails using Nodemailer, we first need to create an HTML template that we want to send. Let's create a sample HTML template file called template.html:

htmlCopy code

<!DOCTYPE html> <html>  <head>    <meta charset="utf-8">    <title>Welcome to our Website</title>  </head>  <body>    <h1>Welcome to our Website</h1>    <p>Dear {{ name }},</p>    <p>Thank you for signing up on our website.</p>    <p>We hope you have a great experience with us!</p>  </body> </html>

In the above HTML template, we are using a template variable {{ name }}, which we will replace with the actual name of the recipient when we send the email.

Next, we need to configure Nodemailer to use our SMTP server and set up the transporter:

javascriptCopy code

const nodemailer = require('nodemailer'); const transporter = nodemailer.createTransport({  host: 'smtp.gmail.com',  port: 587,  secure: false,  auth: {    user: '[email protected]',    pass: 'your-email-password'  } });

In the above code, we are using Gmail SMTP to send emails. You should replace the user and pass values with your own Gmail email address and password.

Now, let's write a function to send the email using the HTML template:

javascriptCopy code

async function sendWelcomeEmail(email, name) {  try {    const info = await transporter.sendMail({      from: 'Your Name <[email protected]>',      to: email,      subject: 'Welcome to our Website',      html: `        <h1>Welcome to our Website</h1>        <p>Dear ${name},</p>        <p>Thank you for signing up on our website.</p>        <p>We hope you have a great experience with us!</p>      `    });    console.log(`Email sent: ${info.messageId}`);  } catch (error) {    console.log(error);  } }

In the above code, we are using the transporter.sendMail() method to send the email. We have set the from, to, subject and html fields of the email. We have used string interpolation to replace the {{ name }} variable in the HTML template with the actual name of the recipient.

We have also added a try-catch block to handle any errors that may occur during sending the email.

To send the email, you can call the sendWelcomeEmail() function and pass in the email and name of the recipient:

javascriptCopy code

sendWelcomeEmail('[email protected]', 'John');

This will send the welcome email to the email address [email protected] with the recipient's name set to John.

44. Parallel Jobs + Mailer

Comments: 2

profile
@niteshguptav63
17-Nov-2024, 01:39 PM

I am not able to access videos from second class and further. I have already completed first class

profile
@niteshguptav63
16-Nov-2024, 10:56 AM

When will I get my course?

profile
@admin79
17-Nov-2024, 01:29 PM

Now, Your query was resolved.

Frequently Asked Questions (FAQs)

How do I register on Sciaku.com?
How can I enroll in a course on Sciaku.com?
Are there free courses available on Sciaku.com?
How do I purchase a paid course on Sciaku.com?
What payment methods are accepted on Sciaku.com?
How will I access the course content after purchasing a course?
How long do I have access to a purchased course on Sciaku.com?
How do I contact the admin for assistance or support?
Can I get a refund for a course I've purchased?
How does the admin grant access to a course after payment?