简体   繁体   中英

Emails sent via nodemailer from my server come through as spam: (unknown sender)

First time using Node and working with email configuration. I downloaded this application from here , it works (uses mustache.js to generate templates for the emails), but the test email ends up in my gmail spam.

from:   via vps1234.inmotionhosting.com 
to: thisisme@gmail.com
date:   Tue, Aug 8, 2017 at 5:30 PM
subject:    Thanks! and review your experience
mailed-by:  vps1234.inmotionhosting.com
security:    Standard encryption (TLS) Learn more

-

var nodemailer = require('nodemailer'); 
let transporter = nodemailer.createTransport({
      service: ' ',
      secure: false,  
      port: 25,
      auth: { 
            user: 'thisisme@mydomain.com',
            pass: 'password1234'
        },
      tls: {
        rejectUnauthorized: false,
    }
 }),


EmailTemplate = require('email-templates').EmailTemplate,
path = require('path'),
Promise = require('bluebird');

let users = [
    {
        name: 'John',
        note: 'I found your purse',
        email: 'recipient@gmail.com',
    }
];



function sendEmail (obj) {
    return transporter.sendMail(obj);
}

function loadTemplate (templateName, contexts) {
    let template = new EmailTemplate(path.join(__dirname, 'templates', templateName));
    return Promise.all(contexts.map((context) => {
        return new Promise((resolve, reject) => {
            template.render(context, (err, result) => {
                if (err) reject(err);
                else resolve({
                    email: result,
                    context,
                });
            });
        });
    }));
}

loadTemplate('welcome', users).then((results) => {
    return Promise.all(results.map((result) => {
        sendEmail({
            to: result.context.email,
            from: 'Me :)',
            subject: result.email.subject,
            html: result.email.html,
            text: result.email.text,
        });
    }));
}).then(() => {
    console.log('Yay!');
});

This is Nodemailer boilerplate, which I tested also on my server, and it worked correctly, and the email did not get flagged:

var nodemailer = require('nodemailer');

let transporter = nodemailer.createTransport({
    service: ' ',
    secure: false,
    port: 25,
    auth:{
        user: 'thisisme@mydomain.com',
        pass: 'password1234'
    },
    tls: {
        rejectUnauthorized: false,
    }
});


let helperOptions = {
    from: '<thisisme@mydomain.com>',
    to: 'recipient1234@gmail.com',
};

transporter.sendMail(helperOptions, (error, info) =>{
    if(error){return alert(error);}
    console.log("sent" . info);
})

A message being marked as spam is not so much a function of the code that generated it but of a few other things such as:

  • the actual content (text, links etc)
  • the domain it came from

The first issue is just something you have to play with. There are services such as MailMonitor and others that help you tweak your content to see how gmail and others handle it. Wording, HTML vs plain text, links vs none, etc all play a part.

As far as the domain, you'll want to setup your SPF (Sender Policy Framework) and DKIM entries to essentially validate your domain as a proper sender. The "unknown sender" is most likely a missing SPF record.

For SPF here's an article . For DKIM here's another

Note that I just googled for this - they seemed like good articles but I am sure there are other great sources.

The gist of it is that you'll want to create a couple of TXT entries in your DNS. There are tools such as SPF generators to help you with this. It's not as complex as it may sound.

Once that is done you might still end up in spam but it will certainly help. You can experiment with services such as Amazon SES and SendGrid which might provide better sending "validity" than your current SMTP server.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM