Skip to content

Instantly share code, notes, and snippets.

@abelsonlive
Last active January 4, 2016 07:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abelsonlive/8587172 to your computer and use it in GitHub Desktop.
Save abelsonlive/8587172 to your computer and use it in GitHub Desktop.
Load a folder of images / pdfs / documents / etc, and send them one at a time as a attachments via gmail.
import smtplib
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email.MIMEMultipart import MIMEMultipart
from email import Encoders
import time
import os
def load_attachments(folder):
# clean up folder name
if folder.endswith('/'):
folder = folder[:-1]
filepaths = ["%s/%s" % (folder, f) for f in os.listdir(folder)]
attachments = []
for f in filepaths:
if '.DS_Store' not in f:
attachments.append({
'data': open(f, 'rb').read(),
'filename': f.split('/')[1]
})
return attachments
def gen_msg(from_, to_, subject, html_body, text_body):
# Create the container (outer) email message.
msg = MIMEMultipart()
msg['Subject'] = subject
msg['From'] = from_
msg['To'] = ', '.join(to_)
html_body = """\
<html>
<head></head>
<body>
%s
</body>
</html>
""" % (html_body)
body = MIMEMultipart('alternative')
part1 = MIMEText(text_body, 'plain')
part2 = MIMEText(html_body, 'html')
body.attach(part1)
body.attach(part2)
msg.attach(body)
return msg
def email_attachments(
from_,
to_,
username,
password,
folder,
subject='OpenNews Receipts',
text_body = '',
html_body=''):
if isinstance(to_, basestring):
to_ = [to_]
# attach each receipt, send one message per attachment
attachments = load_attachments(folder)
for a in attachments:
# gen shell message
msg = gen_msg(from_, to_, subject, html_body, text_body)
# lookup file type
f = a['filename']
if f.endswith('doc') or f.endswith('.docx'):
attach_file = MIMEBase('application', 'msword')
elif f.endswith('pdf'):
attach_file = MIMEBase('application', 'pdf')
elif f.endswith('jpg'):
attach_file = MIMEBase('image', 'jpg')
elif f.endswith('png'):
attach_file = MIMEBase('image', 'png')
elif f.endswith('gif'):
attach_file = MIMEBase('image', 'gif')
else:
attach_file = MIMEBase('application', 'octet-stream')
# attach
attach_file.set_payload(a['data'])
Encoders.encode_base64(attach_file)
attach_file.add_header('Content-Disposition', 'attachment', filename=f)
msg.attach(attach_file)
#
print "< sending: %s > < from: %s > < to: %s > " % (f, from_, msg['To'])
# Send the email via gmail.
s = smtplib.SMTP('smtp.gmail.com', 587)
s.ehlo()
s.starttls()
s.ehlo()
s.login(username, password)
s.sendmail(from_, to_, msg.as_string())
s.close()
time.sleep(1)
if __name__ == '__main__':
email_attachments(
from_ = "brianabelson@gmail.com",
to_ = "mozillafoundation@bill.com",
folder = 'receipts-final',
username = 'brianabelson@gmail.com',
password = '',
subject = "OpenNews Receipt Submission - Brian Abelson"
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment