Skip to content

Instantly share code, notes, and snippets.

@drorata
Created September 27, 2017 13:45
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save drorata/830e0b6b8b50f330c85fdcdad51fe333 to your computer and use it in GitHub Desktop.
Save drorata/830e0b6b8b50f330c85fdcdad51fe333 to your computer and use it in GitHub Desktop.
Minimal example of sending a JSON over email
import json
# Import smtplib for the actual sending function
import smtplib
# Import the email modules we'll need
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from datetime import datetime
def send_email(data):
sending_ts = datetime.now()
sub = "Here's my subject %s" % sending_ts.strftime('%Y-%m-%d %H:%M:%S')
msg = MIMEMultipart('alternative')
msg['From'] = 'no-reply@service.com'
msg['To'] = 'provider@service.com'
msg['Subject'] = sub
body = "This would be the body of the msg"
msg.attach(MIMEText(body, 'plain'))
attachment = MIMEText(json.dumps(data))
attachment.add_header('Content-Disposition', 'attachment',
filename="foo.name.json")
msg.attach(attachment)
s = smtplib.SMTP('email-server')
s.send_message(msg)
s.quit()
return 0
if __name__=="__main__":
data = {"my": "foo", "yours": "bar"}
send_email(data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment