import smtplib

"""
Explanation: Lesson 46:
Sending Emails
Python can also be used send emails. This can be used for a variety of purposes, like updating a user at the end of a script completion, logging results, processing data, batch emailing, etc.
The standard protocol for sending email is 'Simple Mail Transfer Protocol' (SMTP), and one Python module designed to interact with is smtplib.
End of explanation
"""


conn = smtplib.SMTP('smtp.gmail.com', 587)

"""
Explanation: The first step in setting up email is creating a connection object to interact with an email/SMTP server.
This function gets a domain name and a port number. The port number for SMTP is typically 587; it is part of the internet address you are sending data to. The domain name is the address at which an SMTP server is hosted (typically the domain name with 'smtp' appended to it). Here are some additional examples.
End of explanation
"""


conn.ehlo()

"""
Explanation: We will now use the .ehlo() method to test and connect.
End of explanation
"""


conn.starttls()

"""
Explanation: The returned variable is the response code (anything starting with a 2 is good), followed by a byte sting.
Since we have a positive response, we can now start an encrypted TLS connection (which is the newer version of SSL technology, both of which are based on public cryptography). Most modern email services will require TLS to begin interaction.
End of explanation
"""


# Real values were used in testing, and removed for Github
# Due to the nature of Gmail's security, you may have to allow access from 'less secure apps' (like this script)
# The setting can be changed here: https://www.google.com/settings/u/2/security/lesssecureapps

conn.login('youremail@gmail.com','yourpassword')

"""
Explanation: With the positive response, we can now pass in our login paramaters via the .login() method.
With Google specifically, there is an option to create app specific password, to avoid using your primary login for every device. This protects you from changing your password for every device in case of an error, which may be useful.
End of explanation
"""


# For this exercise, we will just send an email to and from the same address; be sure to update these values with your
# own email addresses 

from_email = 'youremail@gmail.com'
to_email = 'theiremail@gmail.com'

conn.sendmail(from_email, to_email, 'Subject: Python Test Email \n\n Look,\n script text!')

"""
Explanation: Once we get a good response, we can now send emails via the .sendmail() method, which takes a variety of parameters, but requires a 'from' email, a 'to' email, and some content. 
Most modern email services include protections to stop users from spamming other users, so they cap the number of emails that can be sent per day (Gmail is roughly 150 emails).
End of explanation
"""


conn.quit()

"""
Explanation: A blank response indicates a successful email has been sent:

We can close our connection via the .quit() method:
End of explanation
"""


