Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to send email with Python on Mac or Linux?

Tags:

python

email

I want to send emails with my python script, but unfortunately it's not as straightforward and smooth as php, where I can just use mail() function.

I've used this example:

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

    SUBJECT = "Hello!"

    TEXT = "This message was sent with Python's smtplib."
    server = smtplib.SMTP(SERVER)
    server.sendmail(FROM, TO, message)
    server.quit()

but it only returns a whole bunch of errors I don't even know the meaning of...

Traceback (most recent call last):
  File "mylo.py", line 70, in <module>
    sys.exit(main())
  File "mylo.py", line 66, in main
    send_mail()
  File "mylo.py", line 37, in send_mail
    server = smtplib.SMTP(SERVER)
  File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/smtplib.py", line 239, in __init__
    (code, msg) = self.connect(host, port)
  File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/smtplib.py", line 295, in connect
    self.sock = self._get_socket(host, port, self.timeout)
  File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/smtplib.py", line 273, in _get_socket
    return socket.create_connection((port, host), timeout)
  File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/socket.py", line 512, in create_connection
    raise error, msg
socket.error: [Errno 61] Connection refused

How do I send emails with python?

like image 429
NoobDev4iPhone Avatar asked Nov 30 '11 05:11

NoobDev4iPhone


People also ask

Can you send an email using Python?

Python offers a ` library to send emails- “SMTP lib”. “smtplib” creates a Simple Mail Transfer Protocol client session object which is used to send emails to any valid email id on the internet.

Can Python automate email?

Using automated email in your email campaign helps increase engagement and provide a more tailored email experience for your recipients. For example , one can send emails with log files, result sets , validation data to other team members using email functionality in python.


2 Answers

I rewrote the emailing logic :

#!/usr/bin/python -tt

from email.mime.text import MIMEText
from datetime import date
import smtplib

SMTP_SERVER = "smtp.gmail.com"
SMTP_PORT = 587
SMTP_USERNAME = "[email protected]"
SMTP_PASSWORD = "yourpassword"

EMAIL_TO = ["[email protected]", "[email protected]"]
EMAIL_FROM = "[email protected]"
EMAIL_SUBJECT = "Demo Email : "

DATE_FORMAT = "%d/%m/%Y"
EMAIL_SPACE = ", "

DATA='This is the content of the email.'

def send_email():
    msg = MIMEText(DATA)
    msg['Subject'] = EMAIL_SUBJECT + " %s" % (date.today().strftime(DATE_FORMAT))
    msg['To'] = EMAIL_SPACE.join(EMAIL_TO)
    msg['From'] = EMAIL_FROM
    mail = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
    mail.starttls()
    mail.login(SMTP_USERNAME, SMTP_PASSWORD)
    mail.sendmail(EMAIL_FROM, EMAIL_TO, msg.as_string())
    mail.quit()

if __name__=='__main__':
    send_email()

This is very much configurable script.

like image 193
Yugal Jindle Avatar answered Nov 01 '22 09:11

Yugal Jindle


I suggest you employ mailtools 2 http://pypi.python.org/pypi/mailtools/2

It can send plain text and HTML email. Very easy to use.

like image 2
maguschen Avatar answered Nov 01 '22 09:11

maguschen