Skip to content

Commit

Permalink
Implemented the SendgridEMailAdapter.
Browse files Browse the repository at this point in the history
  • Loading branch information
lingthio committed Sep 16, 2017
1 parent 4cfb502 commit 3a3054d
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 8 deletions.
34 changes: 28 additions & 6 deletions flask_user/email_adapters/sendgrid_email_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
from flask_user import current_app, ConfigError
from flask_user.email_adapters import EmailAdapterInterface

SENDGRID_IMPORT_ERROR_MESSAGE = 'The sendgrid package is missing. Install sendgrid with "pip install sendgrid".'

class SendgridEmailAdapter(EmailAdapterInterface):
""" Implements the EmailAdapter interface to send emails with SendGrid Web API v3 using sendgrid-python."""
def __init__(self, app):
Expand All @@ -22,14 +24,17 @@ def __init__(self, app):

super(SendgridEmailAdapter, self).__init__(app)

sendgrid_api_key = app.config.get('SENDGRID_API_KEY')
if not sendgrid_api_key:
raise ConfigError(
"The SENDGRID_API_KEY setting is missing. Set SENDGRID_API_KEY in your app config.")

# Setup sendgrid-python
try:
from sendgrid import SendGridAPIClient
self.sg = SendGridAPIClient(apikey=sendgrid_api_key)
except ImportError:
raise ConfigError(
"sendgrid-python has not been installed. Install sendgrid-python with 'pip install sendgrid' or use a different EmailAdapter.")

pass # TODO
raise ConfigError(SENDGRID_IMPORT_ERROR_MESSAGE)


def send_email_message(self, recipient, subject, html_message, text_message, sender_email, sender_name):
Expand All @@ -44,8 +49,25 @@ def send_email_message(self, recipient, subject, html_message, text_message, sen

if not current_app.testing: # pragma: no cover
try:
# Prepare Sendgrid helper objects
from sendgrid.helpers.mail import Email, Content, Substitution, Mail
pass # TODO
from_email = Email(sender_email, sender_name)
to_email = Email(recipient)
text_content = Content('text/plain', text_message)
html_content = Content('text/html', html_message)
# Prepare Sendgrid Mail object
# Note: RFC 1341: text must be first, followed by html
mail = Mail(from_email, subject, to_email, text_content)
mail.add_content(html_content)
# Send mail via the Sendgrid API
response = self.sg.client.mail.send.post(request_body=mail.get())
print(response.status_code)
print(response.body)
print(response.headers)
except ImportError:
pass # TODO
raise ConfigError(SENDGRID_IMPORT_ERROR_MESSAGE)
except Exception as e:
print(e)
print(e.body)
raise

2 changes: 1 addition & 1 deletion flask_user/email_adapters/sendmail_email_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def __init__(self, app, sender_email=None, sender_name=None):
from flask_sendmail import Mail
except ImportError:
raise ConfigError(
"Flask-Sendmail has not been installed. Install Flask-Sendmail with 'pip install Flask-Sendmail' or use a different EmailAdapter.")
"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):
Expand Down
2 changes: 1 addition & 1 deletion flask_user/email_adapters/smtp_email_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def __init__(self, app):
from flask_mail import Mail
except ImportError:
raise ConfigError(
"Flask-Mail has not been installed. Install Flask-Mail with 'pip install Flask-Mail' or use a different EmailAdapter.")
"The Flask-Mail package is missing. Install Flask-Mail with 'pip install Flask-Mail'.")
self.mail = Mail(app)

def send_email_message(self, recipient, subject, html_message, text_message, sender_email, sender_name):
Expand Down
3 changes: 3 additions & 0 deletions flask_user/tests/tst_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ class ConfigClass(object):
MAIL_PORT = int(os.getenv('MAIL_PORT', '465'))
MAIL_USE_SSL = os.getenv('MAIL_USE_SSL', True)

# Sengrid settings
SENDGRID_API_KEY = 'some-bogus-value'

# Flask-User settings
USER_APP_NAME = 'TestApp'
USER_ENABLE_EMAIL = True
Expand Down

0 comments on commit 3a3054d

Please sign in to comment.