Weblate setup SES email for password reset - django

I tried to set up a Weblate instance on AWS to send password reset emails. I followeed the following steps:
pip install boto
pip install django-ses
sudo vim /opt/bitnami/apps/django/lib/python3.7/site-packages/Django-2.1.4-py3.7.egg/django/conf/global_settings.py
EMAIL_BACKEND = 'django_ses.SESBackend'
AWS_ACCESS_KEY_ID = 'xxxx'
AWS_SECRET_ACCESS_KEY = 'xxx'
However it still doesn't work properly and I'm not getting any errors. my AWS key has a full permission to SES.
Update:
If i run send_mail from ./manage.py shell I get this output:
>>> send_mail('Subject here', 'Here is the message.', 'xx#gmail.com',['xx#gmail.com'], fail_silently=False)
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/opt/bitnami/apps/weblate/venv/lib/python3.7/site-packages/django/core/mail/__init__.py", line 60, in send_mail
return mail.send()
File "/opt/bitnami/apps/weblate/venv/lib/python3.7/site-packages/django/core/mail/message.py", line 291, in send
return self.get_connection(fail_silently).send_messages([self])
File "/opt/bitnami/apps/weblate/venv/lib/python3.7/site-packages/django/core/mail/backends/smtp.py", line 103, in send_messages
new_conn_created = self.open()
File "/opt/bitnami/apps/weblate/venv/lib/python3.7/site-packages/django/core/mail/backends/smtp.py", line 63, in open
self.connection = self.connection_class(self.host, self.port, **connection_params)
File "/opt/bitnami/python/lib/python3.7/smtplib.py", line 251, in __init__
(code, msg) = self.connect(host, port)
File "/opt/bitnami/python/lib/python3.7/smtplib.py", line 336, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/opt/bitnami/python/lib/python3.7/smtplib.py", line 307, in _get_socket
self.source_address)
File "/opt/bitnami/python/lib/python3.7/socket.py", line 727, in create_connection
raise err
File "/opt/bitnami/python/lib/python3.7/socket.py", line 716, in create_connection
sock.connect(sa)
ConnectionRefusedError: [Errno 111] Connection refused

You're editing Django default settings, which is weird way to change configuration, but it seems that Bitnami is recommending this approach. It seems the setting is not applied properly as the test command still uses default SMTP backend.
Things to check:
is setting actually applied: ./manage.py shell -c 'from django.conf import settings; print(settings.EMAIL_BACKEND)'
was django-ses installed into system? better run the installation as root sudo pip install django-ses
aren't you mixing Python 2 and Python 3 environments (not sure how this is done in Bitnami images)

Related

Django smtp connection with office 365 Outlook

