how to send email in django? [duplicate] - python

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Django - [Errno 111] Connection refused
I have found a a link on how to send email with django : https://docs.djangoproject.com/en/dev/topics/email/
i followed the example and i got this:
>>> from django.core.mail import send_mail
>>> send_mail('dfdf', 'dfdfdf', 'logadrinab#agile.com.ph', ['ddcahanap#agile.com.ph'])
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 61, in send_mail
connection=connection).send()
File "/usr/local/lib/python2.7/dist-packages/django/core/mail/message.py", line 251, 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 79, in send_messages
new_conn_created = self.open()
File "/usr/local/lib/python2.7/dist-packages/django/core/mail/backends/smtp.py", line 42, in open
local_hostname=DNS_NAME.get_fqdn())
File "/usr/lib/python2.7/smtplib.py", line 239, in __init__
(code, msg) = self.connect(host, port)
File "/usr/lib/python2.7/smtplib.py", line 295, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/usr/lib/python2.7/smtplib.py", line 273, in _get_socket
return socket.create_connection((port, host), timeout)
File "/usr/lib/python2.7/socket.py", line 571, in create_connection
raise err
error: [Errno 111] Connection refused
>>>
i think it's because of EMAIL_HOST and EMAIL_PORT, if it is so
where can I find it and how to use it? If it is not can anyone give me an idea on how to send email in django?
thanks in advance...

In your settings.py for your project, make sure you set your EMAIL_HOST, EMAIL_PORT and other settings (like a username or password if your host requires authentication).
Also, make sure you are running the console using the manage.py shell command, and not your normal python shell, so django loads the settings correctly.

You are getting an error because you have no mail transfer agent installed on you system.
You just need to install postfix package in default configuration (it will ask you some questions during install, just choose defaults).
And there is no need to altering your configuration or settings.py or whatever.

You'll need to correctly configure the SMTP email backend for your current host and test it interactively like you just did.
It's possible that they're using TLS or an alternate port--you should look it up in your account settings. Alternatively you can configure Django to use a remote service host, like Gmail, to get yourself started if you have no other options.

You are right, you need to specify correct host and port in settings.py.
What server you are going to use? If it is your hosting provider's server, for example, it is better to contact hosting support to get this info.

Related

Not able to authenticate Askbot with LDP authentication provider

