gws.plugin.email_helper

Email helper.

The email helper sends email messages over SMTP. A Message has a subject, recipients, an optional sender and Bcc addresses, a plain text part and an optional HTML part. Both parts are encoded as quoted-printable. If the message has no sender, the configured mailFrom is used.

The SMTP connection mode is ssl (SMTP over SSL, default port 465), tls (STARTTLS, default port 587) or plain (no encryption, default port 25). If login is set, the helper logs in before sending.

The helper is used, for example, by the email multi-factor adapter (gws.plugin.auth_mfa.email) and the account plugin. It must be configured with an SMTP server.

Example:

helpers+ {
    type "email"
    mailFrom "gws@example.com"
    smtp {
        host "smtp.example.com"
        mode "tls"
        login "gws"
        password "secret"
    }
}

Usage in Python:

helper = cast(gws.plugin.email_helper.Object, root.app.helper('email'))
helper.send_mail(gws.plugin.email_helper.Message(
    subject='Hello',
    mailTo='user_1@example.com',
    text='Hello, world.',
))

Source code: gws.plugin.email_helper

Package Contents

class gws.plugin.email_helper.Config(*args, **kwargs)

Bases: gws.Config

Sending of emails via SMTP.

mailFrom: str = ''

Sender address for messages that do not set one.

smtp: SmtpConfig

SMTP server configuration.

exception gws.plugin.email_helper.Error

Bases: gws.Error

Raised when an email cannot be sent.

class gws.plugin.email_helper.Message(*args, **kwargs)

Bases: gws.Data

Email message.

bcc: str

Bcc addresses.

html: str

HTML content.

mailFrom: str

From address (default if omitted).

mailTo: str

To addresses, comma separated.

subject: str

Subject.

text: str

Plain text content.

class gws.plugin.email_helper.Object

Bases: gws.Node

Email helper.

mailFrom: str

Default sender address.

smtp: _SmtpServer

SMTP server settings.

configure()

Configuration hook.

Called when the node is initialized. Raising an exception here aborts the creation of the node.

send_mail(m: Message)

Send an email message.

Parameters:

m – The message.

Raises:

Error – If the connection to the SMTP server or the sending fails.

class gws.plugin.email_helper.SmtpConfig(*args, **kwargs)

Bases: gws.Config

SMTP server configuration.

host: str

SMTP host name.

login: str = ''

Login name for the SMTP server.

mode: SmtpMode

Connection encryption mode.

password: str = ''

Password for the SMTP server.

port: int = 0

SMTP port.

timeout: gws.Duration = '30'

Connection timeout.

class gws.plugin.email_helper.SmtpMode

Bases: gws.Enum

SMTP connection modes.

plain = 'plain'

Plain connection without encryption.

ssl = 'ssl'

SSL connection.

tls = 'tls'

TLS connection (STARTTLS).