LoginSignup
13
20

More than 5 years have passed since last update.

node.jsでGmailメール受信をトリガーしてなにか処理する

Last updated at Posted at 2017-04-19

npm install inbox
npm install mailparser

mail_trigger_sample.js

'use strict';

var inbox = require('inbox');

//var nodemailer = require('nodemailer');
//var iconv = require('iconv');
//var conv = new iconv.Iconv("UTF-8", "UTF-8");

const simpleParser = require('mailparser').simpleParser;

var client = inbox.createConnection(false, 'imap.gmail.com', {
    secureConnection: true
    ,auth: {
        user:'yourmailaddress@gmail.com'
        ,pass:'password'
    }
});

client.on('connect', function() {
    console.log('connected');
    client.openMailbox('INBOX', function(error) {
        if (error) throw error;
    });
});

client.connect();

client.on("new", function(message) {
    console.log('日時:' + message.date);
    console.log('送信者:' + message.from.name + '-' + message.from.address);
    console.log('タイトル:' + message.title);

    var stream = client.createMessageStream(message.UID);
    simpleParser(stream)
        .then(mail=> {
            console.log('本文(HTMLテキスト):' + mail.textAsHtml);
        })
        .catch(err=> {
            console.log(err);
        });

});
13
20
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
20