-
Notifications
You must be signed in to change notification settings - Fork 0
/
mailer.js
41 lines (35 loc) · 1.1 KB
/
mailer.js
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
const nodemailer = require('nodemailer');
const path = require('path');
const fs = require('fs');
require('dotenv').config(); // Load environment variables from .env file
// Configure your email transport
const transporter = nodemailer.createTransport({
service: 'Gmail', // You can use any email service
auth: {
user: process.env.EMAIL_USER, // Your email address
pass: process.env.EMAIL_PASS // Your email password
}
});
// Function to send email with an optional attachment
const sendEmail = (to, subject, text, filePath) => {
// Prepare email options
const mailOptions = {
from: 'sbentaarit@gmail.com',
to: to,
subject: subject,
text: text,
attachments: []
};
// Check if filePath is provided and add it as an attachment
if (filePath) {
const fileContent = fs.readFileSync(filePath);
mailOptions.attachments.push({
filename: path.basename(filePath),
content: fileContent,
contentType: 'image/jpeg' // Adjust the MIME type as necessary
});
}
// Send the email
return transporter.sendMail(mailOptions);
};
module.exports = sendEmail;