# coding=utf-8 #: ## Lab 5 ## #: #: CS-2910 Network Protocols #: Dr. Yoder #: Fall quarter 2014-2015 #: #: | Team members (username) | #: |:------------------------| #: | Grace Hopper (hopperg) | #: | Donald Knuth (knuthd) | #: # Socket library import socket # SSL/TLS library import ssl # base-64 encode/decode import base64 # Python date/time and timezone modules import datetime import time import pytz # Module for reading password from console without echoing it import getpass # Modules for some file operations import os import mimetypes # Host name for MSOE (hosted) SMTP server SMTP_SERVER = 'smtp.office365.com' # Sender/user email # Used as "From" and as username for authentication # Modify as needed SMTP_USER = 'student@msoe.edu' # The default port for STARTTLS SMTP servers is 587 SMTP_PORT = 587 # SMTP domain name SMTP_DOMAINNAME = 'msoe.edu' # Name of the timezone to use for the date field. # See RFC 5322 for details about how to pick this. # https://tools.ietf.org/html/rfc5322 TIMEZONE_NAME = '' # Insert the timezone here. # Main test method to send an SMTP email message # Modify data as needed/desired to test your code, # but keep the same interface for the smtp_send # method. def main(): message_info = {} message_info['To'] = 'sebern@msoe.edu' message_info['From'] = SMTP_USER message_info['Subject'] = 'Yet another test message' message_info['Date'] = 'Thu, 9 Oct 2014 23:56:09 +0000' message_info['Date'] = get_formatted_date() print "message_info =",message_info message_text = 'Test message_info number 6\r\n\r\nAnother line.' # Prompt user for email password # You may alternatively wish to read the password from a file. Or hard-code it here. # If you run Pycharm in Debug mode, it will allow you to enter the password, but it will show up as plain-text. password = getpass.getpass() smtp_send(password,message_info,message_text) # Send a message via SMTP. # password -- string containing user password # message_info -- dictionary with string values for the following keys: # 'To': Recipient address (only one recipient required) # 'From': Sender address # 'Date': Date string for current date/time in SMTP format # 'Subject': Email subject # Other keys can be added to support other email headers, etc. # Returns: # No return value def smtp_send(password,message_info,message_text): pass # Replace this line with your code # Your code and additional functions go here. (Replace this line, too.) # ** Do not modify code below this line. # Utility functions # You may use these functions to simplify your code. # Get the current date and time, in a format suitable for an email date header. # The constant TIMEZONE_NAME should be one of the standard pytz timezone names. # If you really want to see them all, call the print_all_timezones function. # Returns: # Formatted current date/time value, as a string def get_formatted_date(): zone = pytz.timezone(TIMEZONE_NAME) print "zone =",zone timestamp = datetime.datetime.now(zone) timestring = timestamp.strftime('%a, %d %b %Y %H:%M:%S %z') #Sun, 06 Nov 1994 08:49:37 +0000 return timestring # Print all pytz timezone strings def print_all_timezones(): for tz in pytz.all_timezones: print tz # You probably won't need the following methods, unless you decide to # try to handle email attachments or send multi-part messages. # These advanced capabilities are not required for the lab assignment. # Try to guess the MIME type of a file (resource), given its path (primarily its file extension) # file_path -- string containing path to (resource) file, such as './abc.jpg' # Returns: # If successful in guessing the MIME type, a string representing the content type, such as 'image/jpeg' # Otherwise, None def get_mime_type(file_path): mime_type_and_encoding = mimetypes.guess_type(file_path) mime_type = mime_type_and_encoding[0] return mime_type # Try to get the size of a file (resource) in bytes, given its path # file_path -- string containing path to (resource) file, such as './abc.html' # Returns: # If file_path designates a normal file, an integer value representing the the file size in bytes # Otherwise (no such file, or path is not a file), None def get_file_size(file_path): # Initially, assume file does not exist file_size = None if os.path.isfile(file_path): file_size = os.stat(file_path).st_size return file_size main() # Your report goes here (replace this line)