Send an email directly by pulling an MX record from your email address in Python

Overview

I wanted to make a program that doesn't require specifying a mail server, so I wrote it.

First, insert the module for pulling MX records.

pip install dnspython

Run

Pass the recipient and sender's email address as arguments.

python sendmail.py [email protected] [email protected]

Implementation

sendmail.py

# -*- coding: utf-8 -*-

import smtplib
from email.MIMEText import MIMEText
from email.Header import Header
from email.Utils import formatdate
import sys
import dns.resolver

if __name__ == "__main__":

    argvs = sys.argv

    if len(argvs) != 3:
        print 'Usage arg1 = to_address, arg2 = from_address'
        quit()

    to_address   = argvs[1]
    from_address = argvs[2]
    to_list = to_address.split('@')

    to_address = '<' + to_address + '>'
    from_address = '<' + from_address + '>'

    if len(to_list) < 2:
        print 'format error to_address'
        quit()

    domain = to_list[1]
    mailserver = dns.resolver.query(domain, 'MX')

    if len(mailserver) < 1:
        print 'not found mx recored'
        quit()

    mailserver = mailserver[0].to_text().split(' ')
    mailserver = mailserver[1][:-1]
    print mailserver

    charset = "ISO-2022-JP"
    subject = u"this is Python test mail!"
    text    = u"this is Python test mail!"

    msg = MIMEText(text.encode(charset),"plain",charset)
    msg["Subject"] = Header(subject,charset)
    msg["From"]    = from_address
    msg["To"]      = to_address
    msg["Date"]    = formatdate(localtime=True) 

    smtp = smtplib.SMTP(mailserver)
    smtp.sendmail(from_address,to_address,msg.as_string())
    smtp.close()

It seems that Gmail has some restrictions on anti-spam measures, and if you send it, you may get the following error.

smtplib.SMTPServerDisconnected: Connection unexpectedly closed: [Errno 54] Connection reset by peer

Recommended Posts

Send an email directly by pulling an MX record from your email address in Python
Send an email to Spushi's address with python
Send an email with Excel attached in Python
Send email in Python (Outlook)
[Python] Send an email from gmail with two-step verification set
[Python] Send an email with outlook
Send an email to a specific email address with python without SMTP settings
Send email to multiple recipients in Python (Python 3)
Send an email with Amazon SES + Python
Get your own IP address in Python
Send an email from the VirtualBox CentOS 8 server using your Google account as the sending address and using the app password
To automatically send an email with an attachment using the Gmail API in Python
Email attachments using your gmail account in python.
Use Python in your environment from Win Automation
[Python] Send email
[Python] Send email
View images in OpenCV from Python using an external USB camera on your MacBook
Import classes in jar files directly from Python scripts
Send email with Python
Send Gmail in Python
Get your heart rate from the fitbit API in Python!
I tried sending an email from Amazon SES with Python
I tried to make it possible to automatically send an email just by double-clicking the [Python] icon