mirror of
https://github.com/Foundry376/Mailspring.git
synced 2025-09-06 20:54:26 +08:00
Add send endpoint
This commit is contained in:
parent
b58c11605c
commit
6ce54c2a34
4 changed files with 70 additions and 2 deletions
|
@ -15,6 +15,7 @@
|
|||
"hapi-swagger": "6.1.0",
|
||||
"inert": "4.0.0",
|
||||
"joi": "8.4.2",
|
||||
"nodemailer": "2.5.0",
|
||||
"nylas-core": "0.x.x",
|
||||
"nylas-sync": "0.x.x",
|
||||
"nylas-metrics": "0.x.x",
|
||||
|
|
|
@ -24,10 +24,9 @@ function inflateTransactions(db, transactionModels = []) {
|
|||
return ModelKlass.findAll({where: {id: ids}, include: includes})
|
||||
.then((models = []) => {
|
||||
for (const model of models) {
|
||||
model.dataValues.object = object
|
||||
const tsForId = byObjectIds[model.id];
|
||||
if (!tsForId || tsForId.length === 0) { continue; }
|
||||
for (const t of tsForId) { t.attributes = model.dataValues; }
|
||||
for (const t of tsForId) { t.attributes = model.toJSON(); }
|
||||
}
|
||||
})
|
||||
})).then(() => transactions.map(JSON.stringify).join("\n"))
|
||||
|
|
53
packages/nylas-api/routes/send.js
Normal file
53
packages/nylas-api/routes/send.js
Normal file
|
@ -0,0 +1,53 @@
|
|||
const Joi = require('joi');
|
||||
const nodemailer = require('nodemailer');
|
||||
const {DatabaseConnector} = require('nylas-core');
|
||||
|
||||
function toParticipant(payload) {
|
||||
return payload.map((p) => `${p.name} <${p.email}>`).join(',')
|
||||
}
|
||||
|
||||
module.exports = (server) => {
|
||||
server.route({
|
||||
method: 'POST',
|
||||
path: '/send',
|
||||
config: {
|
||||
validate: {
|
||||
payload: {
|
||||
subject: Joi.string(),
|
||||
reply_to_message_id: Joi.number().integer(),
|
||||
from: Joi.array(),
|
||||
reply_to: Joi.array(),
|
||||
to: Joi.array(),
|
||||
cc: Joi.array(),
|
||||
bcc: Joi.array(),
|
||||
body: Joi.string(),
|
||||
file_ids: Joi.array(),
|
||||
},
|
||||
},
|
||||
},
|
||||
handler: (request, reply) => { DatabaseConnector.forShared().then((db) => {
|
||||
const accountId = request.auth.credentials.id;
|
||||
db.Account.findById(accountId).then((account) => {
|
||||
const sender = nodemailer.createTransport(account.smtpConfig());
|
||||
const data = request.payload;
|
||||
|
||||
const msg = {}
|
||||
for (key of ['from', 'to', 'cc', 'bcc']) {
|
||||
if (data[key]) msg[key] = toParticipant(data[key])
|
||||
}
|
||||
msg.subject = data.subject,
|
||||
msg.html = data.body,
|
||||
|
||||
console.log("------------------------------------------------")
|
||||
console.log(msg)
|
||||
sender.sendMail(msg, (error, info) => {
|
||||
console.log("DONE ===========================================");
|
||||
console.log(error)
|
||||
console.log(info)
|
||||
if (error) { reply(error).code(400) }
|
||||
else { reply(info.response) }
|
||||
});
|
||||
})
|
||||
})},
|
||||
});
|
||||
};
|
|
@ -67,6 +67,21 @@ module.exports = (sequelize, Sequelize) => {
|
|||
return null;
|
||||
}
|
||||
},
|
||||
|
||||
smtpConfig: function smtpConfig() {
|
||||
if (this.provider !== "imap") {
|
||||
throw new Error("Non IMAP not yet supported")
|
||||
}
|
||||
const {smtp_username, smtp_password} = this.decryptedCredentials();
|
||||
const {smtp_host, smtp_port, ssl_required} = this.connectionSettings;
|
||||
|
||||
return {
|
||||
port: smtp_port, host: smtp_host, secure: ssl_required,
|
||||
auth: { user: smtp_username, pass: smtp_password, },
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
},
|
||||
});
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue