Merge pull request #63 from dternyak/fix-sendgrid
Fix sendgrid 400 error
This commit is contained in:
commit
1e5f7323fe
|
@ -8,7 +8,7 @@ from sentry_sdk.integrations.flask import FlaskIntegration
|
|||
import sentry_sdk
|
||||
|
||||
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
|
||||
|
||||
|
||||
|
@ -36,7 +36,6 @@ def register_extensions(app):
|
|||
db.init_app(app)
|
||||
migrate.init_app(app, db)
|
||||
ma.init_app(app)
|
||||
mail.init_app(app)
|
||||
user_datastore = SQLAlchemyUserDatastore(db, user.models.User, user.models.Role)
|
||||
security.init_app(app, user_datastore)
|
||||
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
from flask import render_template, Markup, current_app
|
||||
|
||||
from grant.extensions import mail
|
||||
import sendgrid
|
||||
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 = {
|
||||
'home_url': 'https://grant.io',
|
||||
|
@ -94,14 +96,20 @@ def send_email(to, type, email_args):
|
|||
|
||||
try:
|
||||
email = generate_email(type, email_args)
|
||||
res = mail.send_email(
|
||||
to_email=to,
|
||||
sg = sendgrid.SendGridAPIClient(apikey=SENDGRID_API_KEY)
|
||||
|
||||
mail = Mail(
|
||||
from_email=Email(SENDGRID_DEFAULT_FROM),
|
||||
to_email=Email(to),
|
||||
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))
|
||||
except Exception as e:
|
||||
print('An error occured while sending an email to %s - %s: %s' % (to, e.__class__.__name__, e))
|
||||
if hasattr(e, 'body'):
|
||||
except HTTPError as e:
|
||||
print('An HTTP error occured while sending an email to %s - %s: %s' % (to, e.__class__.__name__, e))
|
||||
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))
|
||||
|
|
|
@ -4,12 +4,10 @@ from flask_bcrypt import Bcrypt
|
|||
from flask_marshmallow import Marshmallow
|
||||
from flask_migrate import Migrate
|
||||
from flask_sqlalchemy import SQLAlchemy
|
||||
from flask_sendgrid import SendGrid
|
||||
from flask_security import Security
|
||||
|
||||
bcrypt = Bcrypt()
|
||||
db = SQLAlchemy()
|
||||
migrate = Migrate()
|
||||
ma = Marshmallow()
|
||||
mail = SendGrid()
|
||||
security = Security()
|
||||
|
|
|
@ -51,8 +51,7 @@ redis==2.10.6
|
|||
markdownify
|
||||
|
||||
# email
|
||||
flask-sendgrid==0.6
|
||||
sendgrid==5.3.0
|
||||
sendgrid==5.6.0
|
||||
|
||||
# input validation
|
||||
flask-yolo2API==0.2.6
|
||||
|
|
Loading…
Reference in New Issue