Not able to authenticate Askbot django application on Ubantu 18.04
Able to ping the LDAP server, but still it says - "Can't contact LDAP server"
Error logs shows:-
Traceback (most recent call last): File
"/srv/askbot/venv/local/lib/python2.7/site-packages/askbot/deps/django_authopenid/ldap_auth.py",
line 127, in ldap_authenticate_default
get_attrs File "/srv/askbot/venv/local/lib/python2.7/site-packages/ldap/ldapobject.py",
line 854, in search_s
return self.search_ext_s(base,scope,filterstr,attrlist,attrsonly,None,None,timeout=self.timeout)
File
"/srv/askbot/venv/local/lib/python2.7/site-packages/ldap/ldapobject.py",
line 847, in search_ext_s
msgid = self.search_ext(base,scope,filterstr,attrlist,attrsonly,serverctrls,clientctrls,timeout,sizelimit)
File
"/srv/askbot/venv/local/lib/python2.7/site-packages/ldap/ldapobject.py",
line 843, in search_ext
timeout,sizelimit, File "/srv/askbot/venv/local/lib/python2.7/site-packages/ldap/ldapobject.py",
line 331, in _ldap_call
reraise(exc_type, exc_value, exc_traceback) File "/srv/askbot/venv/local/lib/python2.7/site-packages/ldap/ldapobject.py",
line 315, in _ldap_call
result = func(*args,**kwargs) SERVER_DOWN: {u'info': 'Transport endpoint is not connected', 'errno': 107, 'desc': u"Can't contact LDAP
server"}
Ping uses the ICMP protocol. LDAP uses a TCP connection to (usually) port 389 (or 636 for LDAPS). Those are two different things.
So the fact that you can ping the server only means the server is online. It doesn't mean that the LDAP service is running or that a firewall is not blocking TCP traffic to the right port.
You can test opening a TCP connection to the right port on Ubuntu in one of two ways:
telnet yourserver 389
Or,
nmap yourserver 389
More information on that here.
You may have to install telnet or nmap for those to work. And if you're using LDAPS (LDAP over SSL) then you should test port 636.

Flask app get "IOError: [Errno 32] Broken pipe"

Now I use flask to develop web app.
But at first it works well,after operating web page for a while,the flask back-end shows error like these:
File "/usr/lib64/python2.6/BaseHTTPServer.py", line 329, in handle
self.handle_one_request()
File "/usr/lib/python2.6/site-packages/werkzeug/serving.py", line 251, in handle_one_request
return self.run_wsgi()
File "/usr/lib/python2.6/site-packages/werkzeug/serving.py", line 193, in run_wsgi
execute(self.server.app)
File "/usr/lib/python2.6/site-packages/werkzeug/serving.py", line 184, in execute
write(data)
File "/usr/lib/python2.6/site-packages/werkzeug/serving.py", line 152, in write
self.send_header(key, value)
File "/usr/lib64/python2.6/BaseHTTPServer.py", line 390, in send_header
self.wfile.write("%s: %s\r\n" % (keyword, value))
IOError: [Errno 32] Broken pipe
My app run on port 5000 app.run(debug=True,port=5000),
I use nginx as web server,and set proxy_pass http://127.0.0.1:5000 in nginx config file.
Now I really don't know where is the wrong,I use session['email'] = request.form['email'] and in other file I use email = session.get('email').
Is this usage right? How to set session active period?
or any other reason cause this error ?
then I set app.run(debug=False,port=5000),it shows new error
File "/usr/lib64/python2.6/SocketServer.py", line 671, in finish
self.wfile.flush()
File "/usr/lib64/python2.6/socket.py", line 303, in flush
self._sock.sendall(buffer(data, write_offset, buffer_size))
socket.error: [Errno 32] Broken pipe
why ?
Please help me,thks.
The built-in werkzeug server is not capable of handling the remote end closing the connection while the server is still churing its content out.
instead of app.run(debug=True,port=5000)
try
from gevent.wsgi import WSGIServer
http_server = WSGIServer(('', 5000), app)
http_server.serve_forever()
or if you are using nginx, use it with uwsgi as described here
It is rather a werkzeug issue I would argue

IO Error - Flask-mail and running server with proxy

I'm with a very very wierd bug...
I have a flask app using flask-mail to send email messages.
In a RedHat Server, I tryied using runserver (flask-manager) and gunicorn. So I have a apache server connecting to this app using Proxy.
When I run the app, using any user (root or other), the app runs and it sends emails normally.
But when i close the session with the server (exit in terminal) it stops to send mail and gives me this stack trace:
in send_mail
return mail.send(msg)
File "/usr/local/lib/python2.7/site-packages/flask_mail.py", line 415, in send
with self.connect() as connection:
File "/usr/local/lib/python2.7/site-packages/flask_mail.py", line 123, in __enter__
self.host = self.configure_host()
File "/usr/local/lib/python2.7/site-packages/flask_mail.py", line 144, in configure_host
host.login(self.mail.username, self.mail.password)
File "/usr/local/lib/python2.7/smtplib.py", line 575, in login
self.ehlo_or_helo_if_needed()
File "/usr/local/lib/python2.7/smtplib.py", line 535, in ehlo_or_helo_if_needed
if not (200 <= self.ehlo()[0] <= 299):
File "/usr/local/lib/python2.7/smtplib.py", line 406, in ehlo
self.putcmd(self.ehlo_msg, name or self.local_hostname)
File "/usr/local/lib/python2.7/smtplib.py", line 336, in putcmd
self.send(str)
File "/usr/local/lib/python2.7/smtplib.py", line 320, in send
print>>stderr, 'send:', repr(str)
IOError: [Errno 5] Input/output error
Running with manager:
python myapp.py
Running with gunicorn I use:
gunicorn -w 2 -b 0.0.0.0:8388 myapp:app
I'm really stuck here.. as i tested using 2 different containers... i do not have any other ideias to solve it... using wsgi i could not make it work on this server cause the lib do not install at all =(
any other ideas?
thanks!
Looking at smtplib source (https://hg.python.org/cpython/file/2.7/Lib/smtplib.py#l324), it looks like what's happening is you're trying to write to stderr, which may be the source of the I/O error when running under a server.
If you're setting SMTP(...).debuglevel anywhere, try removing that line.

error 10054 while trying to download binary file through ftp in python 2.7

I'm pretty new to python, so this very well be user error here, but I'm not quite sure what to do from here.
I'm trying to login to a zyxel gs2200 switch via ftp and download it's config file.
The command through cmd ftp is
get config X.log where X.log is whatever you decide to name it.
In python I can log in just fine, but I can not download a file without throwing an exception.
import ftplib
ftp = ftplib.FTP("my.ip.here")
ftp.login('user','Pass')
'230 Logged in'
ftp.retrbinary('RETR config', open('config', 'wb').write)
Traceback (most recent call last):
File "<pyshell#14>", line 1, in <module>
File "C:\Python27\lib\ftplib.py", line 398, in retrbinary
self.voidcmd('TYPE I')
File "C:\Python27\lib\ftplib.py", line 248, in voidcmd
self.putcmd(cmd)
File "C:\Python27\lib\ftplib.py", line 178, in putcmd
self.putline(line)
File "C:\Python27\lib\ftplib.py", line 173, in putline
self.sock.sendall(line)
File "C:\Python27\lib\socket.py", line 222, in meth
return getattr(self._sock,name)(*args)
error: [Errno 10054] An existing connection was forcibly closed by the remote host
I've also tried opening a file before and then just calling it in the method, but it still gives the same error.
does anyone have any idea on how I can get these config files in python?
I Can tell you easy method to do this.
Ftp Login Url: ftp://username:password#hostname/
Username: your Username
Password: Your Password
To download a file from FTP server you could:
import urllib
urllib.urlretrieve('ftp://username:password#hostname/yourconfigname', 'yourconfigname')
In your case you can do something like this.
===============================================================
As for your problem the python error clearly indicating that you did not establish connection correctly(Always Close Connection) or May be your destination had already connected to maximum destinations thats why your target destination closing your connection.

Connecting to Gmail from Python

I've been trying to connect to my Gmail account using python. imap is enabled.
import imaplib
imap_server = imaplib.IMAP4_SSL("imap.gmail.com",993)
# also tried imap_server = imaplib.IMAP4_SSL("imap.gmail.com"), doesnt work.
Traceback is :
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
imap_server = imaplib.IMAP4_SSL("imap.gmail.com",993)
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/imaplib.py", line 1202, in __init__
IMAP4.__init__(self, host, port)
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/imaplib.py", line 172, in __init__
self.open(host, port)
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/imaplib.py", line 1217, in open
IMAP4.open(self, host, port)
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/imaplib.py", line 248, in open
self.sock = self._create_socket()
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/imaplib.py", line 1205, in _create_socket
sock = IMAP4._create_socket(self)
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/imaplib.py", line 238, in _create_socket
return socket.create_connection((self.host, self.port))
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/socket.py", line 435, in create_connection
raise err
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/socket.py", line 426, in create_connection
sock.connect(sa)
OSError: [Errno 65] No route to host
What OSError: [Errno 65] No route to host means is what it say: you can't get to that machine from your machine.
You can test that from outside of Python by opening up a terminal/DOS prompt and typing this:
ping imap.gmail.com
It's possible that this is actually a name lookup error, and you're somehow getting a bad address for imap.gmail.com. So, just to be sure, check by IP address too:
ping 74.125.129.108
ping 74.125.129.109
If ping works, you can check whether your router is for some reason just blocking TCP access to the host, e.g., with:
telnet imap.gmail.com
If it's working, this should either hang for a long time, or give you a connection-refused error; if it gives you a no-route-to-host error, it's the same problem you're seeing.
It's also possible that your router is specifically blocking port 993. You can test this too:
telnet imap.gmail.com 993
If it doesn't come back with something like "Connected to gmail-imap.l.google.com", same problem here too.
At any rate, once you've verified that this is a system or network configuration problem, not a programming problem, go ask for help with your system on the appropriate site.

Categories