Home > Software engineering >  How can I send E-Mail Notifications in node.js
How can I send E-Mail Notifications in node.js

Time:05-10

My query is simple, all of us receive many e-mail notifications and alerts every day and from various companies. I want to achieve exactly the same. I searched as much as I can, but I could only find the way to send the mail from my personal account (Gmail). But I want to use the company's account to send the mail.

If I use nodemailer, I need to use some smtp-host or some service (like Gmail), then I need to put some credentials (auth) to send.

But if I want to use the private company's account. How can I do it with that? I can't say service: "myPrivateCompany" and and assign properties to auth, it won't work, it works with Gmail, but it won't work with private company,

how can I make it work so the receiver knows which company/website or platform has sent e-mail?

I am a beginner as you know by now. Can someone also explain a bit about the 'service' and smptp-host and port?.

Here's the picture of what I have tried.enter image description here

CodePudding user response:

Hello here are my code I use for my company:

export const sendEmails = async () => {
  try {
    let transporter = nodemailer.createTransport({
      host: "smtp.your server",
      port: server port,
      secure: false,
      auth: {
        user: "email to used to send",
        pass: "That email password",
      },
      tls: {
        rejectUnauthorized: false,
      },
    });

    let info = await transporter.sendMail({
      from: "Sender email",
      to: "email to send to,
      subject: header,
      html: "message",
    });
  } catch (e) {
    throw new Error(e);
  }
};

The above is a function I use to send my email from my nodejs app.Try it am sure it will help

You can also pass in email,message and subject for easie

CodePudding user response:

That is what I am asking, if anyone could define the "service" part. Like it works if I put "gmail" as a service

That just reads some configuration options from a JSON file distributed with Nodemailer.

e.g. the Gmail part looks like:

"Gmail": {
    "aliases": ["Google Mail"],
    "domains": ["gmail.com", "googlemail.com"],
    "host": "smtp.gmail.com",
    "port": 465,
    "secure": true
},

It's only useful if you're using a large third-party email service which Nodemailer has the data for. Otherwise you need to supply the configuration for your mail server explicitly instead of using the service shorthand.

e.g. as per the documentation:

nodemailer.createTransport({
  host: "smtp.example.com",
  port: 587,
  secure: false, // upgrade later with STARTTLS
  auth: {
    user: "username",
    pass: "password",
  },
});
  • Related