-
-
Save richard-scott/6d04ffc3eab47454e1bd4c8af2e3a704 to your computer and use it in GitHub Desktop.
Minimal example of sending a JSON over email
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'] = '[email protected]' | |
msg['To'] = '[email protected]' | |
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