Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Avoid using SMTP sever on development #51

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ const envVarsSchema = z
.string()
.transform((val) => Number(val))
.refine((val) => {
if (process.env.NODE_ENV === 'production') return !Number.isNaN(val);
return true;
return process.env.NODE_ENV !== 'production' || val;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you mind updating all other cases with this? Also, I think we can change this to implicit return

}, 'EMAIL PORT must be a number'),
APP_NAME: z.string(),
REDIS_HOST: z.string(),
Expand Down
31 changes: 17 additions & 14 deletions src/emails/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,30 @@ import previewEmail from 'preview-email';

import { config, isProduction } from 'config/config';

const emailTransporter = nodemailer.createTransport({
host: config.smtpHost,
port: config.smtpPort,
auth: {
user: config.smtpUser,
pass: config.smtpPassword,
},
});
const emailTransporter = isProduction
? nodemailer.createTransport({
host: config.smtpHost,
port: config.smtpPort,
auth: {
user: config.smtpUser,
pass: config.smtpPassword,
},
})
: null;

const sendEmail = async (
emailTo: string,
subject: string,
html: string,
): Promise<void> => {
if (isProduction)
await emailTransporter.sendMail({
from: config.emailFrom,
to: emailTo,
subject,
html,
});
await (emailTransporter &&
emailTransporter.sendMail({
from: config.emailFrom,
to: emailTo,
subject,
html,
}));
else
previewEmail({
from: config.emailFrom,
Expand Down