From 1795715a3eb859f0f4a901501fe7d4cd20436b1a Mon Sep 17 00:00:00 2001 From: jingfelix Date: Sat, 16 Dec 2023 13:30:25 +0800 Subject: [PATCH] Add `MAIL_SEND_OPTIONS` for `send_mail` (#61) --- CHANGELOG.md | 5 +++++ docs/index.md | 4 ++++ flask_mailman/__init__.py | 3 +++ flask_mailman/backends/smtp.py | 7 ++++++- 4 files changed, 18 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index dcdb84f..f5e0ddb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/docs/index.md b/docs/index.md index d1842d7..f75e324 100644 --- a/docs/index.md +++ b/docs/index.md @@ -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 diff --git a/flask_mailman/__init__.py b/flask_mailman/__init__.py index 12b4432..65c5f21 100644 --- a/flask_mailman/__init__.py +++ b/flask_mailman/__init__.py @@ -203,6 +203,7 @@ def __init__( use_localtime, file_path, default_charset, + mail_options, backend, ): self.server = server @@ -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 @@ -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, ) diff --git a/flask_mailman/backends/smtp.py b/flask_mailman/backends/smtp.py index 4198b49..5439b74 100644 --- a/flask_mailman/backends/smtp.py +++ b/flask_mailman/backends/smtp.py @@ -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