mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-01-06 22:40:14 -05:00
Refactored GhostMailer's send to current code standards
no issue - While working on https://github.com/TryGhost/Team/issues/726 have questioned some of the options that were passed along to the `send` method. Documented findings and refactored the code slightly while touching it
This commit is contained in:
parent
3ca7b74987
commit
a1556797b6
1 changed files with 27 additions and 9 deletions
|
@ -33,6 +33,12 @@ function getFromAddress(requestedFromAddress) {
|
||||||
return address;
|
return address;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Decorates incoming message object wit h nodemailer compatible fields.
|
||||||
|
* For nodemailer 0.7.1 reference see - https://github.com/nodemailer/nodemailer/tree/da2f1d278f91b4262e940c0b37638e7027184b1d#e-mail-message-fields
|
||||||
|
* @param {Object} message
|
||||||
|
* @returns {Object}
|
||||||
|
*/
|
||||||
function createMessage(message) {
|
function createMessage(message) {
|
||||||
const encoding = 'base64';
|
const encoding = 'base64';
|
||||||
const generateTextFromHTML = !message.forceTextContent;
|
const generateTextFromHTML = !message.forceTextContent;
|
||||||
|
@ -70,22 +76,34 @@ module.exports = class GhostMailer {
|
||||||
this.transport = nodemailer.createTransport(transport, options);
|
this.transport = nodemailer.createTransport(transport, options);
|
||||||
}
|
}
|
||||||
|
|
||||||
send(message) {
|
/**
|
||||||
|
*
|
||||||
|
* @param {Object} message
|
||||||
|
* @param {string} message.subject - email subject
|
||||||
|
* @param {string} message.html - email content
|
||||||
|
* @param {string} message.to - email recipient address
|
||||||
|
* @param {boolean} [message.forceTextContent] - maps to generateTextFromHTML nodemailer option
|
||||||
|
* which is: "if set to true uses HTML to generate plain text body part from the HTML if the text is not defined"
|
||||||
|
* (ref: https://github.com/nodemailer/nodemailer/tree/da2f1d278f91b4262e940c0b37638e7027184b1d#e-mail-message-fields)
|
||||||
|
* @returns {Promise}
|
||||||
|
*/
|
||||||
|
async send(message) {
|
||||||
if (!(message && message.subject && message.html && message.to)) {
|
if (!(message && message.subject && message.html && message.to)) {
|
||||||
return Promise.reject(createMailError({
|
throw createMailError({
|
||||||
message: i18n.t('errors.mail.incompleteMessageData.error'),
|
message: i18n.t('errors.mail.incompleteMessageData.error'),
|
||||||
ignoreDefaultMessage: true
|
ignoreDefaultMessage: true
|
||||||
}));
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
const messageToSend = createMessage(message);
|
const messageToSend = createMessage(message);
|
||||||
|
|
||||||
return this.sendMail(messageToSend).then((response) => {
|
const response = await this.sendMail(messageToSend);
|
||||||
|
|
||||||
if (this.transport.transportType === 'DIRECT') {
|
if (this.transport.transportType === 'DIRECT') {
|
||||||
return this.handleDirectTransportResponse(response);
|
return this.handleDirectTransportResponse(response);
|
||||||
}
|
}
|
||||||
|
|
||||||
return response;
|
return response;
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
sendMail(message) {
|
sendMail(message) {
|
||||||
|
|
Loading…
Reference in a new issue