Skip to content

Instantly share code, notes, and snippets.

@richard-scott
Forked from drorata/send_email.py
Created May 17, 2021 14:38
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 richard-scott/6d04ffc3eab47454e1bd4c8af2e3a704 to your computer and use it in GitHub Desktop.
Save richard-scott/6d04ffc3eab47454e1bd4c8af2e3a704 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