We are receiving the following error using the Office 365 SMTP server to send mails from the Django application.
Traceback (most recent call last):
File "/home/app/views.py", line 598, in send_mail
django.core.mail.send_mail(
File "/usr/local/lib/python3.8/site-packages/django/core/mail/__init__.py", line 61, in send_mail
return mail.send()
File "/usr/local/lib/python3.8/site-packages/django/core/mail/message.py", line 284, in send
return self.get_connection(fail_silently).send_messages([self])
File "/usr/local/lib/python3.8/site-packages/django/core/mail/backends/smtp.py", line 102, in send_messages
new_conn_created = self.open()
File "/usr/local/lib/python3.8/site-packages/django/core/mail/backends/smtp.py", line 69, in open
self.connection.login(self.username, self.password)
File "/usr/local/lib/python3.8/smtplib.py", line 732, in login
raise last_exception
File "/usr/local/lib/python3.8/smtplib.py", line 721, in login
(code, resp) = self.auth(
File "/usr/local/lib/python3.8/smtplib.py", line 644, in auth
raise SMTPAuthenticationError(code, resp)
smtplib.SMTPAuthenticationError: (535, b'5.7.139 Authentication unsuccessful, SmtpClientAuthentication is disabled for the Mailbox. Visit https://aka.ms/smtp_auth_disabled for more information. [PN2PR01CA0172.INDPRD01.PROD.OUTLOOK.COM]')
following setup did in the Django setting.py
EMAIL_HOST=smtp.office365.com
EMAIL_USE_TLS=True
EMAIL_PORT=587 # also tried port 25
EMAIL_USER=test#example.com
EMAIL_PASSWORD=test
And following the steps for setting up the Outlook 365 SMTP relay account (Steps)
and also checked the Authenticated SMTP for the admin account. Is I am doing anything wrong or missing anything?
It will be appreciated for any help, Thank you :)

Send email with django + gunicorn + nginx get timeout

I'm trying to deploy a Django project in AWS with gunicorn + nginx but I'm getting a timeout error. My email backend uses AWS SES with SMTP authentication.
The first 2 emails are sent normally, after these I'm getting this error.
The account is already an unblocked restriction for sending an email.
Local django development server works fine, it sends emails normally. In local virtual machine with same EC2 specs sends emails normally.
Running in django shell I'm getting this log at third email sent:
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "<console>", line 5, in sendE
File "/home/ubuntu/myproj/share/forms.py", line 23, in share_email
email_message.send()
File "/home/ubuntu/.local/share/virtualenvs/myproj-rqmaW6Yv/lib/python3.7/site-packages/django/core/mail/message.py", line 291, in send
return self.get_connection(fail_silently).send_messages([self])
File "/home/ubuntu/.local/share/virtualenvs/myproj-rqmaW6Yv/lib/python3.7/site-packages/django/core/mail/backends/smtp.py", line 103, in send_messages
new_conn_created = self.open()
File "/home/ubuntu/.local/share/virtualenvs/myproj-rqmaW6Yv/lib/python3.7/site-packages/django/core/mail/backends/smtp.py", line 63, in open
self.connection = self.connection_class(self.host, self.port, **connection_params)
File "/usr/lib/python3.7/smtplib.py", line 251, in __init__
(code, msg) = self.connect(host, port)
File "/usr/lib/python3.7/smtplib.py", line 336, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/usr/lib/python3.7/smtplib.py", line 307, in _get_socket
self.source_address)
File "/usr/lib/python3.7/socket.py", line 727, in create_connection
raise err
File "/usr/lib/python3.7/socket.py", line 716, in create_connection
sock.connect(sa)
TimeoutError: [Errno 110] Connection timed out
```
Any idea?
Ok after testing a lot, I Found EC2 instances have a restriction over default smtp port connection. Change the port from 25 to 587 and it works perfectly.
More info: https://aws.amazon.com/es/premiumsupport/knowledge-center/smtp-connectivity-timeout-issues-ses/

django mail webform not connection to mail server

I spent a lot of time developing the form and connecting all the pieces together and now I cannot connect to my smtp server through django settings. Here are my settings...
DEFAULT_FROM_EMAIL = 'auto#domain.com'
EMAIL_HOST = 'smtp.mailhost.com'
EMAIL_HOST_USER = 'auto#domain.com'
EMAIL_HOST_PASSWORD = 'password'
EMAIL_USE_TLS = True
EMAIL_PORT = 465
I tried to send it through my form and it didn't work so I tried to send one through the shell..
from django.core.mail import send_mail
send_mail('subject','message','auto#domain.com', ['me#domain.com'], fail_silently=False)
and I get this traceback...
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/django/core/mail/__init__.py", line 62, in send_mail
return mail.send()
File "/usr/local/lib/python2.7/dist-packages/django/core/mail/message.py", line 286, in send
return self.get_connection(fail_silently).send_messages([self])
File "/usr/local/lib/python2.7/dist-packages/django/core/mail/backends/smtp.py", line 92, in send_messages
new_conn_created = self.open()
File "/usr/local/lib/python2.7/dist-packages/django/core/mail/backends/smtp.py", line 50, in open
self.connection = connection_class(self.host, self.port, **connection_params)
File "/usr/lib/python2.7/smtplib.py", line 251, in __init__
(code, msg) = self.connect(host, port)
File "/usr/lib/python2.7/smtplib.py", line 312, in connect
(code, msg) = self.getreply()
File "/usr/lib/python2.7/smtplib.py", line 363, in getreply
raise SMTPServerDisconnected("Connection unexpectedly
closed")
SMTPServerDisconnected: Connection unexpectedly closed
You should recheck the data you use: login, password, etc.
If it doesn't help, try to use port 587.
SMTP uses this one. I hope it will help.
495 is now deprecated.

Python SSL web page scraping

I am trying to scrape web page using Python 2.7 and BeautifulSoup but I can't get past a protocol error which doesn't make much sense to me. This only happens on the specific website that I need to do this for: https://edd.telstra.com/telstra
The code I use just for basic test:
#! /usr/bin/python
from urllib import urlopen
from BeautifulSoup import BeautifulSoup
import re
# Copy all of the content from the provided web page
webpage = urlopen("https://edd.telstra.com/telstra/").read()
And I get the following error (running on Ubuntu 12.10):
Traceback (most recent call last):
File "e.py", line 8, in <module>
webpage = urlopen("https://edd.telstra.com/telstra/").read()
File "/usr/lib/python2.7/urllib.py", line 86, in urlopen
return opener.open(url)
File "/usr/lib/python2.7/urllib.py", line 207, in open
return getattr(self, name)(url)
File "/usr/lib/python2.7/urllib.py", line 436, in open_https
h.endheaders(data)
File "/usr/lib/python2.7/httplib.py", line 958, in endheaders
self._send_output(message_body)
File "/usr/lib/python2.7/httplib.py", line 818, in _send_output
self.send(msg)
File "/usr/lib/python2.7/httplib.py", line 780, in send
self.connect()
File "/usr/lib/python2.7/httplib.py", line 1165, in connect
self.sock = ssl.wrap_socket(sock, self.key_file, self.cert_file)
File "/usr/lib/python2.7/ssl.py", line 381, in wrap_socket
ciphers=ciphers)
File "/usr/lib/python2.7/ssl.py", line 143, in __init__
self.do_handshake()
File "/usr/lib/python2.7/ssl.py", line 305, in do_handshake
self._sslobj.do_handshake()
IOError: [Errno socket error] [Errno 1] _ssl.c:504: error:1408F119:SSL routines:SSL3_GET_RECORD:decryption failed or bad record mac
Could someone tell me if there is some parameter that I need to specify to get this page to download in Python? It seems that this is the problem just on this web page as the code above (plus lots of other code I tried) works fine on other HTTPS/SSL pages I tried.
Thanks for any help!
I can recommend using requests lib :
def get_page(login, password):
'''Docstring
'''
url = 'https://qwe.qwe'
payload = {
'user': login,
'pass': password
}
with requests.Session() as my_session:
my_session.post(url, data=payload)
data = my_session.get(url)
return data.text
More info : http://docs.python-requests.org/en/latest/user/advanced/#session-objects

Configuring Django/Pinax Email

I'm trying to configure automated emails for my Pinax app.
In the shell (python manage.py shell) I am doing the following to test my email settings:
>>> send_mail ('Test','Test','myemail#myemail.com',['myemail#myemail.com'])
However I am getting the follow traceback:
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/Users/myname/Desktop/myenv2/lib/python2.7/site-packages/django/core/mail/__init__.py", line 61, in send_mail
connection=connection).send()
File "/Users/myname/Desktop/myenv2/lib/python2.7/site-packages/django/core/mail/message.py", line 175, in send
return self.get_connection(fail_silently).send_messages([self])
File "/Users/myname/Desktop/myenv2/lib/python2.7/site-packages/django/core/mail/backends/smtp.py", line 78, in send_messages
new_conn_created = self.open()
File "/Users/myname/Desktop/myenv2/lib/python2.7/site-packages/django/core/mail/backends/smtp.py", line 41, in open
local_hostname=DNS_NAME.get_fqdn())
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/smtplib.py", line 242, in __init__
(code, msg) = self.connect(host, port)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/smtplib.py", line 302, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/smtplib.py", line 277, in _get_socket
return socket.create_connection((port, host), timeout)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 571, in create_connection
raise err
error: [Errno 60] Operation timed out
My email settings in settings.py are as follow:
# These are the email settings for sending out confirmation emails, alerts,etc.
DEFAULT_FROM_EMAIL = 'Mysite <myemail#myemal.com>'
EMAIL_USE_TLS = True
EMAIL_HOST = 'box401.bluehost.com'
EMAIL_HOST_USER = 'myemail#myemal.com'
EMAIL_HOST_PASSWORD = 'password'
EMAIL_PORT = 587
EMAIL_SUBJECT_PREFIX = '[Your Site] '
Any insight into what could be wrong? I checked my password and the settings with the host and they seem fine.