Skip to content

Commit

Permalink
Add MAIL_SEND_OPTIONS for send_mail (#61)
Browse files Browse the repository at this point in the history
  • Loading branch information
jingfelix authored Dec 16, 2023
1 parent 2af0ced commit 1795715
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 1 deletion.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## [1.0.1] - 2023-12-16

- Add configuration key `MAIL_SEND_OPTIONS` to support setting `mail_options` for `smtplib.SMTP.send_mail`
(e.g. `SMTPUTF8`) ([#61](https://github.com/waynerv/flask-mailman/pull/61)).

## [1.0.0] - 2023-11-04

- Drop Python 3.6 support.
Expand Down
4 changes: 4 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ Flask-Mailman is configured through the standard Flask config API. A list of con

Default: False.

- **MAIL_SEND_OPTIONS**: `mail_options` for `smtplib.SMTP.sendmail`. If `SMTPUTF8` is included in `MAIL_SEND_OPTIONS`, and the server supports it, `from_mail` and `to` may contain non-ASCII characters.

Default: `[]`

Emails are managed through a *Mail* instance:
```python
from flask import Flask
Expand Down
3 changes: 3 additions & 0 deletions flask_mailman/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ def __init__(
use_localtime,
file_path,
default_charset,
mail_options,
backend,
):
self.server = server
Expand All @@ -218,6 +219,7 @@ def __init__(
self.use_localtime = use_localtime
self.file_path = file_path
self.default_charset = default_charset
self.mail_options = mail_options
self.backend = backend


Expand Down Expand Up @@ -255,6 +257,7 @@ def init_mail(config, testing=False):
config.get('MAIL_USE_LOCALTIME', False),
config.get('MAIL_FILE_PATH'),
config.get('MAIL_DEFAULT_CHARSET', 'utf-8'),
config.get('MAIL_SEND_OPTIONS', []),
mail_backend,
)

Expand Down
7 changes: 6 additions & 1 deletion flask_mailman/backends/smtp.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,12 @@ def _send(self, email_message):
recipients = [sanitize_address(addr, encoding) for addr in email_message.recipients()]
message = email_message.message()
try:
self.connection.sendmail(from_email, recipients, message.as_bytes(linesep='\r\n'))
self.connection.sendmail(
from_email,
recipients,
message.as_bytes(linesep='\r\n'),
mail_options=self.mailman.mail_options,
)
except smtplib.SMTPException:
if not self.fail_silently:
raise
Expand Down

0 comments on commit 1795715

Please sign in to comment.