lib/mailer
Classes
Section titled “Classes”Logger
Section titled “Logger”Defined in: studiocms/packages/studiocms/src/lib/mailer/index.ts:73^
Extends
Section titled “Extends”any
Constructors
Section titled “Constructors”Constructor
Section titled “Constructor”new Logger(): Logger
Returns
Section titled “Returns”Inherited from
Section titled “Inherited from”Effect.Tag('studiocms/lib/mailer/Logger')< Logger, Effect.Effect.Success<typeof makeLogger>>().constructor
Properties
Section titled “Properties”static Layer: any;
Defined in: studiocms/packages/studiocms/src/lib/mailer/index.ts:78^
static Live: any = makeLogger;
Defined in: studiocms/packages/studiocms/src/lib/mailer/index.ts:77^
Mailer
Section titled “Mailer”Defined in: studiocms/packages/studiocms/src/lib/mailer/index.ts:81^
Extends
Section titled “Extends”any
Constructors
Section titled “Constructors”Constructor
Section titled “Constructor”new Mailer(): Mailer
Returns
Section titled “Returns”Inherited from
Section titled “Inherited from”Effect.Service<Mailer>()('studiocms/lib/mailer/Mailer', { effect: genLogger('studiocms/lib/mailer/Mailer.effect')(function () { const logger = yield Logger; const SMTP = yield SMTPMailer;
/ Logs the response from the mailer.
@param data - The response from the mailer. @returns The response from the mailer. / const mailerResponse = (data: MailerResponse) => { if ('error' in data) { logger.error(data.error); return data; } isVerbose && logger.info(data.message); return data; };
/ Gets the mailer configuration from the database.
@returns A promise that resolves with the mailer configuration object. / const getMailerConfigTable = pipeLogger('studiocms/lib/mailer/Mailer.getMailerConfigTable')( sdk.dbService.execute((db) => db.select().from(tsMailerConfig).where(eq(tsMailerConfig.id, CMSMailerConfigId)).get() ) );
/ Updates the mailer configuration in the database.
@param config - The new mailer configuration object. @returns A promise that resolves when the mailer configuration has been updated. / const updateMailerConfigTable = (config: tsMailerInsert) => genLogger('studiocms/lib/mailer/Mailer.updateMailerConfigTable')(function () { yield sdk.dbService .execute((db) => db.update(tsMailerConfig).set(config).where(eq(tsMailerConfig.id, CMSMailerConfigId)) ) .pipe( Effect.catchAll((e) => Effect.succeed( mailerResponse({ error: Error updating mailer configuration: ${String(e)} }) ) ) ); return mailerResponse({ message: 'Mailer configuration updated successfully' }); });
const createMailerConfigTable = (config: tsMailerInsert) => pipeLogger('studiocms/lib/mailer/Mailer.createMailerConfigTable')( sdk.dbService.execute((db) => db .insert(tsMailerConfig) .values({ ...config, id: CMSMailerConfigId }) .onConflictDoUpdate({ target: tsMailerConfig.id, set: config, where: eq(tsMailerConfig.id, CMSMailerConfigId), }) .returning() .get() ) );
/ Sends an email using the provided mailer configuration and mail options.
@param mailOptions - The options for the mail, including the subject and other message details. @returns A promise that resolves with the result of the mail sending operation. @throws Will throw an error if the mail sending operation fails.
@example typescript * const mailOptions = { * subject: 'Test Email', * to: 'recipient@example.com', * text: 'This is a test email.' * }; * * sendMail(mailerConfig, mailOptions) * .then(result => console.log('Email sent:', result)) * .catch(error => console.error('Error sending email:', error)); * / const sendMail = ({ subject, ...message }: MailOptions) => genLogger('studiocms/lib/mailer/Mailer.sendMail')(function () { // Create the mail options object const toSend: Mail.Options = { subject };
// Set the to field in the mail options toSend.to = Array.isArray(message.to) ? message.to.join(', ') : message.to;
// If the message has a text field and no html field, set the text field in the mail options if (message.text && !message.html) { toSend.text = message.text; }
// If the message has an html field, set the html field in the mail options if (message.html) { toSend.html = message.html; }
const result = yield SMTP.sendMail(toSend);
return mailerResponse({ message: Message sent: ${result.messageId} }); });
/ Verifies the mail connection using the provided transporter configuration.
@returns A promise that resolves to an object containing either a success message or an error message.
@example typescript * const result = await verifyMailConnection(); * if ('message' in result) { * console.log(result.message); * } else { * console.error(result.error); * } * / const verifyMailConnection = genLogger('studiocms/lib/mailer/Mailer.verifyMailConnection')( function () { const result = yield SMTP.verify();
// If the result is not true, log an error and return an error message if (result !== true) { return mailerResponse({ error: 'Mail connection verification failed' }); }
// Log a success message and return a success message return mailerResponse({ message: 'Mail connection verified successfully' }); } );
/ Checks if the mailer service is enabled in the StudioCMS configuration.
This function retrieves the configuration from the StudioCMS SDK and returns the value of the enableMailer property. If the configuration is not available, it defaults to false. / const isEnabled = genLogger('studiocms/lib/mailer/Mailer.isEnabled')(function () { const config = yield sdk.GET.siteConfig(); const status = config?.data?.enableMailer || false;
return status; });
return { getMailerConfigTable, updateMailerConfigTable, createMailerConfigTable, sendMail, verifyMailConnection, isEnabled, }; }), dependencies: [Logger.Layer, SMTPMailer.Default], accessors: true,}).constructor
Properties
Section titled “Properties”Provide
Section titled “Provide”static Provide: any;
Defined in: studiocms/packages/studiocms/src/lib/mailer/index.ts:248^
Interfaces
Section titled “Interfaces”MailerErrorResponse
Section titled “MailerErrorResponse”Defined in: studiocms/packages/studiocms/src/lib/mailer/index.ts:54^
Interface representing an error response from the mailer.
Properties
Section titled “Properties”error: string;
Defined in: studiocms/packages/studiocms/src/lib/mailer/index.ts:55^
MailerSuccessResponse
Section titled “MailerSuccessResponse”Defined in: studiocms/packages/studiocms/src/lib/mailer/index.ts:61^
Interface representing a success response from the mailer.
Properties
Section titled “Properties”message
Section titled “message”message: string;
Defined in: studiocms/packages/studiocms/src/lib/mailer/index.ts:62^
MailOptions
Section titled “MailOptions”Defined in: studiocms/packages/studiocms/src/lib/mailer/index.ts:29^
Interface representing the options for sending an email.
Properties
Section titled “Properties”optional html: string;
Defined in: studiocms/packages/studiocms/src/lib/mailer/index.ts:48^
The HTML content of the email. Optional.
subject
Section titled “subject”subject: string;
Defined in: studiocms/packages/studiocms/src/lib/mailer/index.ts:38^
The subject of the email.
optional text: string;
Defined in: studiocms/packages/studiocms/src/lib/mailer/index.ts:43^
The plain text content of the email. Optional.
to: string | string[];
Defined in: studiocms/packages/studiocms/src/lib/mailer/index.ts:33^
The recipient(s) of the email. Can be a single email address or an array of email addresses.
Type Aliases
Section titled “Type Aliases”MailerResponse
Section titled “MailerResponse”type MailerResponse = | MailerSuccessResponse | MailerErrorResponse;
Defined in: studiocms/packages/studiocms/src/lib/mailer/index.ts:68^
Interface representing the response from a mail verification operation.
tsMailer
Section titled “tsMailer”type tsMailer = typeof tsMailerConfig.$inferSelect;
Defined in: studiocms/packages/studiocms/src/lib/mailer/index.ts:19^
TypeSafe Table definition for use in StudioCMS Integrations
tsMailerInsert
Section titled “tsMailerInsert”type tsMailerInsert = Omit<typeof tsMailerConfig.$inferInsert, "id">;
Defined in: studiocms/packages/studiocms/src/lib/mailer/index.ts:24^
TypeSafe Table definition for use in StudioCMS Integrations
Variables
Section titled “Variables”makeLogger
Section titled “makeLogger”const makeLogger: any;
Defined in: studiocms/packages/studiocms/src/lib/mailer/index.ts:71^
tsMailerConfig
Section titled “tsMailerConfig”const tsMailerConfig: Table<"StudioCMSMailerConfig", { auth_pass: { type: "text"; }; auth_user: { type: "text"; }; default_sender: { type: "text"; }; host: { type: "text"; }; id: { type: "text"; }; port: { type: "number"; }; proxy: { type: "text"; }; secure: { type: "boolean"; }; tls_rejectUnauthorized: { type: "boolean"; }; tls_servername: { type: "text"; };}>;
Defined in: studiocms/packages/studiocms/src/lib/mailer/index.ts:14^
TypeSafe Table definition for use in StudioCMS Integrations