Django: Outlook email smtp timeout in production server - python

i'm trying to send a SMTP email from Django, using my credentials of Outlook. My code works in localhost, but when I upload my code to production server, it doesn't.
If I use my Gmail credential, it also works in production, but it doesn't with Outlook. So, I think Outlook is configured in a different way, but I dont know.
This is my view code:
def send_my_custom_email():
connection = mail.get_connection(
host = 'smtp-mail.outlook.com',
port = 25,
username = 'myemail#outlook.com',
password = 'mypassword' ,
)
connection.open()
email2send = mail.EmailMessage('hello', 'hello', 'myemail#outlook.com', to=['receiveremail'], connection=connection)
email2send.send()
connection.close()
I know that my configuration setting are right because it can send emails from localhost. These are my settings.py:
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_USE_TLS = True
I already try to check the Outlook settings, but I couldn't find anything about SMTP use.
My exact questions are:
Outlook need aditional settings in production?
The problem is in my code or in Outlook settings?
Why it works in localhost but it does not in production server?

Outlook.com only allows encrypted SMTP TLS connections on port 587. It does not even listen on port 25, that is why you get a timeout.

Related

Django Password Recovery via Gmail [Errno 110] Connection timed out

I had a password recovery system via email in my web app, which was working perfectly before deployment. However, now that I have succesfully deployed, password recovery is the only feature that is not working, when solicited it stays loading for some time until the error TimeoutError at /password-reset/ [Errno 110] Connection timed out
Here is my settings.py file email configuration:
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_USER = config.get('EMAIL_USER')
EMAIL_HOST_PASSWORD = config.get('EMAIL_PASS')
The credentials are tucked away into config file.
What could be causing this issue?
Does sending emails other than the password reset work? If yes, what kind of email?
What platform is the app deployed on?
What service are you using to send emails?
I added the port to my UFW firewall with sudo ufw allow 587. Also since I deployed through Linode (they currently have port 587 blocked for all users to prevent spam) I had to contact Linode Support, who then lifted the ban of port 587 on my Linode.

using docmd to receive queue-id of a mail from smtp server

I have written a code in jython 2.5, which uses the smtplib sendmail to send mail over a smtp server. Below is a simple snippet of the code
mail = MIMEMultipart('mixed')
mail['Subject'] = mail_subject
mail['FROM']=UstrSender
mail['To']=UstrReceivers
mail['Cc']=UstrCC
mail_p2=MIMEText(mail_html, 'html', 'utf-8')
mail.attach(mail_p2)
#Connection to SMTP
#Enter SMTP Server Details, In case your server do require authentication modify authentication parameter below and uncomment
s = smtplib.SMTP(smtpserver)
#s.sendmail(UstrSender, [UstrReceivers, UstrCC], mail.as_string())
sendmail_return = s.sendmail(UstrSender, [UstrReceivers, UstrCC], mail.as_string())
Now, the smtp server mentioned is a cluster of 3 individual servers and there is a lag at times on one of these servers because of a long queue of requests. To identify such issues and the culprit server, need to have a generic script to identify the queue id of the message being sent. Kindly, help on the same, if using docmd we can create such a command to get the queue id of the submitted email.
I tried getting a response from sendmail itself, but since the mail is eventually sent, there is no return from the command.
Thanks,
Dev

How to send a mail using gmail in Django 1.10 on windows [duplicate]

