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

Help & Support Update #124

Merged
merged 17 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
95 changes: 95 additions & 0 deletions backend/config/mailtrap/emailTemplate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
export const VERIFICATION_EMAIL_TEMPLATE = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Verify Your Email</title>
</head>
<body style="font-family: Arial, sans-serif; line-height: 1.6; color: #333; max-width: 600px; margin: 0 auto; padding: 20px; padding-top: 10px;">
<div style="background: linear-gradient(to right, #99DD05, #b8f724); padding: 20px; text-align: center;">
<h1 style="color: black; margin: 0;">Verify Your Email</h1>
</div>
<div style="background-color: #f9f9f9; padding: 20px; border-radius: 0 0 5px 5px; box-shadow: 0 2px 5px rgba(0,0,0,0.1);">
<p>Hello,</p>
<p>Thank you for signing up! Your verification code is:</p>
<div style="text-align: center; margin: 30px 0;">
<span style="font-size: 32px; font-weight: bold; letter-spacing: 5px; color: black;">{verificationCode}</span>
</div>
<p>Enter this code on the verification page to complete your registration.</p>
<p>This code will expire in 15 minutes for security reasons.</p>
<p>If you didn't create an account with us, please ignore this email.</p>
<p>Best regards,<br>Farmcart Team</p>
</div>
<div style="text-align: center; margin-top: 20px; color: #888; font-size: 0.8em;">
<p>This is an automated message, please do not reply to this email.</p>
</div>
</body>
</html>
`

export const PASSWORD_RESET_SUCCESS_TEMPLATE = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Password Reset Successful</title>
</head>
<body style="font-family: Arial, sans-serif; line-height: 1.6; color: #333; max-width: 600px; margin: 0 auto; padding: 20px;">
<div style="background: linear-gradient(to right, #4CAF50, #45a049); padding: 20px; text-align: center;">
<h1 style="color: white; margin: 0;">Password Reset Successful</h1>
</div>
<div style="background-color: #f9f9f9; padding: 20px; border-radius: 0 0 5px 5px; box-shadow: 0 2px 5px rgba(0,0,0,0.1);">
<p>Hello,</p>
<p>We're writing to confirm that your password has been successfully reset.</p>
<div style="text-align: center; margin: 30px 0;">
<div style="background-color: #4CAF50; color: white; width: 50px; height: 50px; line-height: 50px; border-radius: 50%; display: inline-block; font-size: 30px;">
</div>
</div>
<p>If you did not initiate this password reset, please contact our support team immediately.</p>
<p>For security reasons, we recommend that you:</p>
<ul>
<li>Use a strong, unique password</li>
<li>Enable two-factor authentication if available</li>
<li>Avoid using the same password across multiple sites</li>
</ul>
<p>Thank you for helping us keep your account secure.</p>
<p>Best regards,<br>Your App Team</p>
</div>
<div style="text-align: center; margin-top: 20px; color: #888; font-size: 0.8em;">
<p>This is an automated message, please do not reply to this email.</p>
</div>
</body>
</html>
`

export const PASSWORD_RESET_REQUEST_TEMPLATE = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Reset Your Password</title>
</head>
<body style="font-family: Arial, sans-serif; line-height: 1.6; color: #333; max-width: 600px; margin: 0 auto; padding: 20px;">
<div style="background: linear-gradient(to right, #4CAF50, #45a049); padding: 20px; text-align: center;">
<h1 style="color: white; margin: 0;">Password Reset</h1>
</div>
<div style="background-color: #f9f9f9; padding: 20px; border-radius: 0 0 5px 5px; box-shadow: 0 2px 5px rgba(0,0,0,0.1);">
<p>Hello,</p>
<p>We received a request to reset your password. If you didn't make this request, please ignore this email.</p>
<p>To reset your password, click the button below:</p>
<div style="text-align: center; margin: 30px 0;">
<a href="{resetURL}" style="background-color: #4CAF50; color: white; padding: 12px 20px; text-decoration: none; border-radius: 5px; font-weight: bold;">Reset Password</a>
</div>
<p>This link will expire in 1 hour for security reasons.</p>
<p>Best regards,<br>Your App Team</p>
</div>
<div style="text-align: center; margin-top: 20px; color: #888; font-size: 0.8em;">
<p>This is an automated message, please do not reply to this email.</p>
</div>
</body>
</html>
`
88 changes: 88 additions & 0 deletions backend/config/mailtrap/emails.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import {
PASSWORD_RESET_REQUEST_TEMPLATE,
VERIFICATION_EMAIL_TEMPLATE,
PASSWORD_RESET_SUCCESS_TEMPLATE,
} from './emailTemplate.js'
import { mailtrapClient, sender } from './mailtrap.config.js'

export const sendVerificationEmail = async (email, verificationCode) => {
const recipient = [{ email }]

try {
const response = await mailtrapClient.send({
from: sender,
to: recipient,
subject: 'Verify your email',
html: VERIFICATION_EMAIL_TEMPLATE.replace(
'{verificationCode}',
verificationCode
),
category: 'Email verification',
})

console.log('Email sent successfully', response)
} catch (error) {
console.log(`Error sending verification email:${error}`)
throw new Error(`Error sending verification email:${error}`)
}
}

export const sendWelcomeEmail = async (email, firstName) => {
const recipient = [{ email }]
try {
const response = await mailtrapClient.send({
from: sender,
to: recipient,
template_uuid: '36c30838-bef7-4212-9dbb-b25c80989599',
template_variables: {
company_info_name: 'Farmcart PVT LTD',
name: firstName,
},
})

console.log('Welcome Email sent successfully', response)
} catch (error) {
console.log(`Error sending Welcome email:${error}`)
throw new Error(`Error sending Welcome email:${error}`)
}
}

export const sendPasswordResetEmail = async (email, resetURL) => {
const recipient = [{ email }]
try {
const response = await mailtrapClient.send({
from: sender,
to: recipient,
subject: 'Password Reset Request',
html: PASSWORD_RESET_REQUEST_TEMPLATE.replace(
'{resetURL}',
resetURL
),
category: 'Password reset',
})

console.log('Password reset email sent successfully', response)
} catch (error) {
console.log(`Error sending password reset email: ${error}`)
throw new Error(`Error sending password reset email: ${error}`)
}
}

// Send success password reset email
export const sendSuccessResetPasswordEmail = async (email) => {
const recipient = [{ email }]
try {
const response = await mailtrapClient.send({
from: sender,
to: recipient,
subject: 'Password Reset Successful',
html: PASSWORD_RESET_SUCCESS_TEMPLATE,
category: 'Password reset success',
})

console.log('Password reset success email sent successfully', response)
} catch (error) {
console.log(`Error sending password reset success email: ${error}`)
throw new Error(`Error sending password reset success email: ${error}`)
}
}
13 changes: 13 additions & 0 deletions backend/config/mailtrap/mailtrap.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { MailtrapClient } from 'mailtrap'
import dotenv from 'dotenv'

dotenv.config()

export const mailtrapClient = new MailtrapClient({
token: process.env.MAILTRAP_TOKEN,
})

export const sender = {
email: 'hello@demomailtrap.com',
name: 'Farmcart Team',
}
Loading
Loading