Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attachments added to the end of the message (backward compatible version) #8

Merged
merged 1 commit into from
Jan 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# drymail
Makes sending emails easy and [DRY](https://en.wikipedia.org/wiki/Don%27t_repeat_yourself) — For Python 3.
Makes sending emails easy and [DRY](https://en.wikipedia.org/wiki/Don%27t_repeat_yourself) — For Python 3.

[![PyPI version](https://badge.fury.io/py/drymail.svg)](https://badge.fury.io/py/drymail)

Expand All @@ -9,10 +9,9 @@ __Drymail__ is a minimalist wrapper over Python’s existing [smtplib](https://d
from drymail import SMTPMailer, Message

client = SMTPMailer(host='smtp.email.com', user='johndoe', password='password', tls=True)
message = Message(subject='Congrats on the new job!', sender=('John Doe', 'john@email.com'),
message = Message(subject='Congrats on the new job!', sender=('John Doe', 'john@email.com'),
receivers=[('Jane Doe', 'jane@message.com'), 'jane.doe@mail.io'], text='When is the party? ;)')
with open('congrats.pdf', 'rb') as pdf_file:
message.attach(filename='congrats.pdf', data=pdf_file.read(), mimetype='application/pdf')
message.attach(filename='/path/to/congrats.pdf', mimetype='application/pdf')

client.send(message)
```
Expand Down
38 changes: 26 additions & 12 deletions drymail.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import mistune
from bs4 import BeautifulSoup

from os.path import basename

class SMTPMailer:
"""
Expand Down Expand Up @@ -258,6 +258,7 @@ def __init__(self, sender, receivers, subject=None, authors=None, cc=None, bcc=N
self.text = text or ''
self.html = html or ''
self.__attachments = []
self.__attachments_data = []
self.prepared_message = prepared_message
self.prepared = False
self.message = MIMEMultipart('mixed')
Expand All @@ -279,33 +280,42 @@ def attachments(self):
"""
return self.__attachments

def attach(self, data, filename, mimetype=None):
def attach(self, filename, data=None, mimetype=None):
"""
Add a file as attachment to the email.

Parameters
----------
data: bytes
The raw content of the file to be attached.
filename : str
The name of the file to be attached.
If data is provoded:
The filename of the file to be attached.
If data not provoded:
The full name (path + filename) of the
file to be attached.
data: bytes, optional
The raw content of the file to be attached.
mimetype : str, optional
The MIMEType of the file to be attached.
"""
if self.prepared_message:
return

filename_only = basename(filename)
if not mimetype:
mimetype, encoding = mimetypes.guess_type(filename)
if mimetype is None or encoding is not None:
mimetype = 'application/octet-stream'
maintype, subtype = mimetype.split('/', 1)
attachment = MIMEBase(maintype, subtype)
attachment.set_payload(data)
encoders.encode_base64(attachment)
attachment.add_header('Content-Disposition', 'attachment', filename=filename)
self.message.attach(attachment)
self.__attachments.append(filename)
if data:
maintype, subtype = mimetype.split('/', 1)
attachment = MIMEBase(maintype, subtype)
attachment.set_payload(data)
encoders.encode_base64(attachment)
attachment.add_header('Content-Disposition', 'attachment', filename=filename_only)
self.message.attach(attachment)
else:
self.__attachments_data.append([filename, mimetype])
if filename_only not in self.__attachments:
self.__attachments.append(filename_only)

def prepare(self):
"""
Expand Down Expand Up @@ -339,4 +349,8 @@ def prepare(self):
body.attach(plaintext_part)
body.attach(html_part)
self.message.attach(body)
if self.__attachments_data:
for attachment_data in self.__attachments_data:
with open(attachment_data[0], 'rb') as a_file:
self.attach(filename=basename(attachment_data[0]), data=a_file.read(), mimetype=attachment_data[1])
self.prepared = True