top of page

[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 All

Let's do our best with our partner:​ ChatReminder

iphone6.5p2.png

It is an application that achieves goals in a chat format with partners.

google-play-badge.png
Download_on_the_App_Store_Badge_JP_RGB_blk_100317.png

Let's do our best with our partner:​ ChatReminder

納品:iPhone6.5①.png

It is an application that achieves goals in a chat format with partners.

google-play-badge.png
Download_on_the_App_Store_Badge_JP_RGB_blk_100317.png

Theme diary: Decide the theme and record for each genre

It is a diary application that allows you to post and record with themes and sub-themes for each genre.

google-play-badge.png
Download_on_the_App_Store_Badge_JP_RGB_blk_100317.png
bottom of page