[Node.js/cloud functions] Send mail from yahoo mail account with nodemailer
Overview
In the smartphone app, I implemented a function that notifies me by e-mail when a certain type of data is posted using firebase's cloud functions. There were some stumbling points along the way, so I'll write them down.
Detail
Overview
Create a function to send an email with cloud functions. The trigger for executing the function is when the document is created in the "\ data" collection of firestore. The sender of the email is the yahoo Japan email account, and the destination of the email is the gmail account.
Implementation
I implemented it as follows.
const functions=require("firebase-functions");
const admin=require("firebase-admin");
admin.initializeApp(functions.config().firebase);
const nodemailer=require('nodemailer');
const smtpTransport=require("nodemailer-smtp-transport");
const email=functions.config().mailfrom.address;
const password=functions.config().mailfrom.password;
let transporter=nodemailer.createTransport(smtpTransport({
host: 'smtp.mail.yahoo.co.jp',
port: 465,
secure: true,
auth: {
user: email,
pass: password
}
}));
exports.sendOpinion=functions.firestore.document('data/{id}')
.onCreate((snapshot,context)=>{
const to=functions.config().mailto.address;
const data=snapshot.data();
const title=data.title;
const detail=data.detail;
const msg="タイトル: "+title+"\n詳細: "+detail;
const mailOptions={
from: email,
to: to,
subject: 'データが投稿されました',
text: msg
};
return transporter.sendMail(mailOptions, (erro,info)=>{
if(erro){
return res.send(erro.toString());
}
return res.send('Sended');
});
});
The configuration of firestore is as follows.
data
|- title
|- detail
In addition, the sender, destination email address, and password are set in the environment variables of firebase. (Don't write directly in the code)
Error and Solution
Too many bad auth attempts.
→ use nodemailer-smtp-transport
554 Cannot send message due to possible spam
→ There was no text.
Greeting never received
→ secure: true
Lastly
You can now notify by email when the data is posted.
In my case, I use the above when an opinion about the app is posted, but I think that there are other ways to use it, such as when a user is added.
Recent Posts
See AllPhenomenon I want to set up a local server using http-server and check the contents of a static html file. When I start http-server with...
Overview In many cases, you'll send a message with Firebase Messaging and take some action when the notification is tapped (eg to go to a...
Comentários