WebFaction
Community site: login faq

I have a registration form in which the user enters their information and an confirmation email is sent to them. I have come across a problem when sending mail using the Flask-Mail library (I believe that libraries aren't the cause of the problem, but I cannot identify what is causing this particular error). Every time I send an email with the following configurations below:

Note: This error also occurs with the following conditions:

  • SSL is set to true (TLS is set to false) and the appropriate port is given.

Other errors when changing the configuration:

  • When SSL is set to false, but 456 is left as the port, I get a Gateway Timeout error. The case below uses the default port 25 with no SSL or TLS.

__init__.py (Appropriate libraries for Flask-Mail have been imported. Correct credentials have also been used.)

MAIL_DEFAULT_SENDER = 'EMAIL_ADDRESS_GOES_HERE'

app.config.update(dict(
    DEBUG = True,
    MAIL_SERVER = 'smtp.webfaction.com',
    MAIL_PORT = 25,
    MAIL_USE_TLS = False,
    MAIL_USE_SSL = False,
    MAIL_USERNAME = os.environ.get('USERNAME'),
    MAIL_PASSWORD = os.environ.get('PASSWORD'),
))

mail = Mail(app)

def send_email(to, subject, template):
    msg = Message(
        subject,
        recipients=[to],
        html=template,
        sender=MAIL_DEFAULT_SENDER
    )
    mail.send(msg)

Registration View handles sending the email with the send_mail function above

I get the error in my error log below:

error log (Timestamps removed for simplicity):

send: 'ehlo WEBSERVERGOESHERE.webfaction.com\\r\\n'
reply: '250-smtp.webfaction.com\\r\\n'
reply: '250-PIPELINING\\r\\n'
reply: '250-SIZE 36700160\\r\\n'
reply: '250-VRFY\\r\\n'
reply: '250-ETRN\\r\\n'
reply: '250-STARTTLS\\r\\n'
reply: '250-AUTH LOGIN DIGEST-MD5 PLAIN CRAM-MD5\\r\\n'
reply: '250-AUTH=LOGIN DIGEST-MD5 PLAIN CRAM-MD5\\r\\n'
reply: '250-ENHANCEDSTATUSCODES\\r\\n'
reply: '250-8BITMIME\\r\\n'
reply: '250 DSN\\r\\n'
reply: retcode (250); Msg: smtp.webfaction.com
PIPELINING
SIZE 36700160
VRFY
ETRN
STARTTLS
 AUTH LOGIN DIGEST-MD5 PLAIN CRAM-MD5
 AUTH=LOGIN DIGEST-MD5 PLAIN CRAM-MD5
ENHANCEDSTATUSCODES
 8BITMIME
DSN
 send: u'mail FROM:<EMAILGOESHERE@WEBSITEGOESHERE> size=1145\\r\\n'
 reply: '250 2.1.0 Ok\\r\\n'
 reply: retcode (250); Msg: 2.1.0 Ok
send: u'rcpt TO:<USERNAME@SOMEEMAILCLIENT>\\r\\n'
reply: '554 5.7.1 <WEBSERVER.webfaction.com[IP_ADDRESS]>: Client host rejected: Access denied\\r\\n'
 reply: retcode (554); Msg: 5.7.1 <WEBSERVER.webfaction.com[IP_ADDRESS]>: Client host rejected: Access denied
 send: 'rset\\r\\n'
reply: '250 2.0.0 Ok\\r\\n'
 reply: retcode (250); Msg: 2.0.0 Ok
send: 'quit\\r\\n'
 reply: '221 2.0.0 Bye\\r\\n'
 reply: retcode (221); Msg: 2.0.0 Bye
 mod_wsgi (pid=11625): Exception occurred processing WSGI script '/home/username/webapps/myapp/wsgi.py'.
Traceback (most recent call last):
 File "/home/username/lib/python2.7/Flask-0.10.1-py2.7.egg/flask/app.py", line 1836, in __call__
 return self.wsgi_app(environ, start_response)
File "/home/username/webapps/myapp/myapp/__init__.py", line 49, in __call__
return self.app(environ, start_response)
File "/home/username/lib/python2.7/Flask-0.10.1-py2.7.egg/flask/app.py", line 1820, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/home/usename/lib/python2.7/Flask-0.10.1-py2.7.egg/flask/app.py", line 1403, in handle_exception
 reraise(exc_type, exc_value, tb)
File "/home/username/lib/python2.7/Flask-0.10.1-py2.7.egg/flask/app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "/home/username/lib/python2.7/Flask-0.10.1-py2.7.egg/flask/app.py", line 1477, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/home/username/lib/python2.7/Flask-0.10.1-py2.7.egg/flask/app.py", line 1381, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/home/username/lib/python2.7/Flask-0.10.1-py2.7.egg/flask/app.py", line 1475, in full_dispatch_request
rv = self.dispatch_request()
 File "/home/username/lib/python2.7/Flask-0.10.1-py2.7.egg/flask/app.py", line 1461, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/username/webapps/myapp/myapp/__init__.py", line 155, in register
 send_email(form.email.data, subject, html)
 File "/home/username/webapps/myapp/myapp/__init__.py", line 60, in send_email
mail.send(msg)
File "/home/username/lib/python2.7/Flask_Mail-0.9.1-py2.7.egg/flask_mail.py", line 492, in send
 message.send(connection)
File "/home/username/lib/python2.7/Flask_Mail-0.9.1-py2.7.egg/flask_mail.py", line 427, in send
connection.send(self)

File "/home/username/lib/python2.7/Flask_Mail-0.9.1-py2.7.egg/flask_mail.py", line 192, in send
message.rcpt_options)

