<jupyter_start><jupyter_text>______Content Copyright by <NAME>  Overview of Sending Emails The smtplib library allows you to manually go through the steps of creating and sending an email in Python:<jupyter_code>import smtplib<jupyter_output><empty_output><jupyter_text>Create an SMTP object for a server. Here are the main Server Domain Name for the top email services. If you don't see your email server here, you may need to do a quick Google Search to see if there SMTP server domain name is available:    Provider    SMTP server domain name         Gmail (will need App Password)    smtp.gmail.com    Yahoo Mail    smtp.mail.yahoo.com    Outlook.com/Hotmail.com    smtp-mail.outlook.com        AT&T    smpt.mail.att.net (Use port 465)    Verizon    smtp.verizon.net (Use port 465)     Comcast    smtp.comcast.net Next is to create an STMP object that can make the method calls to log you in to your email in order to send messages. Notice how also specify a port number. If the number 587 does not work on your computer, try using 465 instead. Keep in mind, a firewall or antivirus may prevent Python from opening up this port, so you may need to disable it on your computer.<jupyter_code>smtp_object = smtplib.SMTP('smtp.gmail.com',587)<jupyter_output><empty_output><jupyter_text>Next we run the ehlo() command which "greets" the server and establishes the connection. This method call should be done directly after creating the object. Calling it after other methods may result in errors in connecting later on. The first item in the tuple that is returned should be 250, indicating a successful connection.<jupyter_code>smtp_object.ehlo()<jupyter_output><empty_output><jupyter_text>When using the 587 port, this means you are using TLS encryption, which you need to initiate by running the starttls() command. If you are using port 465, this means you are using SSL and you can skip this step.<jupyter_code>smtp_object.starttls()<jupyter_output><empty_output><jupyter_text>Now its time to set up the email and the passwords. You should never save the raw string of your password or email in a script, because anyone that sees this script will then be able to see you email and password! Instead you should use input() to get that information. If you also don't want your password to be visible when typing it in, you can use the built-in **getpass** library that will hide your password as you type it in, either with asterisks or by just keeping it invisible.<jupyter_code># For hidden passwords
import getpass
result = getpass.getpass("Type something here and it will be hidden: ")
# Just keep in mind that its still visible as an object internally:
result
# Or just use input()
input("Enter your password")<jupyter_output>Enter your passwords<jupyter_text>____**Note for Gmail Users, you need to generate an app password instead of your normal email password. This also requires enabling 2-step authentication. Follow the instructions here to set-up 2-Step Factor Authentication as well as App Password Generation:https://support.google.com/accounts/answer/185833?hl=en/. Set-up 2 Factor Authentication, then create the App Password, choose Mail as the App and give it any name you want. This will output a 16 letter password for you. Pass in this password as your login password for the smtp.**____<jupyter_code>email = getpass.getpass("Enter your email: ")
password = getpass.getpass("Enter your password: ")
smtp_object.login(email,password)<jupyter_output><empty_output><jupyter_text>Now we can send an email using the .sendmail() method.<jupyter_code>from_address = getpass.getpass("Enter your email: ")
to_address = getpass.getpass("Enter the email of the recipient: ")
subject = input("Enter the subject line: ")
message = input("Type out the message you want to send: ")
msg = "Subject: " + subject + '\n' + message
smtp_object.sendmail(from_address,to_address,msg)<jupyter_output>Enter your email: ········
Enter the email of the recipient: ········
Enter the subject line: This is a test
Type out the message you want to send: Here is the message.<jupyter_text>If you get back an empty dictionary, then the sending was successful. You can then close your session with the .quit() method.<jupyter_code>smtp_object.quit()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.06721311475409836, 'fraction_numerical': 0.00819672131147541, 'mean_word_length': 4.981792717086835, 'pattern_counts': {'":': 0, '<': 32, '<?xml version=': 0, '>': 32, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '265185', 'license': 'MIT', 'n_tokens_mistral': 1225, 'n_tokens_neox': 1153, 'n_words': 567, 'path': '4-assets/BOOKS/Jupyter-Notebooks/00-Overview-of-Sending-Emails.ipynb', 'repo_name': 'impastasyndrome/Lambda-Resource-Static-Assets', 'size': '4276'}