Sending Emails With Python 3.6+
Sending Emails With Python 3.6+

By Luke9801 | Programming | 11 Jan 2020


Sending automated emails can be a pain. There are many services online but chances are they charge large monthly fees and don't provide the level of control you're looking for. As always, in my opinion, it's best to just write your own. This way you can get exactly what you want and learn something along the way.   Here is some code:

Let's explore this code. Line 1 imports the required library. Lines 3, 4, 5 & 6 define some variables for who is sending your email, who to send it to, and what the message should be (don't forget to change them). Lines 8 & 9 connect to the mail server. Line 10 sends your mail and line 11 closes the connection to the server.  

yourEmail = "replace with your email address" 
yourPassword = "replace with your email password"
recipientEmail = "replace with recipient email"
message = "replace with your message"

server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(yourEmail, yourPassword)
server.sendmail(yourEmail, recipientEmail, message)
server.quit()

This is a simple example with no subject or attachments but if you can get it to run successfully, you've done the hardest part. A common problem people have with this is that gmail disallows signing in from insecure applications by default. Hate to break it to you but the script I wrote it a few minutes is not particularly secure. Less secure apps can be enabled by making sure that the switch on this page is on. Once you do this, you should have no problem sending your emails.  

Sending emails with subjects and attachments gets more complicated but is still fairly manageable. The code:

import smtplib 
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders

yourEmail = "replace with your email address"
yourPassword = "replace with your email password"
recipientEmail = "replace with recipient email"
subject = "replace with your subject"
body = "replace with your message"
filename = "replace with path to the file to attach"

msg = MIMEMultipart()
msg['From'] = yourEmail
msg['To'] = recipientEmail
msg['Subject'] = subject
msg.attach(MIMEText(body, 'plain'))

attachment = open(filename, "rb")
part = MIMEBase('application', 'octet-stream')
part.set_payload((attachment).read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', "attachment; filename= %s" % filename)
msg.attach(part)

text = msg.as_string()

server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(yourEmail, yourPassword)
server.sendmail(yourEmail, recipientEmail, text)
server.quit()

This time lines 1-5 imports the required libraries. Lines 7-12 set variables for the content of your message. Lines 14-18 create a message object from your choices before hand. Lines 20-25 attach your file. Line 27 turns the message object into a string that can be sent in an email. Lines 29-33 start the server, send the mail and close the server.  

This example is a more complicated so more things can go wrong. Again, make sure you set the variables at the beginning and enable insecure apps. Another common problem with this one is where you put the file you want to attach. If it is in the same folder as the script, you can just use the file name. Otherwise, use the full path to the file.



Programming
Programming

I discuss programming in python and javascript. Specifically creating websites, web servers and chrome extensions.

Send a $0.01 microtip in crypto to the author, and earn yourself as you read!

20% to author / 80% to me.
We pay the tips from our rewards pool.