web-dev-qa-db-ja.com

Nodemailerを使用してローカルホストからメールを送信する

ローカルサーバーでメールを送信したいのですが、NodemailerとNodeJSで動作しないようです。

ローカルからメールを送信するための解決策はありますか?

    var contact = {subject: 'test', message: "test message", email: '[email protected]'};
    var to = "[email protected]";
    var transporter = nodemailer.createTransport();
    transporter.sendMail({
      from: contact.email,
      to: to,
      subject: contact.subject,
      text: contact.message
    });
10
tonymx227
  const transporter = nodemailer.createTransport({
    port: 25,
    Host: 'localhost',
    tls: {
      rejectUnauthorized: false
    },
  });

  var message = {
    from: '[email protected]',
    to: '[email protected]',
    subject: 'Confirm Email',
    text: 'Please confirm your email',
    html: '<p>Please confirm your email</p>'
  };

  transporter.sendMail(message, (error, info) => {
    if (error) {
        return console.log(error);
    }
    console.log('Message sent: %s', info.messageId);
  });

私は現在、express.jsのRESTful APIを通じてこれをトリガーしています

これはうまくいくはずです、私はこれを使ってdigitaloceanにpostfixを設定しました: https://www.digitalocean.com/community/tutorials/how-to-install-and-configure-postfix-as-a-send -only-smtp-server-on-ubuntu-16-04

5
suckschool
var nodemailer = require('nodemailer');

// Create a SMTP transport object
var transport = nodemailer.createTransport("SMTP", {
    service: 'Hotmail',
    auth: {
        user: "username",
        pass: "password"
    }
});

// Message object
var message = {

// sender info
from: '[email protected]',

// Comma separated list of recipients
to: req.query.to,

// Subject of the message
subject:req.query.subject, //'Nodemailer is unicode friendly ✔', 

// plaintext body
text: req.query.text //'Hello to myself!',

 // HTML body
  /*  html:'<p><b>Hello</b> to myself <img src="cid:note@node"/></p>'+
     '<p>Here\'s a nyan cat for you as an embedded attachment:<br/></p>'*/
};

console.log('Sending Mail');
transport.sendMail(message, function(error){
if(error){
  console.log('Error occured');
  console.log(error.message);
  return;
}
console.log('Message sent successfully!');

 // if you don't want to use this transport object anymore, uncomment    
 //transport.close(); // close the connection pool
});

SMTPのようなトランスポートプロトコルを指定するか、独自のトランスポートを作成する必要があります。コードでこれを指定していません。

2
aaditya

Localhostでは、メールを送信するために安全で安全な接続が必要なため機能しませんが、gmail [smtp(simple mail transfer protocol)]を使用して機能を実現できます。

最初に設定を行うことを忘れないでください- 安全性の低いアプリにアカウントへのアクセスを許可する

gmailアカウントにアクセスするための所定の権限。

デフォルトでは、この設定はオフであり、オンにするだけです。次にコーディング部分に移ります。

**************************** ---------------------- ---------------------------------------------- **** *****************

const nodemailer = require('nodemailer');

let transporter = nodemailer.createTransport({
Host: 'smtp.gmail.com',
port: 587,
secure: false,
requireTLS: true,
auth: {
    user: '[email protected]', // like : [email protected]
    pass: 'your.gmailpassword'           // like : pass@123
}
});

let mailOptions = {
 from: '[email protected]',
 to: '[email protected]',
 subject: 'Check Mail',
 text: 'Its working node mailer'
};

transporter.sendMail(mailOptions, (error, info) => {
  if (error) {
     return console.log(error.message);
  }
console.log('success');
}); 

@chenielの発言に加えて、「from」メールがトランスポーターオブジェクトのメールと同じユーザーメールであることを確認してください。また、「to」は有効な電子メールであり、存在している必要があります(値が正しく設定されているかどうかを確認します(未定義ではなく、nullでもありません))。

0
nanachimi

ローカルサーバーからの実行では問題になりません。

ダイレクトトランスポートを使用しているようです。ドキュメントで次のように説明されています。

...使用される発信ポート25はデフォルトでブロックされることが多いため、信頼性がありません。さらに、動的アドレスから送信されたメールは、スパムとしてフラグが立てられることがよくあります。 SMTPプロバイダーの使用を検討する必要があります。

1つの解決策は、SMTPトランスポートを定義することです。

var transporter = nodemailer.createTransport({
    service: 'gmail',
    auth: {
        user: '[email protected]',
        pass: 'password'
    }
});

トランスポートプラグインを使用するなど、他のソリューション here があります。

いずれにしても、sendMail関数にコールバックを追加していない場合、問題を診断するのは困難です。メールを送信するときは、次のようにしてください。コンソールをチェックして、エラーが何であるかを確認できます。

transporter.sendMail({
  from: contact.email,
  to: to,
  subject: contact.subject,
  text: contact.message
}, function(error, info){
    if(error){
        console.log(error);
    }else{
        console.log('Message sent: ' + info.response);
    }
});
0
cheniel