Question

I've been beaten by Bluehost. I'm doing something similar to this, except with smtp.gmail.com:587 instead of IMAP.

Works beautifully from terminal (running locally), but I wanted to automate it as a cron job. It failed silently tonight, so I tried via SSH, which is one I found the above problem - socket.error: [Errno 101] Network is unreachable.

I have a shared hosting plan, but Bluehost say that even with a dedicated IP they can only open ports >=1024.

Am I stuck, there is no way to do this? Any ideas for some kind of hacky work around where Python doesn't send an email, but signals something else to send an email..?

Bluehost can send an email when a cron job completes - any way to pass it a variable from Python, so that it can do the mailing for me?

Was it helpful?

Solution

Bluehost isn't allowing scripts access to other ports than 80 and 443 with a standard plan. Since your script tries to use port 587 it simply won't work. Check here for more details about Bluehosts policy: help page on Bluehost

One suggestion would be for you to use another email service that allows sending emails via another port, i.e. HTTP. mailgun is one provider that offers this service.

OTHER TIPS

You can use the smtp server that BlueHost provides you:

#!/usr/bin/env python
# email functions
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from email.MIMEImage import MIMEImage
import smtplib  
from email.Utils import COMMASPACE, formatdate

lines = ''
lines =  r'<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">'
lines += r'<html xmlns="http://www.w3.org/1999/xhtml">'
lines += r'<h1>Hi!'

yourSmtp = 'mail.yourDomain.com'
fromaddr = 'yourEmailName@yourDomain.com'  
password = 'yourPassword'
toaddrs  = ['whoEver@gmail.com']

msg = MIMEMultipart('alternative')
msg['Subject'] = 'Hi'
msg['From'] = fromaddr
msg['Date'] = formatdate(localtime=True)
# Record the MIME types of both parts - text/plain and text/html.
part1 = MIMEText("text", 'plain')
part2 = MIMEText(lines, '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)

server = smtplib.SMTP(yourSmtp,26)
server.set_debuglevel(0)
server.ehlo(yourSmtp)
server.starttls()
server.ehlo(yourSmtp)
server.login(fromaddr,password)
for toadd in toaddrs:
   msg['To'] = toadd
   server.sendmail(fromaddr, toadd, msg.as_string())  
server.quit()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top