Twilio で FAX 受信時にメールを送信するサンプル
ブログ記事
https://blog.mori-soft.com/entry/2018/11/09/204215
発表時の資料(2018/11/10 第19回伊勢IT交流会 with GCPUG Mie のLT資料)
Twilio で FAX 受信時にメールを送信するサンプル
ブログ記事
https://blog.mori-soft.com/entry/2018/11/09/204215
発表時の資料(2018/11/10 第19回伊勢IT交流会 with GCPUG Mie のLT資料)
exports.handler = function(context, event, callback) { | |
//console.log("event", event); | |
//console.log("context", context); | |
if ('received' != event.FaxStatus) { | |
console.log('no fax received'); | |
console.log('fax sid: ' + evemt.FaxSid); | |
console.log('status: ' + event.FaxStatus); | |
console.log('from: ' + event.From); | |
console.log('errCode: ' + event.ErrorCode); | |
console.log('errMessage: ' + event.ErrorMessage); | |
callback(null, 'no fax received'); | |
} | |
sendEmail(event.MediaUrl).then(function(info) { | |
console.log('success: ' + info); | |
callback(null, 'email success'); | |
}).catch(function(err){ | |
console.log('failed: ' + err); | |
callback(null, 'email failed'); | |
}); | |
function sendEmail(url) { | |
console.log("url: " + url); | |
var nodemailer = require('nodemailer'); | |
var transporter = nodemailer.createTransport({ | |
host: 'SMTP サーバー名', | |
port: 587, | |
secure: false, | |
auth: { | |
user: 'ユーザー名', | |
pass: 'パスワード' | |
}, | |
tls: { | |
rejectUnauthorized: false | |
} | |
}); | |
var message = { | |
from: '発信者のメールアドレス', | |
to: '宛先メールアドレス', | |
subject: 'Fax received from ' + event.From, | |
text: 'Fax received from ' + event.From, | |
attachments: [{ | |
filename: 'fax.pdf', | |
path: url | |
}] | |
}; | |
console.log("finish preparation to send mail"); | |
return new Promise(function(resolve, reject) { | |
transporter.sendMail(message, function(error, info) { | |
if (error) { | |
console.log('error: ' + error); | |
reject(error); | |
} else { | |
console.log('email sent, message id: ' + info.messageId); | |
resolve(info); | |
} | |
}); | |
}); | |
} | |
}; |