ローカル環境(localhost:3000)でホームページの問い合わせフォームをnode.jsのnodemailerでGmail送信させようとしています。Google Developers Console でリフレッシュトークンも取得して、コードを書きました。メールアドレスやパスワードも問題なく(それでウェブ上ではログイン可能)、リフレッシュトークンもコピペしているので間違いようがなく、しかしプログラムは上手く作動しません。ターミナルには以下のエラーがでます。
Error: Mail command failed: 530-5.7.0 Authentication Required. Learn more at
530 5.7.0 https://support.google.com/mail/?p=WantAuthError lt17sm9492993pjb.41 - gsmtp
該当部のコードは以下の通り。
node.js
1const transporter = nodemailer.createTransport({ 2 host: "smtp.gmail.com", 3 port:465, 4 secure: true, 5 auth:{ 6 type: 'OAuth2', 7 user: process.env.EMAIL_ADDRESS, 8 pass: process.env.EMAIL_PASSWORD, 9 clientId: process.env.CLIENT_ID, 10 clientSecret: process.env.CLIENT_SECRET, 11 refreshToken: process.env.REFRESH_TOKEN, 12 accessToken: process.env.ACCESS_TOKEN 13 } 14}); 15 16app.post('/contact', (req, res, next) => { 17 const username = req.body.username; 18 const useremail = req.body.useremail; 19 const usermessege = req.body.usermessege; 20 const errors = []; 21 22 if (username === '') { 23 errors.push('お名前がありません!'); 24 } 25 if (useremail === '') { 26 errors.push('メールアドレスがありません!'); 27 } 28 if (usermessege === '') { 29 errors.push('メッセージがありません!'); 30 } 31 if (errors.length > 0) { 32 res.render('contact.ejs', { errors: errors }); 33 } else { 34 next(); 35 } 36}, 37 (req, res) => { 38 const username = req.body.username; 39 const useremail = req.body.useremail; 40 const usermessege = req.body.usermessege; 41 const mailOptions = { 42 from: process.env.EMAIL_ADDRESS, 43 to: process.env.EMAIL_ADDRESS2, 44 subject: '問い合わせ', 45 text: req.body.username + req.body.useremail + req.body.usermessege 46 }; 47 48 transporter.sendMail(mailOptions, function (error, info) { 49 if (error) { 50 console.log(error); 51 } else { 52 console.log(Email_sent); 53 res.redirect('/aftercontact'); 54 } 55 }); 56 57 });
html(ejs)
1<form action="/contact" method="post"> 2 <p>お名前</p> 3 <input type="text" name="username"> 4 <p>メールアドレス</p> 5 <input type="email" name="useremail"> 6 <p>ご質問ご意見</p> 7 <textarea type="text" name="usermessege"></textarea> 8 <% if (errors.length>0) { %> 9 <ul class="errors"> 10 <% errors.forEach(error=>{ %> 11 <li> <%= error %> </li> 12 <% }); %> 13 </ul> 14 <% } %> 15 <p>※全ての項目を必ず間違いなくご記入ください。</p> 16 <button type="submit" class="go">送信</button> 17</form>
.envファイルに記載されている情報には問題がないとすれば、いったい何が原因なのでしょうか? エラーメッセージに表記されているグーグルーのサポートに書いてあることは一通り試しましたが無理でした。
ご指導ご鞭撻のほどよろしくお願いいたします。
回答1件
あなたの回答
tips
プレビュー