digi-sendzi/lib/transporter.js

34 lines
1.1 KiB
JavaScript

import SMTPClient from './smtp.js';
import Email from './email.js';
class Transporter {
constructor(options) {
this.host = options.host;
this.port = options.port || 25;
this.auth = options.auth;
this.client = new SMTPClient(this.host, this.port, this.auth);
}
async sendMail(mailOptions) {
const { from, to, subject, text, html, attachments } = mailOptions;
// Basic validation
if (!from || !to || !subject) {
throw new Error('Missing required email fields: from, to, subject');
}
try {
await this.client.connect();
const email = new Email(from, to, subject, text, html, attachments);
await this.client.sendEmail(from, to, subject, email.format());
console.log('Email sent successfully!');
} catch (error) {
console.error('Error sending email:', error.message);
throw new Error(`Failed to send email: ${error.message}`); // Propagate error to API
}
}
}
export default Transporter;