mirror of
https://github.com/Open-Dev-Society/OpenStock.git
synced 2026-09-03 06:15:16 +08:00
Merge pull request #85 from akoweicollinxx/fix-37-optional-email-config
fix: correct emoji encoding in warning messages
This commit is contained in:
@@ -1,36 +1,45 @@
|
||||
import nodemailer from 'nodemailer';
|
||||
import {WELCOME_EMAIL_TEMPLATE, NEWS_SUMMARY_EMAIL_TEMPLATE} from "@/lib/nodemailer/templates";
|
||||
import { WELCOME_EMAIL_TEMPLATE, NEWS_SUMMARY_EMAIL_TEMPLATE } from "@/lib/nodemailer/templates";
|
||||
|
||||
// Verify transporter configuration
|
||||
if (!process.env.NODEMAILER_EMAIL || !process.env.NODEMAILER_PASSWORD) {
|
||||
console.warn('⚠️ NODEMAILER_EMAIL or NODEMAILER_PASSWORD is not set. Email functionality will not work.');
|
||||
type EmailSendResult =
|
||||
| { status: 'skipped' }
|
||||
| { status: 'sent'; messageId: string };
|
||||
|
||||
const hasEmailConfig = Boolean(process.env.NODEMAILER_EMAIL && process.env.NODEMAILER_PASSWORD);
|
||||
|
||||
if (!hasEmailConfig) {
|
||||
console.warn('⚠️ Email credentials are not configured. Welcome and news summary emails are disabled until NODEMAILER_EMAIL and NODEMAILER_PASSWORD are set.');
|
||||
}
|
||||
|
||||
export const transporter = nodemailer.createTransport({
|
||||
service: 'gmail',
|
||||
auth: {
|
||||
user: process.env.NODEMAILER_EMAIL!,
|
||||
pass: process.env.NODEMAILER_PASSWORD!,
|
||||
},
|
||||
// Add connection timeout and retry options
|
||||
pool: true,
|
||||
maxConnections: 1,
|
||||
maxMessages: 3,
|
||||
})
|
||||
export const transporter = hasEmailConfig
|
||||
? nodemailer.createTransport({
|
||||
service: 'gmail',
|
||||
auth: {
|
||||
user: process.env.NODEMAILER_EMAIL!,
|
||||
pass: process.env.NODEMAILER_PASSWORD!,
|
||||
},
|
||||
// Keep the pool small because email volume is low in this app.
|
||||
pool: true,
|
||||
maxConnections: 1,
|
||||
maxMessages: 3,
|
||||
})
|
||||
: null;
|
||||
|
||||
// Verify connection on startup
|
||||
transporter.verify((error, success) => {
|
||||
if (error) {
|
||||
console.error('❌ Nodemailer transporter verification failed:', error);
|
||||
} else {
|
||||
console.log('✅ Nodemailer transporter is ready to send emails');
|
||||
}
|
||||
});
|
||||
if (transporter) {
|
||||
transporter.verify((error) => {
|
||||
if (error) {
|
||||
console.error('❌ Nodemailer transporter verification failed:', error);
|
||||
} else {
|
||||
console.log('✅ Nodemailer transporter is ready to send emails');
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
export const sendWelcomeEmail = async ({ email, name, intro }: WelcomeEmailData) => {
|
||||
try {
|
||||
if (!process.env.NODEMAILER_EMAIL || !process.env.NODEMAILER_PASSWORD) {
|
||||
throw new Error('Email credentials not configured');
|
||||
if (!transporter) {
|
||||
console.warn('⚠️ Welcome email skipped: email credentials are not configured.');
|
||||
return { status: 'skipped' } satisfies EmailSendResult;
|
||||
}
|
||||
|
||||
const htmlTemplate = WELCOME_EMAIL_TEMPLATE
|
||||
@@ -43,11 +52,11 @@ export const sendWelcomeEmail = async ({ email, name, intro }: WelcomeEmailData)
|
||||
subject: `Welcome to Openstock - your open-source stock market toolkit!`,
|
||||
text: 'Thanks for joining Openstock, an initiative by open dev society',
|
||||
html: htmlTemplate,
|
||||
}
|
||||
};
|
||||
|
||||
const info = await transporter.sendMail(mailOptions);
|
||||
console.log('✅ Welcome email sent successfully:', info.messageId);
|
||||
return info;
|
||||
return { status: 'sent', messageId: info.messageId } satisfies EmailSendResult;
|
||||
} catch (error) {
|
||||
console.error('❌ Failed to send welcome email:', error);
|
||||
throw error;
|
||||
@@ -58,8 +67,9 @@ export const sendNewsSummaryEmail = async (
|
||||
{ email, date, newsContent }: { email: string; date: string; newsContent: string }
|
||||
) => {
|
||||
try {
|
||||
if (!process.env.NODEMAILER_EMAIL || !process.env.NODEMAILER_PASSWORD) {
|
||||
throw new Error('Email credentials not configured');
|
||||
if (!transporter) {
|
||||
console.warn('⚠️ News summary email skipped: email credentials are not configured.');
|
||||
return { status: 'skipped' } satisfies EmailSendResult;
|
||||
}
|
||||
|
||||
const htmlTemplate = NEWS_SUMMARY_EMAIL_TEMPLATE
|
||||
@@ -76,9 +86,9 @@ export const sendNewsSummaryEmail = async (
|
||||
|
||||
const info = await transporter.sendMail(mailOptions);
|
||||
console.log('✅ News summary email sent successfully:', info.messageId);
|
||||
return info;
|
||||
return { status: 'sent', messageId: info.messageId } satisfies EmailSendResult;
|
||||
} catch (error) {
|
||||
console.error('❌ Failed to send news summary email:', error);
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user