0

I would like to know "what is the best way to send an e-mail with attachment through a python script"? Should i use "subprocess" and take this command line

"mail -s "Test" email@googlemail.com < file.txt"

Another option would be smtplib https://docs.python.org/2/library/email-examples.html ?

Which option is better?

Anand S Kumar
  • 88,551
  • 18
  • 188
  • 176
Loretta
  • 154
  • 15
  • 1
    i suggest you use the library (smtplib). this way your script will run independently of the platform you run it on. – hiro protagonist Jul 13 '15 at 07:44
  • your command reads the email message to be send from `file.txt`. There is no indication that it sends an email with an attachment. – jfs Jul 14 '15 at 23:42
  • related: [How to send Email Attachments with python](http://stackoverflow.com/q/3362600/4279) – jfs Jul 14 '15 at 23:42

2 Answers2

2

Python would be the more flexible way of sending an email. And also as you are using Python already, would be the most sensible. No need to use subprocess to call external scripts (which can be insecure).

You can attach more files of different types, control the content of the body better. If you want you can turn it into a generic function or class that can be given the text and filenames, recipients etc.

If you have a class like the above you can import it into other programs where it could be used for debugging, or sending flags when errors occur (or something interesting happens).

I tend to use it to notify me of the health of some automated process I run.

Also as @hiroprotagonist has mentioned - this will make the script platform independent.

This slightly slimmed down example from the doc you linked is all you really need to know:

# Import smtplib for the actual sending function
import smtplib
# Here are the email package modules we'll need
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart

# Create the container (outer) email message.
msg = MIMEMultipart()
msg['Subject'] = 'Subject'
me = 'email@email.com'
recipient = 'recipient@recipient.com'
msg['From'] = me
msg['To'] = recipient

# Assume we know that the image files are all in PNG format
for file in pngfiles:
    # Open the files in binary mode.  Let the MIMEImage class automatically
    # guess the specific image type.
    fp = open(file, 'rb')
    img = MIMEImage(fp.read())
    fp.close()
    msg.attach(img)

# Send the email via our own SMTP server.
s = smtplib.SMTP('localhost')
s.sendmail(me, recipient, msg.as_string())
s.quit()
NDevox
  • 4,056
  • 4
  • 21
  • 36
0

The whole purpose of yagmail (I'm the developer) is to make it really easy to send emails, especially with HTML or attachment needs.

Please try the following code:

import yagmail
yag = yagmail.SMTP('email@email.com', 'password')
contents = ['See my attachment below', '/home/you/some_file.txt']
yag.send('recipient@recipient.com', subject = 'Subject', contents = contents)

Notice the magic here: contents is a list, where an item equal to a file path will automatically be loaded, mimetype guessed, and attached.

There's a lot more magic involved, such as easy to embed images, passwordless scripts, usernameless scripts, easy aliases, smart defaults and much more. I advise/encourage you to read its github page :-). Feel free to raise issues or add feature requests!

You can get yagmail by using pip to install it:

pip install yagmail # Python 2
pip3 install yagmail # Python 3
PascalVKooten
  • 20,643
  • 17
  • 103
  • 160