Merge pull request #63 from dternyak/fix-sendgrid

Fix sendgrid 400 error
This commit is contained in:
Daniel Ternyak 2019-01-10 17:50:46 -06:00 committed by GitHub
commit 1e5f7323fe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 19 additions and 15 deletions

View File

@ -8,7 +8,7 @@ from sentry_sdk.integrations.flask import FlaskIntegration
import sentry_sdk import sentry_sdk
from grant import commands, proposal, user, comment, milestone, admin, email from grant import commands, proposal, user, comment, milestone, admin, email
from grant.extensions import bcrypt, migrate, db, ma, mail, security from grant.extensions import bcrypt, migrate, db, ma, security
from grant.settings import SENTRY_RELEASE, ENV from grant.settings import SENTRY_RELEASE, ENV
@ -36,7 +36,6 @@ def register_extensions(app):
db.init_app(app) db.init_app(app)
migrate.init_app(app, db) migrate.init_app(app, db)
ma.init_app(app) ma.init_app(app)
mail.init_app(app)
user_datastore = SQLAlchemyUserDatastore(db, user.models.User, user.models.Role) user_datastore = SQLAlchemyUserDatastore(db, user.models.User, user.models.Role)
security.init_app(app, user_datastore) security.init_app(app, user_datastore)

View File

@ -1,6 +1,8 @@
from flask import render_template, Markup, current_app from flask import render_template, Markup, current_app
import sendgrid
from grant.extensions import mail from sendgrid.helpers.mail import Email, Mail, Content
from grant.settings import SENDGRID_API_KEY, SENDGRID_DEFAULT_FROM
from python_http_client import HTTPError
default_template_args = { default_template_args = {
'home_url': 'https://grant.io', 'home_url': 'https://grant.io',
@ -94,14 +96,20 @@ def send_email(to, type, email_args):
try: try:
email = generate_email(type, email_args) email = generate_email(type, email_args)
res = mail.send_email( sg = sendgrid.SendGridAPIClient(apikey=SENDGRID_API_KEY)
to_email=to,
mail = Mail(
from_email=Email(SENDGRID_DEFAULT_FROM),
to_email=Email(to),
subject=email['info']['subject'], subject=email['info']['subject'],
text=email['text'],
html=email['html'],
) )
mail.add_content(Content('text/plain', email['text']))
mail.add_content(Content('text/html', email['html']))
res = sg.client.mail.send.post(request_body=mail.get())
print('Just sent an email to %s of type %s, response code: %s' % (to, type, res.status_code)) print('Just sent an email to %s of type %s, response code: %s' % (to, type, res.status_code))
except Exception as e: except HTTPError as e:
print('An error occured while sending an email to %s - %s: %s' % (to, e.__class__.__name__, e)) print('An HTTP error occured while sending an email to %s - %s: %s' % (to, e.__class__.__name__, e))
if hasattr(e, 'body'):
print(e.body) print(e.body)
except Exception as e:
print('An unknown error occured while sending an email to %s - %s: %s' % (to, e.__class__.__name__, e))

View File

@ -4,12 +4,10 @@ from flask_bcrypt import Bcrypt
from flask_marshmallow import Marshmallow from flask_marshmallow import Marshmallow
from flask_migrate import Migrate from flask_migrate import Migrate
from flask_sqlalchemy import SQLAlchemy from flask_sqlalchemy import SQLAlchemy
from flask_sendgrid import SendGrid
from flask_security import Security from flask_security import Security
bcrypt = Bcrypt() bcrypt = Bcrypt()
db = SQLAlchemy() db = SQLAlchemy()
migrate = Migrate() migrate = Migrate()
ma = Marshmallow() ma = Marshmallow()
mail = SendGrid()
security = Security() security = Security()

View File

@ -51,8 +51,7 @@ redis==2.10.6
markdownify markdownify
# email # email
flask-sendgrid==0.6 sendgrid==5.6.0
sendgrid==5.3.0
# input validation # input validation
flask-yolo2API==0.2.6 flask-yolo2API==0.2.6