Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending an email from python using smtpd.DebuggingServer as STMP server

I'm trying to send an email using Python and used the following code:

import smtplib
import datetime

SERVER = "localhost"
PORT = 1025

FROM = "[email protected]"
TO = ["[email protected]"]

SUBJECT = "test"

dt = datetime.datetime.now()
TEXT = "blabla bla @ " + str(dt)

message = """\
From: %s
To: %s
Subject: %s

%s
""" % (FROM, ",".join(TO), SUBJECT, TEXT)

server = smtplib.SMTP(SERVER, PORT)
server.sendmail(FROM, TO, message)
server.quit()

Not having any STMP server already installed/setup, I simply used this:

python -m smtpd -n -c DebuggingServer localhost:1025

The code seems to run fine, no errors, and the server even notifies me with this:

---------- MESSAGE FOLLOWS ----------
From: [email protected]
To: [email protected]
Subject: test
X-Peer: 127.0.0.1

blabla bla @ 2014-01-29 14:44:37.219724
------------ END MESSAGE ------------

'[email protected]' is, of course, a representation of a real, existing email address while '[email protected]' is made up.

But no email arrives at [email protected]...

Am I missing something obvious here?

I read somewhere (sorry but cannot find it anymore) that services likes gmail may well block emails coming from non-static IP addresses. Could that be what is going on here?

like image 341
dm76 Avatar asked Jan 29 '14 15:01

dm76


1 Answers

According to python documentation on the smtpd module:

class smtpd.DebuggingServer(localaddr, remoteaddr)
    Create a new debugging server. Arguments are as per SMTPServer. 
    Messages will be discarded, and printed on stdout.

So the module doesn't actually send an email. It prints it in the terminal.

like image 180
Aaron Lam Avatar answered Nov 16 '22 10:11

Aaron Lam