1

So I built a flask app and it worked great when I ran it locally. Then I pushed it to my AWS EC2 instance. Once I was running it from my instance it was no longer sending emails. It would add to my database then the next line was to send the email. That is where it was failing. It was that Google was blocking the device. I then was able to allow that device and it was all working fine.

Fast forward and I added an elastic IP and then linked it to my domain and now it is not working again. It is still adding to my database so I think that it is the issue is google is not letting the application work. I am not sure how to solve this but I have been working on this for some time. This is the error code that I get on my instance

Traceback (most recent call last):
  File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 2292, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1815, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1718, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1813, in full_dispatch_request
    rv = self.dispatch_request()
  File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1799, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "FlaskApp.py", line 130, in register
    mail.send(msg)
  File "/usr/local/lib/python2.7/site-packages/flask_mail.py", line 492, in send
    message.send(connection)
  File "/usr/local/lib/python2.7/site-packages/flask_mail.py", line 427, in send
    connection.send(self)
  File "/usr/local/lib/python2.7/site-packages/flask_mail.py", line 178, in send
    "The message does not specify a sender and a default sender "
AssertionError: The message does not specify a sender and a default sender has not been configured

And on my website I get the tab that says "500 Internal Server Error" and the website displays the following.

Internal Server Error
The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.

Can someone help me figure this out. I would like to be able to send emails again using my flask app. I am new to this so my logic says that it is all because I am being denyed access to the google account. But I cannot find a link or anything that will enable me to allow access for the EC2 instance. I even have tried to use this: https://accounts.google.com/DisplayUnlockCaptcha but that doesn't work either.

UPDATE: It was working before and I did not change anything in my source code, see below:

FROM_EMAIL = os.environ.get('EMAIL_USERNAME')

app.config.update(
  DEBUG = False,
  MAIL_SERVER = 'smtp.gmail.com',
  MAIL_PORT = 465,
  MAIL_USE_SSL = True,
  MAIL_USERNAME = FROM_EMAIL,
  MAIL_PASSWORD = os.environ.get('EMAIL_PASSWORD_TOKEN'),
  )
mail = Mail(app)

and then when I call is I use:

msg = Message("Welcome",
    sender = FROM_EMAIL,
    recipients = [request.form["email"]])
msg.body = "Welcome! \n\n Congratulations on your sucessful registration."
mail.send(msg)

UPDATE #2: I am not sure why but I had to add a default sender and that worked but I am still unable to send emails. I now get the following error:

[2019-01-22 01:40:44,973] ERROR in app: Exception on /register/ [POST]
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 2292, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1815, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1718, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1813, in full_dispatch_request
    rv = self.dispatch_request()
  File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1799, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "FlaskApp.py", line 131, in register
    mail.send(msg)
  File "/usr/local/lib/python2.7/site-packages/flask_mail.py", line 492, in send
    message.send(connection)
  File "/usr/local/lib/python2.7/site-packages/flask_mail.py", line 427, in send
    connection.send(self)
  File "/usr/local/lib/python2.7/site-packages/flask_mail.py", line 192, in send
    message.rcpt_options)
  File "/usr/lib64/python2.7/smtplib.py", line 737, in sendmail
    raise SMTPSenderRefused(code, resp, from_addr)
SMTPSenderRefused: (530, '5.5.1 Authentication Required. Learn more at\n5.5.1  https://support.google.com/mail/?p=WantAuthError d21sm19653189pfo.162 - gsmtp', u'myemail@gmail.com')
199.169.1.69 - - [22/Jan/2019 01:40:44] "POST /register/ HTTP/1.1" 500 -

Not sure if it is helpful to know or not but I am able to log into gmail using SMTP cmds from the instance but I still get the error. I followed the directions found here: How to send email using simple SMTP commands via Gmail?

Ben
  • 251
  • 1
  • 8
  • 23
  • The error says "AssertionError: The message does not specify a sender and a default sender has not been configured". Have you made sure a sender (default or otherwise) is specified? – Matt Healy Jan 14 '19 at 06:14
  • @MattHealy Please see the update above. It was working before and all that I did was change the IP address and add a DNS record. That is why I think that it is google blocking the IP address from accessing the gmail account. Help here would be great. – Ben Jan 14 '19 at 16:49

1 Answers1

0

If you are getting the assertion error about specifying a sender, you are definitely not providing a sender address. Here's the relevant code in Flask-Mail: https://github.com/mattupstate/flask-mail/blob/e195fca6de1077cabb711426e6378f51dc39d598/flask_mail.py#L177. As you can see, at that point in the code it hasn't even tried sending the email yet, so the cause of that error cannot be Google blocking your device.

From the code you've provided, I would be checking to make sure that your environment variable EMAIL_USERNAME is actually being set. You could try hard-coding it to remove the environment variable from the equation and verify that the code is actually able to send email via Gmail.

Matt Healy
  • 18,033
  • 4
  • 56
  • 56
  • I already verified that it is being set and that I can see it when I run `printenv` Why would it work and then all of a sudden stop working? There were no changes to the code the only thing that changed was the IPv$ address – Ben Jan 15 '19 at 00:52