forked from Movalabs-crew/mova-store
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsendmail.js
More file actions
71 lines (63 loc) · 2.25 KB
/
Copy pathsendmail.js
File metadata and controls
71 lines (63 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import emailjs from "@emailjs/browser";
// Validate required environment variables
const EMAILJS_SERVICE_ID = process.env.NEXT_PUBLIC_EMAILJS_SERVICE_ID;
const EMAILJS_TEMPLATE_ID = process.env.NEXT_PUBLIC_EMAILJS_TEMPLATE_ID;
const EMAILJS_PUBLIC_KEY = process.env.NEXT_PUBLIC_EMAILJS_PUBLIC_KEY;
const DEFAULT_RECIPIENT_EMAIL = process.env.NEXT_PUBLIC_DEFAULT_RECIPIENT_EMAIL;
/**
* Validates that all required EmailJS environment variables are configured.
* @throws {Error} If any required environment variable is missing.
*/
const validateEmailConfig = () => {
const missingVars = [];
if (!EMAILJS_SERVICE_ID) missingVars.push("NEXT_PUBLIC_EMAILJS_SERVICE_ID");
if (!EMAILJS_TEMPLATE_ID) missingVars.push("NEXT_PUBLIC_EMAILJS_TEMPLATE_ID");
if (!EMAILJS_PUBLIC_KEY) missingVars.push("NEXT_PUBLIC_EMAILJS_PUBLIC_KEY");
if (missingVars.length > 0) {
throw new Error(
`Missing required EmailJS configuration. Please set the following environment variables: ${missingVars.join(", ")}. ` +
`See .env.local.example for reference.`
);
}
};
/**
* Sends an email using EmailJS service.
* @param {Object} options - Email options
* @param {string} options.name - Sender's name
* @param {string} options.email - Sender's email address
* @param {string} options.message - Email message body
* @param {string} [options.recipientEmail] - Recipient email (defaults to env var)
* @param {string} options.subject - Email subject line
* @returns {Promise<{status: number, text: string}>} EmailJS response
* @throws {Error} If configuration is missing or email fails to send
*/
const sendMail = async ({
name = "",
email = "",
message = "",
recipientEmail,
subject,
}) => {
// Validate configuration before sending
validateEmailConfig();
const templateParams = {
name: name,
email: email,
message: message,
recipient_email: recipientEmail || DEFAULT_RECIPIENT_EMAIL,
subject: subject,
};
try {
const response = await emailjs.send(
EMAILJS_SERVICE_ID,
EMAILJS_TEMPLATE_ID,
templateParams,
EMAILJS_PUBLIC_KEY
);
return { status: response.status, text: response.text };
} catch (error) {
console.error("Error sending email:", error);
throw error;
}
};
export default sendMail;