Jump to content

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


formigol

Recommended Posts

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()
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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/

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...