24

I am sending email through nodemailer it goes into inbox of gmail if i run from local server but goes into spam of gmail if i run script from microsoft azure server. following is my script

var nodemailer = require('nodemailer');
var EmailTemplates = require('swig-email-templates');
var smtpConfig =  {
        service: 'smtp.office365.com',
        host: 'smtp.office365.com',
        port: 587,
        starttls: {
            enable: true
        },
        secureConnection: true,
        auth: {
            user: 'xxxxx@yyyy.com',
            pass: 'zzzzzz'
        }
    }

var templates = new EmailTemplates();  
var transporter = nodemailer.createTransport(smtpConfig);   

var context = {
  username:'Rajesh',
  email:'xxxxx@gmail.com',
  link : 'www.google.co.in'
};

templates.render('activate_email.html', context, function(err, html,text, subject) {    

  transporter.sendMail({
    from: '"Product Name" <no-reply@xxxxx.com>', // sender address
    to: 'xxxx@gmail.com',
      subject: 'Account activation',
      html: html,
      text:text    
  });    
});
General Grievance
  • 4,555
  • 31
  • 31
  • 45
Rajesh N
  • 6,198
  • 2
  • 47
  • 58
  • I followed the email format provided in this answer and it stopped marking my email as spam: https://www.quora.com/How-can-you-send-a-password-email-verification-link-using-NodeJS-1/answer/Arielle-Baldwynn?ch=99&share=8654a1c8&srid=uBOYG – Germa Vinsmoke Jul 14 '19 at 11:42

6 Answers6

29

The truth is there is no simple one line solutions for your problem :) There are numerous reasons why this can happen, and here are some of them:

  • Your host is marked as a spam - this happens if you have not verified your e-mail or you are sending too much e-mails from the same host. Shared hosting is commonly marked as such, and therefore mail server will regularly mark them as a spam

  • Your from field is different than the one you're allowed to use - as I see you're using smtp, there are strict rules for the mail you can send. Of course you can always send e-mail from mark@facebook.com, but since your SMTP's host is not facebook.com, your e-mail will pretty sure marked as spam

  • You can sign your e-mail in many various mails, assuring the servers that this e-mail is send from you and it has proper signature. Check online for the ways to do so.

  • While developing you have sent numerous alike e-mails - sending the very same "test" e-mail is a common reason for your e-mails to get blacklisted

  • There are emojis in your subject - that is not a 100% reason, but servers are often marking such e-mails as spams, especially in other fields (like from)

Unfortunately as I said there is no one real reason, there could be many of them. I hope this helps at least a little :)

Andrey Popov
  • 7,362
  • 4
  • 38
  • 58
  • i'm on heroku using gmail everything is correct (the from is the same as my gmail account no emoji etc..) is there a solution ? – nab Aug 26 '19 at 00:49
8

in my case i needed to specify the form, the from need to be = to user mail

auth: {
  user: "xxx@yyy.com",
  pass: "password",
      }, 
from: xxx@yyy.com, 
asli
  • 433
  • 1
  • 5
  • 10
6

Please get rid of the and try sending it again. I read in a article once that email clients don't like those icons because a lot of spammers are using them.

Try sending it to multiple gmail accounts. Other than that there's nothing wrong with the code. If you're on a shared hosting or local host it could also go into the junk folder. In that case you would have to look into sending the emails from a different IP, preferred in the same country as where you will send the emails to.

But first try to remove that icon!

PS. I would make this answer as a comment but I can't due to low rep.

Kaasstengel
  • 427
  • 1
  • 4
  • 17
5

For those who are still struggling with this problem. I suggest the following.

  1. Make sure you add from field that matches with auth.user
  let transporter = nodemailer.createTransport({
    host: "smtp-mail.outlook.com",
    secureConnection: false,
    port: 587,
    tls: {
      ciphers: "SSLv3",
    },
    auth: {
      user: <YOUR_EMAIL_ACCOUNT>,
      pass: <YOUR_EMAIL_PASSWORD>,
    },
    from: <YOUR_EMAIL_ACCOUNT>,
  });
  1. Add text field describing your intent for the email in options payload
  const mailOptions = {
    from: `myCompany <mycompanyemail@gmail.com>`,
    to: "recipient@gmail.com",
    subject: "[MYCOMPANY] YOUR EMAIL VERIFICATION",
    text: "Hello. This email is for your email verification.",
    html: <h1>Hello</h1>,
  };
  1. If #1 or #2 doesn't solve your problem. Try using test-mailer and send email to their provided test email address. It will show you where your vulnerabilities are especially the part where you have to setup SPF, DKIM, and DMARC.

  2. If you are using a custom domain setup SPF, DKIM, DMARC following this article.

Hitit
  • 420
  • 6
  • 21
2

Late to the party..

Try adding both HTML and Text Versions both in your emailConfig as below. That way the email will land up in the inbox. It worked for me.

var emailOptions = {
from: 'cxxxxxkxxxxx@xxxxx.com',
to: 'xxxx@xxxxxx.com',
cc:'sxxxxxxsh@xxxx.com, xxxa@xxx.com, aaaxxxx@xxxx.com',
    bcc:'xxxxx.wrxxxk@xxxx.com',
    subject: 'xxxxxxxxxx',
    /* Adding HTML and Text Version, so the email will not land up in the Spam folder */
    html: 'Hello Team! <br><br>Please find attached...<br><br>Thanks,<br>XXXXX',
    text: 'Hello Team! <br><br>Please find attached...<br><br>Thanks,<br>XXXXX',
    attachments: [{
        // file on disk as an attachment, concatenating the file extension separately since NODE is not renaming the file properly with fs.renameSync
        filename: finalFileNameWithoutExt + '.xlsx',
        path: reportsLocationPathWithYearMonth + finalFileNameWithoutExt + '.xlsx' // stream this file
    }]
};
adi
  • 29
  • 1
-3

HTML goes to junk, plain text doesn't

Ethan SK
  • 736
  • 8
  • 12