简体   繁体   中英

Difficulty sending e-mail using python

I am trying to get Python to send e-mails. First, I started python smptd as follows:

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

Then, adapting the example from https://www.tutorialspoint.com/python/python_sending_email.htm , I have the following script:

#!/usr/bin/python

import smtplib

sender = 'from@fromdomain.com'
receivers = ['brt381@gmail.com']

message = """From: From Person <from@fromdomain.com>
To: To Person <brt381@gmail.com>
Subject: SMTP e-mail test

This is a test e-mail message.
"""

try:
   smtpObj = smtplib.SMTP('localhost:1025')
   smtpObj.sendmail(sender, receivers, message)
   print "Successfully sent email"
except SMTPException:
   print "Error: unable to send email"

When I run this script, I get the message "Successfully sent email". In my other terminal window (the one containing the python smptd), it shows this after running the script:

---------- MESSAGE FOLLOWS ----------
From: From Person <from@fromdomain.com>
To: To Person <brt381@gmail.com>
Subject: SMTP e-mail test
X-Peer: ::1

This is a test e-mail message.
------------ END MESSAGE ------------

But... no e-mail gets sent. Any idea what might be wrong? This works just fine - the e-mail gets sent:

/usr/sbin/sendmail brt381@gmail.com <<<"hello"

You are actually sending (in the first example) your email through a local development smtp server, because you mention the DebuggingServer class which, as the documention says :

Create a new debugging server. Arguments are as per SMTPServer. Messages will be discarded, and printed on stdout.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM