web-dev-qa-db-ja.com

nodemailerの「from」フィールドを変更するにはどうすればよいですか?

免責事項:技術的な電子メールの側面はあまり得意ではありません。

だから私は無料の zoho mail アカウントをセットアップしました。これは基本的に私のドメインのメールサーバーです。このちょっとはmxレコード転送か何かを介して動作します、私はそれがどのように動作するか完全にはわかりません。

とにかく、ポイントは次のとおりです。Outlookごとに自分のアカウントを使用すると、fromフィールドを簡単に変更できます。したがって、私のメールアドレス_[email protected]_は、ほとんどのメールクライアントで_Foo from bar.com_と表示されます。

次に、SSLを使用したSMTP経由で、nodemailer(v1.10.0)を使用して_[email protected]_アカウントから自動メールを送信します。私はドキュメント/インターネットで見つけたさまざまなアプローチを試しました。それらはすべて、あいまいなスタックトレースをスローしただけです(以下を参照)。

Fromフィールドの変更をやめるとすぐに、(wrongfromフィールドを除いて)すべて正常に動作します。私は何が起こっているのかわからないので、これをトラブルシューティングするためにいくつかの助けを求めています。

createTransport()の2番目の引数を希望のfromフィールドに変更してみました。動作しませんでした。

_nodemailer.createTransport(auth.mail, {from: auth.mail.auth.user});
_

_nodemailer.createTransport(auth.mail, {from: 'Foo from bar.com'});
_

_auth.mail.from_を設定しようとしましたが、これも機能しませんでした。そして、2番目のパラメータをcreateTransport()に渡して_auth.mail.from_を設定してみました。


私のコード

_var nodemailer = require('nodemailer');
var auth = { mail: { Host: 'smtp.zoho.com', port: 465, secure: true, auth: { user: '[email protected]', pass: 'strongpassword' } };
var log = require('./log');

var transporter = nodemailer.createTransport(auth.mail, {from: auth.mail.auth.user});

function sendText(settings,cb) {
    transporter.sendMail(settings, function (err, info) {
        if (err) {
            log.warn('Failed to send an Email', err);
        } else {
            log.info('Successfully sent email', info);
        }
        if (cb) {
            cb(err, info);
        }
    });
}
_

ここで私が以前に話したスタックトレース

_Message failed
    at SMTPConnection._formatError (c:\...\node_modules\nodemailer\node_modules\nodemailer-smtp-transport\node_modules\smtp-connection\src\smtp-connection.js:388:15)
    at SMTPConnection._actionStream (c:\...\node_modules\nodemailer\node_modules\nodemailer-smtp-transport\node_modules\smtp-connection\src\smtp-connection.js:948:30)
    at SMTPConnection.<anonymous> (c:\...\node_modules\nodemailer\node_modules\nodemailer-smtp-transport\node_modules\smtp-connection\src\smtp-connection.js:579:14)
    at SMTPConnection._processResponse (c:\...\node_modules\nodemailer\node_modules\nodemailer-smtp-transport\node_modules\smtp-connection\src\smtp-connection.js:511:16)
    at SMTPConnection._onData (c:\...\node_modules\nodemailer\node_modules\nodemailer-smtp-transport\node_modules\smtp-connection\src\smtp-connection.js:357:10)
    at emitOne (events.js:77:13)
    at TLSSocket.emit (events.js:169:7)
    at readableAddChunk (_stream_readable.js:146:16)
    at TLSSocket.Readable.Push (_stream_readable.js:110:10)
    at TLSWrap.onread (net.js:523:20)
_
20
boop

FromフィールドはDisplay Name <[email protected]>の形式にする必要があります

transporter.sendMail({ ..., from: 'Foo from @bar.com <[email protected]>' });
23
boop

私はfrom:フィールドで同じ問題が発生し、Gmailの設定を読み直しましたが、これが私が見つけたものです-私が言ったように、GMAILのみです。私はまだ別のプロバイダーを試していません。
src: nodemailer.com/usage/using-gmail/
クライアント:Gmail

Gmailでは、認証されたユーザー名を常にFrom:メールアドレスとして設定します。したがって、foo @ example.comとして認証し、bar @ example.comをfrom:アドレスとして設定すると、Gmailはこれを元に戻し、送信者を認証済みユーザーに置き換えます。

基本的に、認証されたユーザーはfrom:フィールドでのみ指定できます

ありがとう、私が間違っていたら、知らせてください

9
Vic B-A

あなたが見ることができる最初の例 nodemailer.com ウェブサイト。

var nodemailer = require('nodemailer');

// create reusable transporter object using the default SMTP transport
var transporter = nodemailer.createTransport('smtps://user%40gmail.com:[email protected]');
/*set from user in option*/
var mailOptions = {
    from: 'Fred Foo ???? <[email protected]>', // sender address
    to: '[email protected], [email protected]', // list of receivers
    subject: 'Hello ✔', // Subject line
    text: 'Hello world ????', // plaintext body
    html: '<b>Hello world ????</b>' // html body
};

transporter.sendMail(mailOptions, function(error, info){
    if(error){
        return console.log(error);
    }
    console.log('Message sent: ' + info.response);
});
5
xdeepakv