简体   繁体   中英

Error: Invalid from email address - SendGrid

I've been using SendGrid.com to handle password reset email requests for a few years without issue. I haven't changed anything and for the last two days all users have been getting an " Error: Invalid from email address " error on all email requests.

This is happening on all valid email addresses.

For the life of me I cannot figure out why this would randomly start happening. SendGrid seems to be running without issue on their end. I am waiting to get feedback from Sendgrid.

My set up:

    var nodemailer = require('nodemailer');
    var sgTransport = require('nodemailer-sendgrid-transport');

    app.post('/forgot', usernameToLowerCase, function(req, res, next) {
    async.waterfall([
    function(done) {
    crypto.randomBytes(20, function(err, buf) {
    var token = buf.toString('hex');
    done(err, token);
    });
    },
    function(token, done) {
    User.findOne({ username: req.body.username }, function(err, user) {
    if (!user) {
    req.flash('error', 'No account with that email address exists.');
    return res.redirect('/forgot');
    }
    
    user.resetPasswordToken = token;
    user.resetPasswordExpires = Date.now() + 3600000; // 1 hour
    
    user.save(function(err) {
    done(err, token, user);
    });
    });
    },
    function(token, user, done) {
    
    
    var options = {
    auth: {
    api_user: '=====',
    api_key: '======'
    }
    }
    
    var client = nodemailer.createTransport(sgTransport(options));
    
    var email = {
    from: 'BusinessName',
    to: user.username,
    subject: 'Reset Your Password',
    text:'====='
    };
    
    client.sendMail(email, function(err) {
    req.flash('success', 'An email was sent to ' + user.username + ' with further instructions.' );
    done(err);
    });
    }
    ], function(err) {
    if (err) return next(err);
    res.redirect('/forgot');
    });
    });

I was getting the same error message "Invalid from email address" in SendGrid library for php. It seems that SendGrid has made some incompatible changes to their api regarding from field format.

In my case the from field does not accept single email address anymore, so I've changed it to an array with single email address and it worked.

from: ['some@example.com']

If it doesn't help, try to compare the request coming from the transport wrapper with SendGrid documentation https://sendgrid.com/docs/API_Reference/Web_API_v3/index.html . I am not sure how the wrapper works, but the expected format of from field in api seems like this:

"from": {
  "email": "example@example.com"
}

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