I registered a domain and a private email using namecheap.com. I am trying to send an email from this private email. However, I get the error in the title.
In my settings.py, I have these settings:
EMAIL_HOST = 'mail.privateemail.com'
EMAIL_HOST_USER = 'contact#mysite.com'
EMAIL_HOST_PASSWORD = 'my password'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
DEFAULT_FROM_EMAIL = EMAIL_HOST_USER
And I am trying to send the email through a view:
send_mail(
'Subject here',
'Here is the message.',
'contact#mysite.com',
['myname#gmail.com'],
fail_silently=False,
)
However, I get this error:
SMTPException at /
STARTTLS extension not supported by server.
Any idea why? Any help is appreciated.
EDIT
After changing the EMAIL_USE_TLS to False, and also removing it to check both separately, I get this error now:
SMTP AUTH extension not supported by server.
Any idea why? Thanks!
your server mail.privateemail.com does not know what is STARTTLS SMTP Commnad is
this may happen in two cases:
your server (mail.privateemail.com) do not support secure communication at all and you need to use plain, non-encrypted communication.
you are trying to connect to the port that is already using SSL as the communication, then STARTTLS to upgrade connection to secure make no sense whatsoever.
your server is configured improperly and ignores STARTTLS on submission port (587).
Judging, that you are connecting to port 587 which should provide plain communication - it's either 1 or 3.
If you want this just work, remove EMAIL_USE_TLS = True or set it to False, otherwise - SMTP server configuration should be fixed.
You may try SSL instead of TLS by making following changes in settings.py
EMAIL_USE_SSL = True
EMAIL_PORT = 465
hope that helps
Either setup TLS on your mail server or use EMAIL_USE_TLS = False.
I am able to resolve the issue with modifying below line of code, by adding port number with server name:
server = smtplib.SMTP('mail.mycompany.com:587')
Not sure if you have solved the problem yet. I also recently try the 2-month free private email package from NameCheap. Here is my code and it works for me as of Jan 2018:
import smtplib
from email.message import EmailMessage
fromaddr = 'account#yourdomain.com'
toaddrs = "recipient#somedomain.com"
SMTPServer = 'mail.privateemail.com'
port = 465 #587
login = "account#yourdomain.com"
password = "password"
msg = EmailMessage()
msgtxt = "http://www.google.com"+"\n\n"+"This is a test."
msg.set_content(msgtxt)
msg['Subject'] = "Test message"
msg['From'] = fromaddr
msg['To'] = toaddrs
server = smtplib.SMTP_SSL(SMTPServer, port) #use smtplib.SMTP() if port is 587
#server.starttls()
server.login(login, password)
server.send_message(msg)
server.quit()
Hope this help!
PS. You can also use port 587, but you have to use smtplib.SMTP() instead of smtplib.SMTP_SSL(), and also have to un-comment the server.starttls() line.

How to send authenticated email using Python?

I have a python function to send an email notification. I have included the login call to authenticate with my local SMTP server, however emails are being returned stating '553-mail rejected because your IP is in the PBL'. Further reading on https://www.spamhaus.org/pbl reveals that apparently I am not being prevented from sending email, I am simply required to authenticate first.
I have tried base64 encoding to avoid sending the password as plain text without success.
I have also looked at the Pysasl library, but I am a little unsure how I might use this for authenticating with my SMTP server.
Would anyone have any guidance as to the correct use of either base64 encoding, the pysasl library or if a better method exists for satisfying the correct authentication requirements?
My code is below.
def emailNotify(self,userid):
SMTPserver = 'localhost'
sender = '***' # blanked
username = '***' # blanked
receiver = '***' # blanked
pwd = '***' # blanked
text_subtype = 'plain'
msg = 'requested data was posted to dashboard for '+userid
subject = 'confirmation of POST\'d data for '+userid
try:
msg = MIMEText(msg, text_subtype)
msg['Subject'] = subject
msg['From'] = sender
conn = smtplib.SMTP(SMTPserver)
conn.login(username, pwd)
try:
conn.sendmail(sender, receiver, msg.as_string())
finally:
conn.quit()
except:
print('message sending failed')
Thanks in advance.
You have 2 ways of building a program for sending mail:
build a bullet proof solution:
analyze what server announces as its capabilities
choose the one that better meets your requirement
use that authentication method
actually send the mail
simply that means that you have to implement code for reading and decoding what the server returns (which can be weird) and also all various authentication methods, not speaking of TLS
use a custom connection method adapted to that server
read the documentation of the server to know what it declares to support
test it manually first through a mere telnet connection then in an interactive Python session (idle is enough here, but you can choose your prefered Python shell)
carefully program the way you have just tested - but leave relevant error messages in cases the server capabilities change...
IMHO much simpler...

Errno 10060 A connection attempt failed

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST='smtp.gmail.com'
EMAIL_PORT=465
EMAIL_HOST_USER = 'yogi'
EMAIL_HOST_PASSWORD = '###'
DEFAULT_EMAIL_FROM = 'yogi#gmail.com'
above are the settings for django core mail module. I am using its send_mail to send mails to users. When i try to build the program with the gmail smtp it throws the following error
'Errno 10060 A connection attempt failed because the connected party
did not properly respond after a period of time, or established
connection failed because connected host has failed to respond'.
I am doing this in my company and so it has proxy settings. I have given the proxy credentials in .condarc settings file. But still the connection timeout error. Do i need to set the proxy settings somewhere else or let me know where i am going wrong. ?
As far as I know django does not detect any SMTP proxy settings from anaconda configuration files. You can overcome this by manually building a connection.
Notice that send_mail , has an option parameter for a connection. You get one by calling mail.get_connection now you need to wrap it around sockspi
see Python smtplib proxy support and Python send email behind a proxy server for further details.

Categories