14
17

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Gmailの新着メールをNode.jsを使って受信する

Last updated at Posted at 2016-02-02
"use strict";

const inbox = require('inbox');
const nodemailer = require('nodemailer');
const iconv = require('iconv');
const conv = new iconv.Iconv("UTF-8", "UTF-8");
const MailParser = require("mailparser").MailParser;
const mailparser = new MailParser();

const auth = {user: 'xxxx', pass: 'xxxx'};

var imap = inbox.createConnection(false, 'imap.gmail.com', {secureConnection: true, auth});

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

imap.connect();

imap.on("new", function(message) { 
  	console.log('日時:' + message.date);
  	console.log('送信者:' + message.from.name + '-' + message.from.address);
  	console.log('タイトル:' + message.title);
  	
  	imap
  	.createMessageStream(message.UID)
	.pipe(mailparser)
  	.on("end", function(mail) {
  		console.log(mail);
	});
 
});
 
imap.on('new', function(message) {
/*
	var smtp = nodemailer.createTransport('SMTP', {
		port: 465,
		host: 'smtp.gmail.com',
		secureConnection: true,
		auth
	});
	smtp.sendMail({
		from: 'ぁゃぴぼっと<' + auth.user + '>',
		to: message.from.address,
		subject: "ぁりがと",
		text: "登録したょ"
	}, function(e, res) {
		console.log(e ? e.message : res.message);
		smtp.close();
	});
*/
});
14
17
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
14
17

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?