Sending Email With Attachment

I got a question about attaching files to an EmailMessage. I realize that this might not be a huge pain for a lot of people, but it might not be obvious how to do this correctly.

A long time ago, I wrote a JIRA clone. The code for it can be found below on my GitHub account.

But, I’m using it because JIRA email is integral to how it works.

This was written with Django 1.4 so take this with a grain of salt, but it looks more or less like this:

1
2
3
4
5
6
7
8
9
from django.core.mail.message import EmailMessage

class EmailServices(object):
    # ...
    def sendEmailWithAttachment(self, send_from, send_to, subject, body, files=[]):
        msg = EmailMessage(subject, body, send_from, [send_to])
        for f in files:
            msg.attach_file(f)
        msg.send()

And you can see the entire class here. You can take what you need.

comments powered by Disqus