dev: add mail module
Docker Image CI / build-and-push-image (push) Waiting to run
Maintain Release Merge PR / update-release-pr (push) Waiting to run
release-please / release-please (push) Waiting to run
test / test (18.x) (push) Waiting to run
test / test (20.x) (push) Waiting to run
test / test (22.x) (push) Waiting to run

This commit is contained in:
KernelDeimos
2025-02-03 10:08:39 -05:00
parent dc90999fee
commit e281dc92e3
2 changed files with 67 additions and 0 deletions
@@ -0,0 +1,14 @@
const { AdvancedBase } = require("@heyputer/putility");
class MailModule extends AdvancedBase {
async install (context) {
const services = context.get('services');
const { UserSendMailService } = require('./UserSendMailService');
services.registerService('user-send-mail', UserSendMailService);
}
}
module.exports = {
MailModule,
};
@@ -0,0 +1,53 @@
const BaseService = require("../../services/BaseService");
class UserSendMailService extends BaseService {
async ['__on_driver.register.interfaces'] () {
const svc_registry = this.services.get('registry');
const col_interfaces = svc_registry.get('interfaces');
col_interfaces.set('puter-send-mail', {
description: 'Send an email.',
methods: {
send: {
description: 'Recognize text in an image or document.',
parameters: {
to: {
type: 'string',
},
subject: {
type: 'string',
},
html: {
type: 'string',
},
},
result: { type: 'json' },
},
}
});
}
static IMPLEMENTS = {
'puter-send-mail': {
async send ({ to, subject, html }) {
const actor = this.context.get('actor');
const svc_email = this.services.get('email');
if ( ! actor.type.user ) {
throw new Error('Only users can send email.');
}
const user = actor.type.user;
const transporter = svc_email.get_transport_();
const o = {
from: `${user.username}@${this.config.domain}`, // sender address
to, subject, html,
};
await transporter.sendMail(o);
}
}
}
}
module.exports = {
UserSendMailService,
};