Skip to content
🐍 Sending emails with python script!
Profile icon
txasdev

Introduction

Hello everyone! My name is txasdev, and I'm a learning web developer. Recently I have discovered a very cool script that will send emails with python using the smtplib library. This script is very easy, learn how to use it below.

Script Info

This script sends an email using python. By logging in to a Gmail account with python you can send emails using this tutorial. This tutorial shows you how to send basic emails and emails with files attached. These scripts are used to send emails from a Gmail account (can be configured for other email servers). This will allow you to send emails when your script finishes executing, has come to an error or just needs to send some data to you.

Before Starting

Before you start this, make sure you have your Gmail username and password ready. The account you are going to use must not have 2-Step Verification enabled as we will no longer be able to log in.

Next, go to https://myaccount.google.com/lesssecureapps and log in if you need to. On this page, you will want to flick the Allow less secure apps switch to on. This allows us to use less secure sign-in technology to login to the email server; note that this will make your account more vulnerable. (Nitratine)

Layouts

(Simple Layout)

import smtplib email = '[email protected]' # Your email password = 'password' # Your email account password send_to_email = '[email protected]' # Who you are sending the message to message = 'This is my message' # The message in the email server = smtplib.SMTP('smtp.gmail.com', 587) # Connect to the server server.starttls() # Use TLS server.login(email, password) # Login to the email server server.sendmail(email, send_to_email , message) # Send the email server.quit() # Logout of the email server

(Better Layout)

import smtplib from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart email = '[email protected]' password = 'password' send_to_email = '[email protected]' subject = 'This is the subject' # The subject line message = 'This is my message' msg = MIMEMultipart() msg['From'] = email msg['To'] = send_to_email msg['Subject'] = subject # Attach the message to the MIMEMultipart object msg.attach(MIMEText(message, 'plain')) server = smtplib.SMTP('smtp.gmail.com', 587) server.starttls() server.login(email, password) text = msg.as_string() # You now need to convert the MIMEMultipart object to a string to send server.sendmail(email, send_to_email, text) server.quit()

(Attachments)

import smtplib from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart from email.mime.base import MIMEBase from email import encoders import os.path email = '[email protected]' password = 'password' send_to_email = '[email protected]' subject = 'This is the subject' message = 'This is my message' file_location = 'C:\\Users\\You\\Desktop\\attach.txt' msg = MIMEMultipart() msg['From'] = email msg['To'] = send_to_email msg['Subject'] = subject msg.attach(MIMEText(message, 'plain')) # Setup the attachment filename = os.path.basename(file_location) attachment = open(file_location, "rb") part = MIMEBase('application', 'octet-stream') part.set_payload(attachment.read()) encoders.encode_base64(part) part.add_header('Content-Disposition', "attachment; filename= %s" % filename) # Attach the attachment to the MIMEMultipart object msg.attach(part) server = smtplib.SMTP('smtp.gmail.com', 587) server.starttls() server.login(email, password) text = msg.as_string() server.sendmail(email, send_to_email, text) server.quit()

(HTML)

import smtplib from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart email = '[email protected]' password = 'password' send_to_email = '[email protected]' subject = 'This is the subject' messageHTML = '<p>Visit <a href="https://nitratine.net/">nitratine.net<a> for some great <span style="color: #496dd0">tutorials and projects!</span><p>' messagePlain = 'Visit nitratine.net for some great tutorials and projects!' msg = MIMEMultipart('alternative') msg['From'] = email msg['To'] = send_to_email msg['Subject'] = subject # Attach both plain and HTML versions msg.attach(MIMEText(messagePlain, 'plain')) msg.attach(MIMEText(messageHTML, 'html')) server = smtplib.SMTP('smtp.gmail.com', 587) server.starttls() server.login(email, password) text = msg.as_string() server.sendmail(email, send_to_email, text) server.quit()

(Multi)

import smtplib from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart from email.mime.base import MIMEBase from email import encoders import os.path email = '[email protected]' password = 'password' send_to_emails = ['[email protected]', '[email protected]'] # List of email addresses subject = 'This is the subject' message = 'This is my message' file_location = 'C:\\Users\\You\\Desktop\\attach.txt' # Create the attachment file (only do it once) filename = os.path.basename(file_location) attachment = open(file_location, "rb") part = MIMEBase('application', 'octet-stream') part.set_payload(attachment.read()) encoders.encode_base64(part) part.add_header('Content-Disposition', "attachment; filename= %s" % filename) # Connect and login to the email server server = smtplib.SMTP('smtp.gmail.com', 587) server.starttls() server.login(email, password) # Loop over each email to send to for send_to_email in send_to_emails: # Setup MIMEMultipart for each email address (if we don't do this, the emails will concatenate on each email sent) msg = MIMEMultipart() msg['From'] = email msg['To'] = send_to_email msg['Subject'] = subject # Attach the message to the MIMEMultipart object msg.attach(MIMEText(message, 'plain')) # Attach the attachment file msg.attach(part) # Send the email to this specific email address server.sendmail(email, send_to_email, msg.as_string()) # Quit the email server when everything is done server.quit()

Resources

Ending Note

Thanks for reading my Programmers Blog, if you have any questions you can send me a email with the address of [email protected] and as usual hope you enjoyed! Refer to my email for donations, questions, or concerns such as code issues.

You are viewing a single comment. View All
Profile icon
FinnDouglas

@txasdev
I have had the same issue, the email and password that i am using is definitely correct.
img