This repository has been archived by the owner on Oct 15, 2022. It is now read-only.
forked from lingthio/Flask-User
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsendmail_email_adapter.py
57 lines (41 loc) · 1.83 KB
/
sendmail_email_adapter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
"""This module implements the EmailAdapter interface for sendmail.
"""
# Author: Ling Thio <[email protected]>
# Copyright (c) 2013 Ling Thio
from __future__ import print_function
# Non-system imports are moved into the methods to make them an optional requirement
from flask_user import current_app, ConfigError
from flask_user.email_adapters import EmailAdapterInterface
class SendmailEmailAdapter(EmailAdapterInterface):
""" Implements the EmailAdapter interface to send emails with sendmail using Flask-Sendmail."""
def __init__(self, app, sender_email=None, sender_name=None):
"""Check config settings and setup Flask-Sendemail.
Args:
app(Flask): The Flask application instance.
"""
super(SendmailEmailAdapter, self).__init__(app)
# Setup Flask-Mail
try:
from flask_sendmail import Mail
except ImportError:
raise ConfigError(
"The Flask-Sendmail package is missing. Install Flask-Sendmail with 'pip install Flask-Sendmail'.")
self.mail = Mail(app)
def send_email_message(self, recipient, subject, html_message, text_message, sender_email, sender_name):
""" Send email message via Flask-Sendmail.
Args:
recipient: Email address or tuple of (Name, Email-address).
subject: Subject line.
html_message: The message body in HTML.
text_message: The message body in plain text.
"""
if not current_app.testing: # pragma: no cover
# Prepare email message
from flask_sendmail import Message
message = Message(
subject,
recipients=[recipient],
html=html_message,
body=text_message)
# Send email message
self.mail.send(message)