lib/effects/smtp
Esta página aún no está disponible en tu idioma.
Classes
Section titled “Classes”SMTPError
Section titled “SMTPError”Defined in: studiocms/packages/studiocms/src/lib/effects/smtp.ts:51^
Represents an error specific to SMTP operations.
This class extends Data.TaggedError
to provide a tagged error type
with additional context about the error. The context includes an
error
property, which can be an instance of Error
or any other
unknown value.
Template
Section titled “Template”The type of the error context.
Extends
Section titled “Extends”any
Constructors
Section titled “Constructors”Constructor
Section titled “Constructor”new SMTPError(): SMTPError
Returns
Section titled “Returns”Inherited from
Section titled “Inherited from”Data.TaggedError('SMTPError')<{ error: Error | unknown }>.constructor
SMTPMailer
Section titled “SMTPMailer”Defined in: studiocms/packages/studiocms/src/lib/effects/smtp.ts:152^
SMTPMailer is a service that provides functionality for sending emails and verifying SMTP connections.
It uses the nodemailer
library to handle email transport and supports additional configurations
such as proxy settings for SOCKS proxies.
Methods
Section titled “Methods”sendMail(mailOptions: Mail.Options): Effect.Effect<SMTPTransport.SentMessageInfo, SMTPError, never>
Section titled “sendMail(mailOptions: Mail.Options): Effect.Effect<SMTPTransport.SentMessageInfo, SMTPError, never>”Sends an email using the provided mail options.
- Parameters:
mailOptions
: The options for the email to be sent, including recipients, subject, and content.
- Returns: An
Effect
that resolves with the sent message information (SMTPTransport.SentMessageInfo
) on success or fails with anSMTPError
on error.
This method uses an asynchronous effect to handle the email sending process. If an error occurs during
the sending process, it wraps the error in an SMTPError
and fails the effect. Otherwise, it succeeds
with the information about the sent message.
verify(): Effect.Effect<true, SMTPError, never>
Section titled “verify(): Effect.Effect<true, SMTPError, never>”Verifies the SMTP connection asynchronously.
- Returns: An
Effect
that resolves totrue
on success or fails with anSMTPError
on error.
This method wraps the verify
method of the nodemailer
transporter and converts its callback-based
implementation into an Effect. It resumes with a success or failure Effect based on the result of the verification.
Static Methods
Section titled “Static Methods”Live(opts: SMTPOptionsBase): Layer
Section titled “Live(opts: SMTPOptionsBase): Layer”Creates a live instance of the SMTPMailer
service with the provided SMTP options.
- Parameters:
opts
: The base options for configuring the SMTP connection.
- Returns: A
Layer
that provides theSMTPMailer
service.
This method uses the default configuration and provides the SMTPOptions
to the service.
Extends
Section titled “Extends”any
Constructors
Section titled “Constructors”Constructor
Section titled “Constructor”new SMTPMailer(): SMTPMailer
Returns
Section titled “Returns”Inherited from
Section titled “Inherited from”Effect.Service<SMTPMailer>()( 'studiocms/lib/effects/smtp/SMTPMailer', { effect: genLogger('studiocms/lib/effects/smtp/SMTPMailer.effect')(function () { const { transporter: __transporter, defaults } = yield buildTransporterConfig; const MailTransporter = yield pipeLogger( 'studiocms/lib/effects/smtp/SMTPMailer.transporter' )( Effect.try({ try: () => nodemailer.createTransport( __transporter as SMTPTransport.Options, defaults as SMTPTransport.Options ), catch: (error) => new SMTPError({ error }), }) );
// If the proxy is a socks proxy, set the socks module if (__transporter.proxy?.startsWith('socks')) { MailTransporter.set('proxy_socks_module', socks); MailTransporter.setupProxy(__transporter.proxy); }
const { options: _options, meta: _meta, dkim: _dkim, transporter: _transporter, MailMessage: _MailMessage, close: _close, isIdle: _isIdle, verify: _verify, use: _use, sendMail: _sendMail, getVersionString: _getVersionString, setupProxy: _setupProxy, } = MailTransporter;
const options = Effect.succeed(_options); const meta = Effect.succeed(_meta); const dkim = Effect.succeed(_dkim); const transporter = Effect.succeed(_transporter); / Usage: typeof transporter.MailMessage / const MailMessage = Effect.succeed(_MailMessage);
/ Closes all connections in the pool. If there is a message being sent, the connection is closed later / const close = pipeLogger('studiocms/lib/effects/smtp/SMTPMailer.close')( Effect.try({ try: () => _close(), catch: (error) => new SMTPError({ error }), }) );
/ Returns true if there are free slots in the queue / const isIdle = pipeLogger('studiocms/lib/effects/smtp/SMTPMailer.isIdle')( Effect.try({ try: () => _isIdle(), catch: (error) => new SMTPError({ error }), }) );
/ Verifies the SMTP connection asynchronously.
This function wraps the verify method and converts its callback-based implementation into an Effect. It resumes with a success or failure Effect based on the result of the verification.
@returns An Effect that resolves to true on success or fails with an SMTPError on error. / const verify = (): Effect.Effect<true, SMTPError, never> => pipeLogger('studiocms/lib/effects/smtp/SMTPMailer.verify')( Effect.async<true, SMTPError>((resume) => { _verify((error, success) => { if (error) { const toFail = new SMTPError({ error }); resume(errorTap(Effect.fail(toFail), toFail)); } else { resume(Effect.succeed(success)); } }); }) );
/ A utility function to safely apply a mail plugin function within an effect. It wraps the plugin execution in a try-catch block to handle errors gracefully.
@param step - A string representing the current step or context for the plugin. @param plugin - The mail plugin function to be executed. @returns An Effect that attempts to execute the plugin and catches any errors, wrapping them in an SMTPError instance. / const use = (step: string, plugin: Mail.PluginFunction) => pipeLogger('studiocms/lib/effects/smtp/SMTPMailer.use')( Effect.try({ try: () => _use(step, plugin), catch: (error) => new SMTPError({ error }), }) );
/ Sends an email using the provided mail options.
@param mailOptions - The options for the email to be sent, including recipients, subject, and content. @returns An Effect that resolves with the sent message information (SMTPTransport.SentMessageInfo) on success or fails with an SMTPError on error.
The function uses an asynchronous effect to handle the email sending process. If an error occurs during the sending process, it wraps the error in an SMTPError and fails the effect. Otherwise, it succeeds with the information about the sent message. / const sendMail = ( mailOptions: Mail.Options ): Effect.Effect<SMTPTransport.SentMessageInfo, SMTPError, never> => pipeLogger('studiocms/lib/effects/smtp/SMTPMailer.sendMail')( Effect.async<SMTPTransport.SentMessageInfo, SMTPError>((resume) => { _sendMail(mailOptions, (error, info) => { if (error) { const toFail = new SMTPError({ error }); resume(errorTap(Effect.fail(toFail), toFail)); } else { resume(Effect.succeed(info)); } }); }) );
/ An effect that attempts to retrieve the version string by invoking the _getVersionString function. If an error occurs during the operation, it catches the error and wraps it in an SMTPError instance.
@constant @throws {SMTPError} If an error occurs while retrieving the version string. / const getVersionString = pipeLogger('studiocms/lib/effects/smtp/SMTPMailer.getVersionString')( Effect.try({ try: () => _getVersionString(), catch: (error) => new SMTPError({ error }), }) );
/ Sets up a proxy for SMTP communication.
This function wraps the _setupProxy function in an Effect.try block to handle potential errors gracefully. If an error occurs during the setup, it will be caught and wrapped in an SMTPError instance.
@param proxyUrl - The URL of the proxy to be set up. @returns An Effect that attempts to set up the proxy and handles any errors. / const setupProxy = (proxyUrl: string) => pipeLogger('studiocms/lib/effects/smtp/SMTPMailer.setupProxy')( Effect.try({ try: () => _setupProxy(proxyUrl), catch: (error) => new SMTPError({ error }), }) );
return { options, meta, dkim, transporter, MailMessage, close, isIdle, verify, use, sendMail, getVersionString, setupProxy, }; }), }).constructor
Methods
Section titled “Methods”Live()
Section titled “Live()”static Live(): any
Defined in: studiocms/packages/studiocms/src/lib/effects/smtp.ts:337^
Creates a live SMTP layer by combining the default SMTP configuration with the provided options.
Returns
Section titled “Returns”any
A new SMTP layer with the provided options applied.
Type Aliases
Section titled “Type Aliases”tsMailer
Section titled “tsMailer”type tsMailer = typeof tsMailerConfig.$inferSelect;
Defined in: studiocms/packages/studiocms/src/lib/effects/smtp.ts:33^
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/effects/smtp.ts:38^
TypeSafe Table definition for use in StudioCMS Integrations
Variables
Section titled “Variables”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/effects/smtp.ts:28^
TypeSafe Table definition for use in StudioCMS Integrations
References
Section titled “References”Renames and re-exports dual