Use nodemailer
include attachments
This commit is contained in:
parent
abb7521454
commit
a8053bfc6d
54
index.js
54
index.js
@ -3,24 +3,48 @@ var Discord = require("discord.js");
|
||||
var MailInterface = require("./mailinterface");
|
||||
|
||||
var dc = new Discord.Client();
|
||||
dc.login(process.env.TOKEN);
|
||||
dc.login(process.env.TOKEN).then(() => { console.log("Logged in to Discord") });
|
||||
|
||||
var mint = new MailInterface({
|
||||
mbox: process.env.MBOX,
|
||||
smtp: {
|
||||
host: process.env.SMTP_HOST || "localhost",
|
||||
port: process.env.SMTP_PORT || 25,
|
||||
hostname: process.env.SMTP_HOSTNAME,
|
||||
from: process.env.SMTP_FROM
|
||||
nodemailer: {
|
||||
/*options: {
|
||||
host: process.env.SMTP_HOST || "localhost",
|
||||
port: process.env.SMTP_PORT || 25,
|
||||
name: process.env.HOSTNAME,
|
||||
from: process.env.MAIL_FROM,
|
||||
auth: {
|
||||
user: process.env.SMTP_USERNAME,
|
||||
pass: process.env.SMTP_PASSWORD
|
||||
}
|
||||
logger: true;
|
||||
},*/
|
||||
options: process.env.SMTP_URI,
|
||||
defaults: {
|
||||
from: process.env.MAIL_FROM,
|
||||
to: process.env.MAIL_TO
|
||||
}
|
||||
},
|
||||
target: process.env.TARGET_EMAIL
|
||||
});
|
||||
|
||||
|
||||
// discord to mms
|
||||
dc.on("message", async function(message){
|
||||
if (message.channel.id == process.env.DISCORD_CHANNEL_ID) {
|
||||
await mint.send(`${message.author.tag}: ${message.cleanContent}`); //TODO attachment support
|
||||
//await mint.send(`${message.author.tag}: ${message.cleanContent}`); //TODO attachment support
|
||||
try {
|
||||
await mint.send({
|
||||
subject: message.author.tag,
|
||||
body: message.cleanContent,
|
||||
attachments: message.attachments.map(attachment => ({
|
||||
filename: attachment.filename,
|
||||
href: attachment.url
|
||||
}))
|
||||
});
|
||||
} catch(error) {
|
||||
console.error(error);
|
||||
await message.react('⚠');
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
@ -33,8 +57,12 @@ mint.on("mail", async function(mail){
|
||||
var textAttachment = mail.attachments.find(a => a.filename == "text_0.txt");
|
||||
var msg = mail.text || textAttachment.content || mail.html || "<missing message>";
|
||||
if (mail.subject) msg = `**${mail.subject}**\n${msg}`;
|
||||
await dc.channels.get(process.env.DISCORD_CHANNEL_ID).send(msg, {
|
||||
split: {char:'', maxLength:2000},
|
||||
attachments: mail.attachments.filter(a => a.filename != "text_0.txt")
|
||||
});
|
||||
});
|
||||
try {
|
||||
await dc.channels.get(process.env.DISCORD_CHANNEL_ID).send(msg, {
|
||||
split: {char:'', maxLength:2000},
|
||||
attachments: mail.attachments.filter(a => a.filename != "text_0.txt")
|
||||
});
|
||||
} catch (error) {
|
||||
await mint.send({subject: "Error", body: error.message});
|
||||
}
|
||||
});
|
||||
|
@ -2,7 +2,7 @@ var {EventEmitter} = require("events");
|
||||
var fs = require("fs");
|
||||
var Mbox = require("node-mbox");
|
||||
var {simpleParser} = require("mailparser");
|
||||
var {SMTPClient} = require("smtp-client");
|
||||
var nodemailer = require("nodemailer");
|
||||
|
||||
class MailInterface extends EventEmitter {
|
||||
|
||||
@ -10,14 +10,14 @@ class MailInterface extends EventEmitter {
|
||||
super();
|
||||
Object.assign(this, opts);
|
||||
this.mbox = this.mbox || `/var/mail/${require('os').getUser().username}`;
|
||||
this.smtp.host = this.smtp.host || "localhost";
|
||||
this.smtp.port = this.smtp.port || 25;
|
||||
this.smtp.hostname = this.smtp.hostname || this.host;
|
||||
this.smtp.from = this.smtp.from || `${require('os').getUser().username}@${this.hostname}`;
|
||||
this.target = this.target;
|
||||
fs.watch(this.mbox, eventType => {
|
||||
if (eventType == "change") this.check();
|
||||
});
|
||||
this.transporter = nodemailer.createTransport(this.nodemailer.options, this.nodemailer.defaults);
|
||||
this.transporter.verify((error, success) => {
|
||||
error ? console.error(error) : console.log("SMTP connection verified", success);
|
||||
});
|
||||
this.send = this.transporter.sendMail;
|
||||
}
|
||||
|
||||
check() {
|
||||
@ -38,30 +38,32 @@ class MailInterface extends EventEmitter {
|
||||
});
|
||||
}
|
||||
|
||||
async send(body) {
|
||||
console.log("sending mail:", body);
|
||||
var smtpClient = new SMTPClient(this.smtp);
|
||||
await smtpClient.connect();
|
||||
await smtpClient.greet({hostname: this.smtp.hostname});
|
||||
await smtpClient.mail({from: this.smtp.from});
|
||||
await smtpClient.rcpt({to: this.target});
|
||||
var msg = '\r\n' + body.replace(/\n/g, '\r\n') + '\r\n';
|
||||
/*var msg = 'MIME-Version: 1.0\r\n' +
|
||||
'Content-Type: multipart/alternative; boundary="asdfhuiadfghviuarevhilu"\r\n' +
|
||||
"\r\n" +
|
||||
"--asdfhuiadfghviuarevhilu\r\n" +
|
||||
'Content-Type: text/plain; charset="UTF-8"\r\n' +
|
||||
'\r\n' + body.replace(/\n/g, '\r\n') + '\r\n' +
|
||||
'\r\n' +
|
||||
"--asdfhuiadfghviuarevhilu\r\n" +
|
||||
'Content-Type: text/html; charset="UTF-8"\r\n' +
|
||||
'\r\n<div dir="ltr">' + body.replace(/\n/g, '\r\n') + '</div>\r\n' +
|
||||
'\r\n' +
|
||||
'--asdfhuiadfghviuarevhilu--\r\n';*/
|
||||
await smtpClient.data(msg);
|
||||
await smtpClient.quit();
|
||||
console.log("finished sending mail");
|
||||
}
|
||||
//async send(body) {
|
||||
// var smtpClient = new SMTPClient(this.smtp);
|
||||
// await smtpClient.connect();
|
||||
// await smtpClient.greet({hostname: this.smtp.hostname});
|
||||
// await smtpClient.mail({from: this.smtp.from});
|
||||
// await smtpClient.rcpt({to: this.target});
|
||||
// var msg = '\r\n' + body.replace(/\n/g, '\r\n') + '\r\n';
|
||||
// /*var msg = 'MIME-Version: 1.0\r\n' +
|
||||
// 'Content-Type: multipart/alternative; boundary="asdfhuiadfghviuarevhilu"\r\n' +
|
||||
// "\r\n" +
|
||||
// "--asdfhuiadfghviuarevhilu\r\n" +
|
||||
// 'Content-Type: text/plain; charset="UTF-8"\r\n' +
|
||||
// '\r\n' + body.replace(/\n/g, '\r\n') + '\r\n' +
|
||||
// '\r\n' +
|
||||
// "--asdfhuiadfghviuarevhilu\r\n" +
|
||||
// 'Content-Type: text/html; charset="UTF-8"\r\n' +
|
||||
// '\r\n<div dir="ltr">' + body.replace(/\n/g, '\r\n') + '</div>\r\n' +
|
||||
// '\r\n' +
|
||||
// '--asdfhuiadfghviuarevhilu--\r\n';*/
|
||||
// await smtpClient.data(msg);
|
||||
// await smtpClient.quit();
|
||||
|
||||
//await transporter.sendMail({text});//WIP
|
||||
// how bout just use the method directly
|
||||
|
||||
//}
|
||||
|
||||
}
|
||||
|
||||
|
41
package-lock.json
generated
41
package-lock.json
generated
@ -149,11 +149,6 @@
|
||||
"resolved": "https://registry.npmjs.org/libqp/-/libqp-1.1.0.tgz",
|
||||
"integrity": "sha1-9ebgatdLeU+1tbZpiL9yjvHe2+g="
|
||||
},
|
||||
"line-buffer": {
|
||||
"version": "0.1.4",
|
||||
"resolved": "https://registry.npmjs.org/line-buffer/-/line-buffer-0.1.4.tgz",
|
||||
"integrity": "sha1-4H3hgzELYBIv3kszXPZgZUP/sdM="
|
||||
},
|
||||
"line-stream": {
|
||||
"version": "1.0.0",
|
||||
"resolved": "https://registry.npmjs.org/line-stream/-/line-stream-1.0.0.tgz",
|
||||
@ -201,6 +196,13 @@
|
||||
"mailsplit": "4.4.1",
|
||||
"nodemailer": "6.1.1",
|
||||
"tlds": "1.203.1"
|
||||
},
|
||||
"dependencies": {
|
||||
"nodemailer": {
|
||||
"version": "6.1.1",
|
||||
"resolved": "https://registry.npmjs.org/nodemailer/-/nodemailer-6.1.1.tgz",
|
||||
"integrity": "sha512-/x5MRIh56VyuuhLfcz+DL2SlBARpZpgQIf2A4Ao4hMb69MHSgDIMPwYmFwesGT1lkRDZ0eBSoym5+JoIZ3N+cQ=="
|
||||
}
|
||||
}
|
||||
},
|
||||
"mailsplit": {
|
||||
@ -229,9 +231,9 @@
|
||||
}
|
||||
},
|
||||
"nodemailer": {
|
||||
"version": "6.1.1",
|
||||
"resolved": "https://registry.npmjs.org/nodemailer/-/nodemailer-6.1.1.tgz",
|
||||
"integrity": "sha512-/x5MRIh56VyuuhLfcz+DL2SlBARpZpgQIf2A4Ao4hMb69MHSgDIMPwYmFwesGT1lkRDZ0eBSoym5+JoIZ3N+cQ=="
|
||||
"version": "6.4.2",
|
||||
"resolved": "https://registry.npmjs.org/nodemailer/-/nodemailer-6.4.2.tgz",
|
||||
"integrity": "sha512-g0n4nH1ONGvqYo1v72uSWvF/MRNnnq1LzmSzXb/6EPF3LFb51akOhgG3K2+aETAsJx90/Q5eFNTntu4vBCwyQQ=="
|
||||
},
|
||||
"prism-media": {
|
||||
"version": "0.0.3",
|
||||
@ -243,11 +245,6 @@
|
||||
"resolved": "https://registry.npmjs.org/process-nextick-args/-/process-nextick-args-2.0.1.tgz",
|
||||
"integrity": "sha512-3ouUOpQhtgrbOa17J7+uxOTpITYWaGP7/AhoR3+A+/1e9skrzelGi/dXzEYyvbxubEF6Wn2ypscTKiKJFFn1ag=="
|
||||
},
|
||||
"promised-timeout": {
|
||||
"version": "0.2.0",
|
||||
"resolved": "https://registry.npmjs.org/promised-timeout/-/promised-timeout-0.2.0.tgz",
|
||||
"integrity": "sha1-Rer89e2UV+LmDvp3wshg1u8dgW4="
|
||||
},
|
||||
"readable-stream": {
|
||||
"version": "3.4.0",
|
||||
"resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-3.4.0.tgz",
|
||||
@ -273,24 +270,6 @@
|
||||
"resolved": "https://registry.npmjs.org/safer-buffer/-/safer-buffer-2.1.2.tgz",
|
||||
"integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg=="
|
||||
},
|
||||
"smtp-channel": {
|
||||
"version": "0.2.2",
|
||||
"resolved": "https://registry.npmjs.org/smtp-channel/-/smtp-channel-0.2.2.tgz",
|
||||
"integrity": "sha1-W+sbYjmMUXP9fIWJ8P4LAX5bfuw=",
|
||||
"requires": {
|
||||
"line-buffer": "0.1.x",
|
||||
"promised-timeout": "0.2.x"
|
||||
}
|
||||
},
|
||||
"smtp-client": {
|
||||
"version": "0.3.1",
|
||||
"resolved": "https://registry.npmjs.org/smtp-client/-/smtp-client-0.3.1.tgz",
|
||||
"integrity": "sha1-PbR29bH4azc7ebnPeuOzBdyfH6Q=",
|
||||
"requires": {
|
||||
"promised-timeout": "0.2.x",
|
||||
"smtp-channel": "0.2.2"
|
||||
}
|
||||
},
|
||||
"snekfetch": {
|
||||
"version": "3.6.4",
|
||||
"resolved": "https://registry.npmjs.org/snekfetch/-/snekfetch-3.6.4.tgz",
|
||||
|
@ -5,6 +5,6 @@
|
||||
"mail": "^0.2.3",
|
||||
"mailparser": "^2.7.1",
|
||||
"node-mbox": "^1.0.0",
|
||||
"smtp-client": "^0.3.1"
|
||||
"nodemailer": "^6.4.2"
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user