2023-05-30 00:38:38 +02:00
|
|
|
const nodemailer = require('nodemailer')
|
2023-07-09 18:39:15 +02:00
|
|
|
const Database = require('../Database')
|
2023-05-30 00:38:38 +02:00
|
|
|
const Logger = require("../Logger")
|
|
|
|
|
|
|
|
class EmailManager {
|
2023-07-05 01:14:44 +02:00
|
|
|
constructor() { }
|
2023-05-30 00:38:38 +02:00
|
|
|
|
|
|
|
getTransporter() {
|
2023-07-05 01:14:44 +02:00
|
|
|
return nodemailer.createTransport(Database.emailSettings.getTransportObject())
|
2023-05-30 00:38:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async sendTest(res) {
|
|
|
|
Logger.info(`[EmailManager] Sending test email`)
|
|
|
|
const transporter = this.getTransporter()
|
|
|
|
|
|
|
|
const success = await transporter.verify().catch((error) => {
|
|
|
|
Logger.error(`[EmailManager] Failed to verify SMTP connection config`, error)
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
|
|
|
|
if (!success) {
|
|
|
|
return res.status(400).send('Failed to verify SMTP connection configuration')
|
|
|
|
}
|
|
|
|
|
|
|
|
transporter.sendMail({
|
2023-07-05 01:14:44 +02:00
|
|
|
from: Database.emailSettings.fromAddress,
|
|
|
|
to: Database.emailSettings.testAddress || Database.emailSettings.fromAddress,
|
2023-05-30 00:38:38 +02:00
|
|
|
subject: 'Test email from Audiobookshelf',
|
|
|
|
text: 'Success!'
|
|
|
|
}).then((result) => {
|
|
|
|
Logger.info(`[EmailManager] Test email sent successfully`, result)
|
|
|
|
res.sendStatus(200)
|
|
|
|
}).catch((error) => {
|
|
|
|
Logger.error(`[EmailManager] Failed to send test email`, error)
|
|
|
|
res.status(400).send(error.message || 'Failed to send test email')
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
async sendEBookToDevice(ebookFile, device, res) {
|
|
|
|
Logger.info(`[EmailManager] Sending ebook "${ebookFile.metadata.filename}" to device "${device.name}"/"${device.email}"`)
|
|
|
|
const transporter = this.getTransporter()
|
|
|
|
|
|
|
|
const success = await transporter.verify().catch((error) => {
|
|
|
|
Logger.error(`[EmailManager] Failed to verify SMTP connection config`, error)
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
|
|
|
|
if (!success) {
|
|
|
|
return res.status(400).send('Failed to verify SMTP connection configuration')
|
|
|
|
}
|
|
|
|
|
|
|
|
transporter.sendMail({
|
2023-07-05 01:14:44 +02:00
|
|
|
from: Database.emailSettings.fromAddress,
|
2023-05-30 00:38:38 +02:00
|
|
|
to: device.email,
|
2023-06-12 06:53:51 +02:00
|
|
|
subject: "Here is your Ebook!",
|
2023-05-30 00:38:38 +02:00
|
|
|
html: '<div dir="auto"></div>',
|
|
|
|
attachments: [
|
|
|
|
{
|
|
|
|
filename: ebookFile.metadata.filename,
|
|
|
|
path: ebookFile.metadata.path,
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}).then((result) => {
|
|
|
|
Logger.info(`[EmailManager] Ebook sent to device successfully`, result)
|
|
|
|
res.sendStatus(200)
|
|
|
|
}).catch((error) => {
|
|
|
|
Logger.error(`[EmailManager] Failed to send ebook to device`, error)
|
|
|
|
res.status(400).send(error.message || 'Failed to send ebook to device')
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
module.exports = EmailManager
|