fix; updated the nodemailer
This commit is contained in:
1 parent
9166838edc
commit
676a486878
1 file changed
+56
-51
+56
-51
@@ -1,28 +1,33 @@
|
||||
'use server'
|
||||
|
||||
import { EmailContent, EmailProductInfo } from '@/types';
|
||||
import { EmailContent, EmailProductInfo, NotificationType } from '@/types';
|
||||
import nodemailer from 'nodemailer';
|
||||
|
||||
export const Notification = {
|
||||
WELCOME: 'WELCOME',
|
||||
CHANGE_OF_STOCK: 'CHANGE_OF_STOCK',
|
||||
LOWEST_PRICE: 'LOWEST_PRICE',
|
||||
THRESHOLD_MET: 'THRESHOLD_MET'
|
||||
WELCOME: 'WELCOME',
|
||||
CHANGE_OF_STOCK: 'CHANGE_OF_STOCK',
|
||||
LOWEST_PRICE: 'LOWEST_PRICE',
|
||||
THRESHOLD_MET: 'THRESHOLD_MET'
|
||||
}
|
||||
|
||||
export const generateEmailBody(product: EmailProductInfo, type: NotificationType) => {
|
||||
const THRESHOLD_PERCENTAGE = 40;
|
||||
export async function generateEmailBody(
|
||||
product: EmailProductInfo,
|
||||
type: NotificationType
|
||||
) {
|
||||
const THRESHOLD_PERCENTAGE = 40;
|
||||
// Shorten the product title
|
||||
const shortenedTitle =
|
||||
product.title.length > 20
|
||||
? `${product.title.substring(0, 20)}...`
|
||||
: product.title;
|
||||
|
||||
const shortenTitle = product.title.length > 20 ? `${product.title.substring(0, 20)}...` : product.title;
|
||||
let subject = "";
|
||||
let body = "";
|
||||
|
||||
let subject = ''
|
||||
|
||||
let body = ''
|
||||
|
||||
switch (type) {
|
||||
case Notification.WELCOME:
|
||||
subject = `Welcome to Price Tracking for ${shortenTitle}`;
|
||||
body = `
|
||||
switch (type) {
|
||||
case Notification.WELCOME:
|
||||
subject = `Welcome to Price Tracking for ${shortenedTitle}`;
|
||||
body = `
|
||||
<div>
|
||||
<h2>Welcome to PriceWise 🚀</h2>
|
||||
<p>You are now tracking ${product.title}.</p>
|
||||
@@ -36,67 +41,67 @@ export const generateEmailBody(product: EmailProductInfo, type: NotificationType
|
||||
<p>Stay tuned for more updates on ${product.title} and other products you're tracking.</p>
|
||||
</div>
|
||||
`;
|
||||
break;
|
||||
break;
|
||||
|
||||
case Notification.CHANGE_OF_STOCK:
|
||||
subject = `${shortenedTitle} is now back in stock!`;
|
||||
body = `
|
||||
case Notification.CHANGE_OF_STOCK:
|
||||
subject = `${shortenedTitle} is now back in stock!`;
|
||||
body = `
|
||||
<div>
|
||||
<h4>Hey, ${product.title} is now restocked! Grab yours before they run out again!</h4>
|
||||
<p>See the product <a href="${product.url}" target="_blank" rel="noopener noreferrer">here</a>.</p>
|
||||
</div>
|
||||
`;
|
||||
break;
|
||||
break;
|
||||
|
||||
case Notification.LOWEST_PRICE:
|
||||
subject = `Lowest Price Alert for ${shortenTitle}`;
|
||||
body = `
|
||||
case Notification.LOWEST_PRICE:
|
||||
subject = `Lowest Price Alert for ${shortenedTitle}`;
|
||||
body = `
|
||||
<div>
|
||||
<h4>Hey, ${product.title} has reached its lowest price ever!!</h4>
|
||||
<p>Grab the product <a href="${product.url}" target="_blank" rel="noopener noreferrer">here</a> now.</p>
|
||||
</div>
|
||||
`;
|
||||
break;
|
||||
break;
|
||||
|
||||
case Notification.THRESHOLD_MET:
|
||||
subject = `Discount Alert for ${shortenTitle}`;
|
||||
body = `
|
||||
case Notification.THRESHOLD_MET:
|
||||
subject = `Discount Alert for ${shortenedTitle}`;
|
||||
body = `
|
||||
<div>
|
||||
<h4>Hey, ${product.title} is now available at a discount more than ${THRESHOLD_PERCENTAGE}%!</h4>
|
||||
<p>Grab it right away from <a href="${product.url}" target="_blank" rel="noopener noreferrer">here</a>.</p>
|
||||
</div>
|
||||
`;
|
||||
break;
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new Error("Invalid notification type.");
|
||||
}
|
||||
default:
|
||||
throw new Error("Invalid notification type.");
|
||||
}
|
||||
|
||||
return { subject, body };
|
||||
return { subject, body };
|
||||
}
|
||||
|
||||
const transporter = nodemailer.createTransport({
|
||||
pool: true,
|
||||
service: 'hotmail',
|
||||
port: 2525,
|
||||
auth: {
|
||||
user: 'javascriptmastery@outlook.com',
|
||||
pass: process.env.EMAIL_PASSWORD,
|
||||
},
|
||||
maxConnections: 1
|
||||
pool: true,
|
||||
service: 'hotmail',
|
||||
port: 2525,
|
||||
auth: {
|
||||
user: 'javascriptmastery@outlook.com',
|
||||
pass: process.env.EMAIL_PASSWORD,
|
||||
},
|
||||
maxConnections: 1
|
||||
})
|
||||
|
||||
export const sendEmail = async (emailContent: EmailContent, sendTo: string[]) => {
|
||||
const mailOptions = {
|
||||
from: 'javascriptmastery@outlook.com',
|
||||
to: sendTo,
|
||||
html: emailContent.body,
|
||||
subject: emailContent.subject,
|
||||
}
|
||||
const mailOptions = {
|
||||
from: 'javascriptmastery@outlook.com',
|
||||
to: sendTo,
|
||||
html: emailContent.body,
|
||||
subject: emailContent.subject,
|
||||
}
|
||||
|
||||
transporter.sendMail(mailOptions, (error: any, info: any) => {
|
||||
if (error) return console.log(error);
|
||||
transporter.sendMail(mailOptions, (error: any, info: any) => {
|
||||
if (error) return console.log(error);
|
||||
|
||||
console.log('Email sent: ', info);
|
||||
})
|
||||
console.log('Email sent: ', info);
|
||||
})
|
||||
}
|
||||
Reference in new issue
Block a user