Jump to content

[Solved] Unable to connect to smtp - "smtp.gmail.com",587


Recommended Posts

Posted

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()
Posted

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.
Posted

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/

Posted

@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...

Posted

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?

Posted

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.

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...