File "/usr/lib64/python2.7/smtplib.py", line 734, in sendmail
raise SMTPRecipientsRefused(senders)

SMTPRecipientsRefused: {u'EMAILGOESHERE@WEBSITEGOESHERE': (554, '5.7.1 <WEBSERVERGOESHERE.webfaction.com[IP_ADDRESS]>: Client host rejected: Access denied')}

Can anyone help me with this error. I'm not quite sure how to approach this issue.

asked 03 Dec '15, 06:20

Guled
1538
accept rate: 100%

edited 03 Dec '15, 15:35


"Client host rejected: Access denied" most commonly indicates that you're trying to send a message without authenticating first.

I see you're getting your credentials from the environment. How and where are you setting them in the environment?

permanent link

answered 03 Dec '15, 16:05

seanf
12.2k31836
accept rate: 37%

I stored my environment variables in the $HOME/.bash_profile. I used the export command to set my USERNAME and PASSWORD credentials and then I used the os library in the __init__.py file to use the get method to obtain those environment variables. I just discovered where to place environment variables yesterday. The post to that thread on this site is here: https://community.webfaction.com/questions/19266/where-do-i-set-my-environment-variables-using-python

(03 Dec '15, 17:27) Guled

.bash_profile is only used for interactive login sessions.

Please try exporting the variables in your app's apache2/bin/start script instead - that way, they'll be set for the httpd worker processes that run your application.

(03 Dec '15, 17:32) seanf

I'll give that a shot when I have time today and let you know how it goes. Thank you so much for your support Sean. If there aren't any problems I'll be sure to mark the answer as correct when I get back to testing it later today.

(03 Dec '15, 20:28) Guled

You're very welcome!

(03 Dec '15, 20:34) seanf

Hello Sean. I removed the export commands from the .bash_profile and inserted them into my apps apache2/bin/start script. I'm not sure if you needed to append the export command before the variable, but did two variants where I included the export command in front of the variable and I tried to also remove the export command from the variable definitions. Either way, I got the same SMTPRecipientsRefused error as before.

(03 Dec '15, 22:50) Guled

The correct syntax is:

export USERNAME="whatever"
export PASSWORD="whatever"

Did you fully stop and start the app after making the change?

(03 Dec '15, 22:58) seanf

Does a restart ($HOME/webapps/myapp/myapp/bin/restart) accomplish the same thing? If not, then no I did not do a separate stop and then start.

(03 Dec '15, 23:01) Guled

Ok, please try a separate stop and start. Run stop a few times until you see a message indicating the httpd is no longer running, then start it again.

(03 Dec '15, 23:10) seanf

Ok so I ran the stop command like so:

$ ~/webapps/devstash/apache2/bin/stop

And I go to my site and I see a 502 error, so I'm guessing that worked, but I have a problem with my start command. For some reason when go to the bin directory using the cd command through SSH, I get this error when I run ./start. Correct me if this is the wrong way of running start. And it totally makes sense why we don't want to use restart. I get it now.

My start command:

[USERNAME@WEBSERVER bin]$ ./start

-bash: ./start: /bin/bash^M: bad interpreter: No such file or directory

(03 Dec '15, 23:14) Guled

Figure it out:

sed -i 's/\r//' start

Format error, I'll go back to testing it now and report back.

For anyone else who runs into this: http://stackoverflow.com/questions/2920416/configure-bin-shm-bad-interpreter

(03 Dec '15, 23:18) Guled

You're amazing Sean! My program is working!!!!

(03 Dec '15, 23:19) Guled

Thank you so much!!!!

(03 Dec '15, 23:19) Guled
showing 5 of 12 show 7 more comments
Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×337
×259
×113
×69
×44

question asked: 03 Dec '15, 06:20

question was seen: 6,960 times

last updated: 03 Dec '15, 23:19

WEBFACTION
REACH US
SUPPORT
AFFILIATE PROGRAM
LEGAL
© COPYRIGHT 2003-2019 SWARMA LIMITED - WEBFACTION IS A SERVICE OF SWARMA LIMITED
REGISTERED IN ENGLAND AND WALES 5729350 - VAT REGISTRATION NUMBER 877397162
5TH FLOOR, THE OLD VINYL FACTORY, HAYES, UB3 1HA, UNITED KINGDOM