lingthio / Flask-User

Customizable User Authorization & User Management: Register, Confirm, Login, Change username/password, Forgot password and more.
http://flask-user.readthedocs.io/
MIT License
1.06k stars 292 forks source link

SendGrid email adapter does not work #290

Open bmarsh9 opened 4 years ago

bmarsh9 commented 4 years ago

When sending emails with SendGrid, the code does not work. For example, in the file sendgrid_email_adapter.py, the code self.sg = SendGridAPIClient(apikey=sendgrid_api_key) is invalid with the sendgrid library.

I was able to successfully send email by replacing the function with the following code:

def send_email_message(self, recipient, subject, html_message, text_message, sender_email, sender_name):
        if not current_app.testing:  # pragma: no cover
            try:
                # Prepare Sendgrid helper objects
                from sendgrid.helpers.mail import Mail
                msg = Mail(
                  from_email=sender_email,
                  to_emails=recipient,
                  subject=subject,
                  html_content=html_message)
                response = self.sg.send(msg)
                print(response.status_code)
                print(response.body)
                print(response.headers)
            except ImportError:
                raise ConfigError(SENDGRID_IMPORT_ERROR_MESSAGE)
            except Exception as e:
                print(e)
                #print(e.body)
                raise
bmarsh9 commented 4 years ago

And change self.sg = SendGridAPIClient(apikey=sendgrid_api_key) to self.sg = SendGridAPIClient(sendgrid_api_key)