Reputation: 69
Email cannot be sent in Python.I want to send an email by using Gmail. I wrote codes in views.py like
def mail(mail_adress, docx):
msg = MIMEMultipart()
msg["Subject"] = "TEST"
msg["From"] = settings.EMAIL_HOST_USER
msg["To"] = email
message = "TEST"
body = MIMEText(message)
msg.attach(body)
mime = {'type': 'text', 'subtype': 'docx'}
attach_file = {'name': 'test.docx', 'path': '/Users/test.docx'}
attachment = MIMEBase(mime['type'], mime['subtype'])
file = open(attach_file['path'], 'rb')
attachment.set_payload(file.read())
file.close()
encoders.encode_base64(attachment)
msg.attach(attachment)
attachment.add_header("Content-Disposition", "attachment", filename=attach_file['name'])
return msg
def send(from_addr, to_addrs, msg):
smtp = smtplib.SMTP()
smtp.connect()
from_addr = '[email protected]'
from_addr_name = "test"
to_addr= "[email protected]"
subject = u"test"
message = "test"
body = MIMEText(message)
mime = {'type': 'text', 'subtype': 'docx'}
attach_file = {'name': 'test.docx', 'path': '/Users/test.docx'}
msg = mail(from_addr, from_addr_name, to_addr, subject, body, mime, attach_file)
smtp.sendmail(from_addr, to_addrs, msg)
smtp.close()
send()
in settings.py
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = '[email protected]'
EMAIL_HOST_PASSWORD = 'xxxxxxxxx'
EMAIL_USE_TLS = True
When I run the codes, [Errno 61] Connection refused error happens.Traceback says
Traceback (most recent call last):
File "/Users/xxxx/anaconda3/envs/py36/lib/python3.6/site-packages/django/core/handlers/exception.py", line 35, in inner
response = get_response(request)
File "/Users/xxxx/anaconda3/envs/py36/lib/python3.6/site-packages/django/core/handlers/base.py", line 128, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/Users/xxxx/anaconda3/envs/py36/lib/python3.6/site-packages/django/core/handlers/base.py", line 126, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/xxxx/Downloads/legal_doc_mail/common/views.py", line 400, in make_document
send()
File "/Users/xxxx/Downloads/legal_doc_mail/common/views.py", line 454, in send
smtp.connect()
File "/Users/xxxx/anaconda3/envs/py36/lib/python3.6/smtplib.py", line 336, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/Users/xxxx/anaconda3/envs/py36/lib/python3.6/smtplib.py", line 307, in _get_socket
self.source_address)
File "/Users/xxxx/anaconda3/envs/py36/lib/python3.6/socket.py", line 724, in create_connection
raise err
File "/Users/xxxx/anaconda3/envs/py36/lib/python3.6/socket.py", line 713, in create_connection
sock.connect(sa)
ConnectionRefusedError: [Errno 61] Connection refused
I really cannot understand why such an error happens.What is wrong in my codes?How should I fix this?
Upvotes: 1
Views: 3999
Reputation: 1494
Settings.py
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = '[email protected]'
EMAIL_HOST_PASSWORD = 'xxxxxxxxx'
EMAIL_USE_TLS = True
EMAIL_USE_SSL = False
Go to your Google Account settings, find Security -> Account permissions -> Access for less secure apps, enable let less secure apps sign in option. https://support.google.com/accounts/answer/6010255
[Errorno 61] is due to connection refused by server. See if this could help.
Upvotes: 2