Sending email with python without an MTA

I always found puzzling that to send simple notification emails all examples I could find on the web required a valid account on an MTA and hard-coding your credentials in the script or some other dependencies that seemed a bit non-straight forward to me.

This is an alternative method that sends directly an email without intermediaries. First, you’ll need to install the dnspython package:

pip install dnspython

Now, this is the script that takes care of sending email:

#!/usr/bin/env python2.7
 
import sys
import smtplib
import dns.resolver
 
answers = dns.resolver.query('dest-server.com', 'MX')
if len(answers) <= 0:
    sys.stderr.write('No mail servers found for destination\n')
    sys.exit(1)
 
# Just pick the first answer
server = str(answers[0].exchange)
 
# Add the From: and To: headers
fromaddr = 'source-email@whatever.com'
toaddr = 'destination@dest-server.com'
body = 'some email body'
msg = "From: {}\r\nTo: {}\r\n\r\n{}".format(fromaddr, toaddr, body)
 
server = smtplib.SMTP(server)
server.set_debuglevel(1)
server.sendmail(fromaddr, toaddr, msg)
server.quit()

This script resolves the mail server DNS record for the given destination (you could use a parameter instead of hard-coding it if you want to make it more generic). Once it finds out the mail server it directly tries to contact the server and send the email there using the usual basic SMTP commands (ehlo, mail FROM:, rcpt TO: etc). No need for intermediaries for such simple cases 🙂

There are surely a bunch of more complicated scenarios this script does not handle, but, it fits nicely when you need to send emails to your sysadmin account from cron jobs on random servers in your infrastructure.

This entry was posted in python-dev. Bookmark the permalink.

4 Responses to Sending email with python without an MTA

  1. imdadhussain says:

    I have try your just to send mail for notification.
    It give me error
    SMTPRecipientsRefused: {‘my email id ‘: (550, “Mail rejected by for policy reasons. We generally do not accept email from dynamic IP’s as they are typically used to deliver unauthenticated SMTP e-mail to an Internet mail server. http://www.spamhaus.org maintains lists of dynamic and residential IP addresses. If you are not an email/network admin please contact your E-mail/Internet Service Provider for help. Email/network admins, please contact for email delivery information and support”)}

  2. Christian says:

    Good idea!
    However, i guess this only works when:
    a) Your mailserver at ‘dest-server.com’ does not check for a valid certificate for the sending MTA?
    b) Your mailserver at ‘dest-server.com’ does not demand TLS encryption?
    c) You don’t send your emails to ‘gmail.com’ or ‘yahoo.com’ or any other big MTA, that demands the above mentioned points?

    Would be interesting to have an example that can deal with these “advanced” features.

  3. moy says:

    @Brenden, glad that was of use to you. Cheers.

  4. Brenden says:

    This is exactly what I needed. I just wanted a python program to be checking the status of a web service, and to send an email if it couldn’t connect properly. Thanks!!!

Leave a Reply

Your email address will not be published. Required fields are marked *

*