Skip to content

Instantly share code, notes, and snippets.

@Lukasa
Created August 6, 2012 18:44
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 Lukasa/3277447 to your computer and use it in GitHub Desktop.
Save Lukasa/3277447 to your computer and use it in GitHub Desktop.
An example script for sending a text file to yourself via email
#!/usr/bin/env python
'''A script that sends the contents of a file in a plain-text email.'''
# Imports
import smtplib
import sys
# Important constants.
FROM_ADDR = 'name@example.com'
FROM_PASS = 'thisisntarealpassword'
FROM_NAME = 'Constable Reggie'
TO_ADDR = 'other.name@example.com'
TO_NAME = 'Inspector Spacetime'
SUBJECT = 'Fileserver ZFS Status Report'
SMTP_HOST = 'smtp.gmail.com'
SMTP_PORT = '465'
def main():
message_file = sys.argv[1]
body = construct_message_body(message_file)
# SMTPlib stuff.
sender = FROM_ADDR
receivers = [TO_ADDR]
smtp_obj = smtplib.SMTP_SSL(SMTP_HOST, SMTP_PORT)
smtp_obj.login(FROM_ADDR, FROM_PASS)
smtp_obj.sendmail(sender, receivers, body)
def construct_message_body(input_file):
'''This function constructs the body of the message'''
from_line = 'From: ' + FROM_NAME + ' <' + FROM_ADDR + '>'
to_line = 'To: ' + TO_NAME + ' <' + TO_ADDR + '>'
subject_line = 'Subject: ' + SUBJECT
body = '\n'.join([from_line, to_line, subject_line, '\n'])
with open(input_file, 'r') as f:
body += f.read()
body += '\n'
return body
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment