-1

I'm trying to send emails programmatically with Python. The emails stay in the mail queue (mailq). Details are below.

My diagnosing abilities have reached their limit. Perhaps the issue is my ISP blocking outgoing emails from my (dynamic) IP address. Perhaps Gmail and Yahoo are expecting a different port or protocol.

Technical setup:

  • Debian 4.8.4-1
  • exim4 (reconfigured using # dpkg-reconfigure exim4-config; how do I reset to defaults?)
  • Python 2.7.9
  • Debian PC --> router --> WAN

Python emailer script

#!/usr/bin/env python

import cgi
from email.MIMEImage import MIMEImage
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
import mimetypes
import multiprocessing
import os
import smtplib
import sys
import traceback

to_addr = "<me>@gmail.com"
from_addr = "<me>@gmail.com"
body_text = "auto email test"
message = MIMEText(body_text)
message["Subject"] = "subject"
message["From"] = from_addr
message["To"] = to_addr
message["Return-Path"] = "<>"
message["Auto-Submitted"] = "auto-generated"
smtp = smtplib.SMTP("localhost")
#smtp = smtplib.SMTP("localhost", 587)
#smtp = smtplib.SMTP("localhost", 666)
#smtp = smtplib.SMTP("localhost", 25)
try:
    print("sending email")
    smtp.sendmail(from_addr, [to_addr], body_text)
    print("sent email")
except Exception, em:
    print("ERROR: " + str(em) )
except SMTPException, em:
    print("ERROR: " + str(em) )
smtp.quit()

Python script output (no port specified, or port 25 specified)

root@deb:/****/****/****# python emailer.py
sending email
sent email

Here is the email, stuck in the mail queue:

root@deb:/****/****/****# mailq
 5m   266 1aFMdt-0002d6-KW <>
          <me>@gmail.com

Python script output (using port 587, or port 666) ~ In this case, mailq is empty

root@mypc:/****/****/****# python emailer.py
Traceback (most recent call last):
  File "emailer.py", line 51, in <module>
    smtp = smtplib.SMTP("localhost", 666)
  File "/usr/lib/python2.7/smtplib.py", line 256, in __init__
    (code, msg) = self.connect(host, port)
  File "/usr/lib/python2.7/smtplib.py", line 316, in connect
    self.sock = self._get_socket(host, port, self.timeout)
  File "/usr/lib/python2.7/smtplib.py", line 291, in _get_socket
    return socket.create_connection((host, port), timeout)
  File "/usr/lib/python2.7/socket.py", line 571, in create_connection
    raise err
socket.error: [Errno 111] Connection refused

exim4 log output (/var/log/exim4/mainlog)

2016-01-02 08:54:49 1aFMdt-0002d6-KW <= <> H=localhost (deb.home) [127.0.0.1] P=esmtp S=266
2016-01-02 08:56:57 1aFMdt-0002d6-KW gmail-smtp-in.l.google.com [74.125.203.26] Connection timed out
2016-01-02 08:56:57 1aFMdt-0002d6-KW == <me>@gmail.com R=dnslookup T=remote_smtp defer (110): Connection timed out
  • Try sending a mail manually like suggested by vonbrand below. If you get your mail out this way, you can look into you python script. If you don't manage to get it out, you should get a proper error message. – michas Jan 2 '16 at 15:28
3

You can check if you have access to the mail relay by doing e.g.

nc mailrelay 25

I'd strongly suspect your ISP blocks outgoing mail traffic (there is just way too much spam on the net already, by some estimates fully 95% of all email traffic)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.