Email Service
The email service: the platform’s outgoing email.
Email does not depend on a cloud: Smarter sends it with SMTP, to any SMTP server, e.g. AWS
Simple Email Service’s. So EmailService is implemented by SMTPEmailService,
for every cloud provider, and an email API, e.g. a cloud’s, would be another implementation.
The platform uses it through smarter.apps.infrastructure.services.infrastructure
.email. Tests replace it with configure_email(), e.g. with
InMemoryEmailService.
from smarter.apps.infrastructure.services import infrastructure
infrastructure.email.send_email(subject="Welcome!", body="<p>Hello</p>", to="user@example.com", html=True)
- class smarter.apps.infrastructure.services.email.EmailService(provider_name, *args, **kwargs)[source]
Bases:
InfrastructureServiceThe platform’s outgoing email.
- static admin_bcc(recipients)[source]
The blind copy to the platform’s admin,
smarter_settings.email_admin.
- error_class
alias of
EmailServiceError
- message(subject, body, to, html=False, from_email=None)[source]
Return a message, from
smarter_settings.smtp_from_emailunlessfrom_emailis given.It has no
Bccheader: blind copies are added to the envelope by_deliver().- Return type:
- send_email(subject, body, to, html=False, from_email=None, quiet=False, bcc_admin=True)[source]
Send an email.
Failures are logged, and announced with
email_failed, rather than raised, so that email never breaks the request that sends it.- Parameters:
subject (
str) – The subject.body (
str) – The body, HTML ifhtmlis True.to (
Union[str,List[str]]) – The recipient, or recipients. Invalid addresses are dropped.html (
bool) – True if the body is HTML.from_email (
Optional[str]) – The sender, by defaultsmarter_settings.smtp_from_email.quiet (
bool) – True to only log what would have been sent.bcc_admin (
bool) – Send a blind copy to the platform’s admin,smarter_settings.email_admin. Pass False for an email that carries a secret, e.g. a password reset link, which only its recipient may see.
- Return type:
- Returns:
True if the email was sent.
- service_name: str = 'email'
The name of the service in signals and logs, see
InfrastructureServiceNames.
- class smarter.apps.infrastructure.services.email.InMemoryEmailService(ready=True, **kwargs)[source]
Bases:
EmailServiceEmail that is kept in
outbox, for tests and local development.- Parameters:
ready (
bool) – Whether the service is ready.
- class smarter.apps.infrastructure.services.email.SMTPEmailService(allow_in_tests=False, smtp_class=<class 'smtplib.SMTP'>, **kwargs)[source]
Bases:
EmailServiceEmail, with SMTP, configured by the
smtp_*settings ofsmarter_settings.In the unit tests, nothing is sent, unless
allow_in_testsis True, so that tests never email real people.- Parameters:
- class smarter.apps.infrastructure.services.email.SentEmail(subject, sender, recipients, bcc=<factory>, body='', headers=<factory>)[source]
Bases:
objectAn email that
InMemoryEmailServicesent.- __init__(subject, sender, recipients, bcc=<factory>, body='', headers=<factory>)
- smarter.apps.infrastructure.services.email.configure_email(factory)[source]
Set the factory of the email service that
get_email()returns.- Parameters:
factory (
Optional[Callable[[],EmailService]]) – Returns the service, or None to restore the default,SMTPEmailService.- Return type: