formigol Posted January 14, 2018 Posted January 14, 2018 Hello! I am using django on Tommy. In one of my django apps I would like to connect to my google mail trough "smtp.gmail.com",587 and send some emails. When I try to do this the following error arise: Exception Type: OSError Exception Value: [Errno 101] Network is unreachable I think that is a firewall issue. Is possible to solve this problem? the script that I wrote is the following: #! /usr/bin/python # -*- coding: utf-8 -*- from __future__ import unicode_literals import smtplib from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from .models import Story from django.utils import timezone def send_mail(html, destination): gmail_pwd = '********' # me == my email address # you == recipient's email address me = "colouredsweat@gmail.com" smtpserver = smtplib.SMTP("smtp.gmail.com",587) smtpserver.ehlo() smtpserver.starttls() smtpserver.ehlo smtpserver.login(me, gmail_pwd) header = 'To:' + destination.email + '\n' + 'From: ' + me + '\n' + 'Subject:Coloured Sweat \n' #print header # Create message container - the correct MIME type is multipart/alternative. msg = MIMEMultipart('alternative') msg['Subject'] = "Coloured Sweat" msg['From'] = me msg['To'] = destination.email # Create the body of the message (a plain-text and an HTML version). text = 'hey' # Record the MIME types of both parts - text/plain and text/html. part1 = MIMEText(text, 'plain') part2 = MIMEText(html, 'html') # Attach parts into message container. # According to RFC 2046, the last part of a multipart message, in this case # the HTML message, is best and preferred. msg.attach(part1) msg.attach(part2) smtpserver.sendmail(me, destination.email, msg.as_string()) #print 'done!' smtpserver.close()
Krydos Posted January 14, 2018 Posted January 14, 2018 Port 587 to smtp.gmail.com is not blocked in the firewall: # telnet smtp.gmail.com 587 Trying 108.177.98.108... Connected to smtp.gmail.com. Escape character is '^]'. 220 smtp.gmail.com ESMTP u67sm54542445pfd.162 - gsmtp ^] telnet> quit Connection closed.
wolstech Posted January 15, 2018 Posted January 15, 2018 Despite it working from telnet, we seem to get this problem with Gmail a lot. Gmail's servers do accept our connections, and we don't block them on our end, but my experience is that it won't actually allow our servers to send mail from a script. I'm not sure if they're blocking us, if there's a setting that needs to be changed on gmail's end, or what. Nobody's ever gotten it to work though. Here's two others for the same issue that were never resolved: https://www.helionet.org/index/topic/31306-cannot-send-email-via-googleoutlookmailgun-smtp-via-phpmailer/ https://www.helionet.org/index/topic/30127-could-not-connect-to-smtp-host-smtpgooglecom-port587/
formigol Posted January 19, 2018 Author Posted January 19, 2018 Hello! Good news for django users! The django smtp built in library works! Here is the link to the documentation:https://docs.djangoproject.com/en/2.0/topics/email/#django.core.mail.backends.smtp.EmailBackend
Krydos Posted January 19, 2018 Posted January 19, 2018 Thanks for sharing. We've had a few people with the port 587 smtp issue.
miwilc Posted January 20, 2018 Posted January 20, 2018 It needs be enabled in settings: https://myaccount.google.com/lesssecureapps
formigol Posted January 20, 2018 Author Posted January 20, 2018 The Google settings were not enough for me. The only way I found to solve the problem was the django library.
wolstech Posted January 21, 2018 Posted January 21, 2018 @miwilc: For whatever reason, the SMTP support on our servers has been known to be incompatible with Gmail (and Outlook.com as well) for some reason, even if you turn on the insecure connections option (which oddly is also required for Outlook 2016 and the stock iOS mail app...I suspect it's more about making people use Google's apps/websites). This comes up a lot. PHP and Python can't talk to those two, yet they work with every other mail provider in existence. Django must implement it differently because that one works when the others don't...
vukosyst Posted January 22, 2018 Posted January 22, 2018 It seems like this is why the e-mail functionality in our servlet is not working (on Tommy). Did you get in touch with Google/team Gmail to hear why they're not transporting emails that were sent from a Heliohost domain?
wolstech Posted January 22, 2018 Posted January 22, 2018 There's nobody to talk to. Google (like most gigantic tech companies, Microsoft is also known for this...) has virtually 0 technical support for this sort of issue. We don't have any idea what these two companies do differently that breaks them when everyone else's mail service works fine with our servers. The odd part is they have no problem receiving mail from us, if I send email to gmail account using PHP mail(), I get it just fine, but they will not relay our mail. We can open a connection to them via telnet and get the hello response without issue, so they didn't block us. Django's SMTP library reportedly does work with them, though it's the only one I know of that works with gmail from our servers. It probably does something differently from the others, so it doesn't have whatever incompatibility breaks most SMTP libraries.
vukosyst Posted January 27, 2018 Posted January 27, 2018 (edited) [edit] moved post to new topic [/edit] Edited January 29, 2018 by vukosyst
vukosyst Posted January 29, 2018 Posted January 29, 2018 So, topicstarter and everyone reading, please have a look at this topic: https://www.helionet.org/index/topic/31605-cant-send-e-mails-from-servlet/ Sending e-mails through Gmail SMTP now works, at least from my Java servlet!!!
wolstech Posted January 29, 2018 Posted January 29, 2018 This issue with gmail and other mail providers has been fixed.
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now