chore: Enable automatic generation of email server in non production environments (#1731)
This commit is contained in:
parent
e34ba1457e
commit
25d5ad8a7e
|
@ -18,7 +18,7 @@ PORT=3000
|
||||||
FORCE_HTTPS=true
|
FORCE_HTTPS=true
|
||||||
|
|
||||||
ENABLE_UPDATES=true
|
ENABLE_UPDATES=true
|
||||||
DEBUG=cache,presenters,events
|
DEBUG=cache,presenters,events,emails,mailer,utils,multiplayer,server,services
|
||||||
|
|
||||||
# Third party signin credentials (at least one is required)
|
# Third party signin credentials (at least one is required)
|
||||||
SLACK_KEY=get_a_key_from_slack
|
SLACK_KEY=get_a_key_from_slack
|
||||||
|
|
|
@ -26,6 +26,8 @@ import { baseStyles } from "./emails/components/EmailLayout";
|
||||||
import { createQueue } from "./utils/queue";
|
import { createQueue } from "./utils/queue";
|
||||||
|
|
||||||
const log = debug("emails");
|
const log = debug("emails");
|
||||||
|
const useTestEmailService =
|
||||||
|
process.env.NODE_ENV !== "production" && !process.env.SMTP_USERNAME;
|
||||||
|
|
||||||
type Emails = "welcome" | "export";
|
type Emails = "welcome" | "export";
|
||||||
|
|
||||||
|
@ -73,7 +75,7 @@ export class Mailer {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
log(`Sending email "${data.title}" to ${data.to}`);
|
log(`Sending email "${data.title}" to ${data.to}`);
|
||||||
await transporter.sendMail({
|
const info = await transporter.sendMail({
|
||||||
from: process.env.SMTP_FROM_EMAIL,
|
from: process.env.SMTP_FROM_EMAIL,
|
||||||
replyTo: process.env.SMTP_REPLY_EMAIL || process.env.SMTP_FROM_EMAIL,
|
replyTo: process.env.SMTP_REPLY_EMAIL || process.env.SMTP_FROM_EMAIL,
|
||||||
to: data.to,
|
to: data.to,
|
||||||
|
@ -82,6 +84,10 @@ export class Mailer {
|
||||||
text: data.text,
|
text: data.text,
|
||||||
attachments: data.attachments,
|
attachments: data.attachments,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (useTestEmailService) {
|
||||||
|
log("Email Preview URL: %s", nodemailer.getTestMessageUrl(info));
|
||||||
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
if (process.env.SENTRY_DSN) {
|
if (process.env.SENTRY_DSN) {
|
||||||
Sentry.captureException(err);
|
Sentry.captureException(err);
|
||||||
|
@ -159,6 +165,10 @@ export class Mailer {
|
||||||
};
|
};
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
|
this.loadTransport();
|
||||||
|
}
|
||||||
|
|
||||||
|
async loadTransport() {
|
||||||
if (process.env.SMTP_HOST) {
|
if (process.env.SMTP_HOST) {
|
||||||
let smtpConfig = {
|
let smtpConfig = {
|
||||||
host: process.env.SMTP_HOST,
|
host: process.env.SMTP_HOST,
|
||||||
|
@ -174,6 +184,24 @@ export class Mailer {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.transporter = nodemailer.createTransport(smtpConfig);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (useTestEmailService) {
|
||||||
|
log("SMTP_USERNAME not provided, generating test account…");
|
||||||
|
let testAccount = await nodemailer.createTestAccount();
|
||||||
|
|
||||||
|
const smtpConfig = {
|
||||||
|
host: "smtp.ethereal.email",
|
||||||
|
port: 587,
|
||||||
|
secure: false,
|
||||||
|
auth: {
|
||||||
|
user: testAccount.user,
|
||||||
|
pass: testAccount.pass,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
this.transporter = nodemailer.createTransport(smtpConfig);
|
this.transporter = nodemailer.createTransport(smtpConfig);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Reference in New Issue