概要
やりたいことは、作成中のAmisの上位側にあたるサーバーの作成(AmisServerとする)
1.Amisからの通知を受けてGmailでメール送信を行う
2.Gmailでメール受信してAmisに通知する
この試作を作ってみた。
構成
巨人の肩に乗らせて頂き、node.jsを使ってみた。
inboxというライブラリが優秀なので、受信ほぼこのまま。
送信は別のライブラリがいるらしいが、ssmtpで送信する方法を試していたので、
今回はこっちを使用した。
必要なライブラリnpm install inbox
npm install iconv
npm install express
iconvが無いと文字化けする
expressはwebサーバーを簡易につくるために使用している
備考
エンコードがメールによって異なっていたりして、判別が微妙
yahooメールは iso-2022-jp?
gmailはbase64やquoted-printable??
送信はsendmail.shを呼び出しているだけ
実態はssmtpを呼び出しているだけ
来週にでもちゃんと作ろうOrz
参考
inbox使用方法
https://github.com/pipedrive/inbox
http://www.jonki.net/entry/2014/05/11/022046
http://liginc.co.jp/web/service/facebook/153850
VSCODEを使用したnode.jsのデバッグ方法
http://www.atmarkit.co.jp/ait/articles/1508/27/news020_2.html
以下ソースコード
app.js
+ext-program.js
+gmail-imap.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//app.js | |
//Amis Server V0.1 | |
var gi = require('./gmail-imap.js'); | |
var xp = require('./ext-program.js'); | |
ws.run(3000,null); | |
gi.run("username@gmail.com","password", | |
function (result) { | |
console.log("imap rcv callback"); | |
console.log("subject:"+result["subject"]); | |
if(result["subject"].toLowerCase().indexOf("[amis]") == 0){ | |
console.log("Amis宛のメールだ!"); | |
xp.runSendMail(result["sender"],"Amis受信通知","メールを受信しましたよ!",null); | |
} | |
} | |
); | |
//adress,subject,body,callback_func | |
xp.runSendMail("root@mailadress","AmisServer起動","動き始めましたよ",null); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var spawn = require("child_process").spawn; | |
exports.runSendMail = function runSendMail(adress,subject,body,callback_func) { | |
var buf=""; | |
var cmd = "./sendmail.sh "+adress+" "+body; | |
var child = shspawn(cmd); | |
child.on('close',function(code) { | |
console.log('exec fin: '); | |
if(callback_func != null){ | |
console.log("[runSendMail]コールバックあり"); | |
callback_func(); | |
} | |
}); | |
} | |
function shspawn(command) { | |
console.log(command); | |
return spawn('sh', ['-c', command]); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//gmail-imap.js | |
var inbox = require('inbox'); | |
var iconv = require('iconv'); | |
var ptn_string = "Content-Type: text/plain; charset="; | |
var ptn2_string = "Content-Type: text/html; charset="; | |
var ptn_encode_string = "Content-Transfer-Encoding: "; | |
exports.run = function run(username,password,callback_func) { | |
var client = inbox.createConnection(false, "imap.gmail.com", { | |
secureConnection: true, | |
auth:{ | |
user: username, | |
pass: password | |
} | |
}); | |
client.on("connect", function(){ | |
console.log("Successfully connected to server"); | |
client.openMailbox("INBOX", function(error, info){ | |
if(error) throw error; | |
console.log("Message count in INBOX: " + info.count); | |
}); | |
}); | |
client.on("new", function(message){ | |
console.log("----------------------------------------------------------"); | |
console.log("New incoming message " + message.title + "\t" + message.from.address + "\t" + message.date); | |
//client.createMessageStream(message.UID).pipe(process.stdout, {end: false}); | |
//Content-Type: text/plain; charset= | |
client.createMessageStream(message.UID).on("data", function(data){ | |
var body = getMailBody(data); | |
if(body.length > 0){ | |
var result = { subject: message.title, | |
sender: message.from.address, | |
date: message.date, | |
body: body} | |
if(callback_func != null){ | |
// console.log("[imap]新着あり"); | |
callback_func(result); | |
} | |
} | |
}); | |
}); | |
client.on('close', function (){ | |
console.log('DISCONNECTED!'); | |
}); | |
client.connect(); | |
}; | |
function getMailBody(body) { | |
var lines = body.toString().split(/\r\n|\r|\n/); | |
var charset = "",charset_old=""; | |
var encodeset = "",encodeset_old=""; | |
var conv = null; | |
var body_text = ""; | |
for(var i=0; i<lines.length;i++){ | |
var index = lines[i].indexOf(ptn_string); | |
if(index != -1){ | |
charset = lines[i].substr(index+ptn_string.length).trim(); | |
console.log("charset find (Content-Type: text/plain) = "+charset); | |
} | |
var index2 = lines[i].indexOf(ptn2_string); | |
if(index2 != -1){ | |
charset = lines[i].substr(index2+ptn2_string.length).trim(); | |
console.log("charset find (Content-Type: text/html) = "+charset); | |
charset=""; | |
console.log("->charset remove"); | |
} | |
//Content-Transfer-Encodingを発見したら | |
var index_encode = lines[i].indexOf(ptn_encode_string); | |
if(index_encode != -1){ | |
encodeset = lines[i].substr(index+ptn_encode_string.length).trim(); | |
console.log("encodeset find = "+encodeset); | |
} | |
if((index == -1) && (index2 == -1) && (index_encode == -1) && (charset != "")){ | |
if(charset != charset_old){ | |
conv = new iconv.Iconv(charset, "UTF-8"); | |
charset_old = charset; | |
} | |
if(encodeset != encodeset_old){ | |
console.log("-- encode change ---"); | |
encodeset_old = encodeset; | |
} | |
if(encodeset != ""){ | |
var retval =""; | |
try{ | |
retval = conv.convert(Buffer(lines[i], encodeset).toString()).toString(); | |
}catch(e){ | |
console.log(e); | |
} | |
body_text+=retval+"\n"; | |
}else{ | |
body_text+=conv.convert(lines[i]).toString()+"\n"; | |
} | |
} | |
} | |
if(body_text.length > 0){ | |
console.log("----- body start ------"); | |
console.log(body_text); | |
console.log("+++++++++++++++++++++++"); | |
} | |
return body_text; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
cat << END | /usr/sbin/ssmtp -v -t | |
To:$1 | |
Subject:$2 | |
From:"システム管理" <username@gmail.com> | |
$3 | |
END |