-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathsendEmail.js
More file actions
61 lines (53 loc) · 1.68 KB
/
sendEmail.js
File metadata and controls
61 lines (53 loc) · 1.68 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
// Sends email to hello@webdevpath.co when user submit the form in "Contact Us" page
import { Client } from 'node-mailjet';
import { encode } from 'html-entities';
const mailjet = new Client({
apiKey: process.env.MAILJET_API_KEY,
apiSecret: process.env.MAILJET_API_SECRET,
});
export default async (email, name, subject, message, subscribe) => {
// receiverEmail: The email will be sent here
const receiverEmail = 'hello@webdevpath.co';
// mailJetEmail: This is the email verified by mailjet
// the email will appear to be sent from this email
// If a non-verified email is used, it just fails silently
const mailjetEmail = 'support@webdevpath.co';
try {
const safeName = encode(name);
const safeEmail = encode(email);
const safeSubject = encode(subject);
const safeMessage = encode(message);
const data = {
Messages: [
{
From: {
Email: mailjetEmail,
name: 'Web Dev Path',
},
To: [
{
Email: receiverEmail,
},
],
Subject: `New message from ${safeName} via webdevpath.co 'Contact Us' Form`,
HTMLPart: `
<b>Name:</b> ${safeName} <br/>
<b>Email:</b> <a href='mailto:${safeEmail}'>${safeEmail}</a><br/><br/>
<u><b>Subject:</b> ${safeSubject}</u><br/>
<b>Message:</b> ${safeMessage} <br/>
<b>Subscribe?:</b> ${subscribe ? 'Yes' : 'No'}
`,
},
],
};
await mailjet.post('send', { version: 'v3.1' }).request(data);
return {
status: 'okay',
};
} catch (e) {
return {
status: 'error',
message: `Error: ${e.message}`,
};
}
};