


How to Send Emails in Python Using Gmail's Free SMTP Mail Server API
This is the easiest way you can start sending emails with Python using only two libraries, smtplib and email.
We will be using Gmail’s free RESTful API in this example.
Here's the code
import smtplib from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText message = MIMEMultipart() message["To"] = 'To line here.' message["From"] = 'From line here.' message["Subject"] = 'Subject line here.' title = '<b> Title line here. </b>' messageText = MIMEText('''Message body goes here.''','html') message.attach(messageText) email = 'Your Gmail address here.' password = 'Your app password here.' server = smtplib.SMTP('smtp.gmail.com:587') server.ehlo('Gmail') server.starttls() server.login(email,password) fromaddr = 'From line here.' toaddrs = 'Address you send to.' server.sendmail(fromaddr,toaddrs,message.as_string()) server.quit()
How the code works ?
First, let’s import smtplib then MIMEMultipart and MIMEText from email.mime.multipart and email.mime.text respectively:
import smtplib from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText
We then call MIMEMultiPart() and instantiate it to the variable Message. We then give a string value to each variable which are Message, To, From and Subject, it should look like this:
Message = MIMEMultipart() Message["To"] = 'To line here.' Message["From"] = 'From line here.' Message["Subject"] = 'Subject line here.'
We'll also give a title to our email through the title variable and add a message through MIMEText() and instantiate it to the variable messageText and finally attach our email message to our main variable Message using the attach() function, it should look like this:
title = '<b> Title line here. </b>' messageText = MIMEText('''Message body goes here.''','html') Message.attach(messageText)
Let’s add our Gmail address and our App password, click the link here if you don’t know how to get one:
email = 'Your Gmail address here.' password = 'Your App password here.'
Then we'll have to connect to Gmail's SMTP server, we'll do that using the smtplib library and we'll need two variables which are the server we want to connect to and the port which are smtp.gmail.com and 587 respectively.
Using the smtplib library we'll be calling the SMTP function and add the server and port variables as its arguments, it should look like this:
smtplib.SMTP('smtp.gmail.com:587') (don't forget the : between them)
We'll then instantiate it to the variable server:
server = smtplib.SMTP('smtp.gmail.com:587')
We then add an ehlo statement using server.ehlo(‘Gmail’), this should be your domain name, this is useful when sending en email to another mail server that supports ESMTP, let’s keep it simple and just put Gmail:
server.ehlo('Gmail')
Let’s also start the TLS protocol with server.starttls(), this tells the mail server we want to send our email through a secure connection:
server.starttls()
We'll then login to the mail sever using this line:
server.login(email,password)
Let’s add a from address and to address(es), using fromaddr and toaddrs respectively:
fromaddr = 'Your Gmail address.' toaddrs = 'Destination address.'
Finally, we send our email using server.sendmail(fromaddr,toaddrs,message.as_string()) and we close our connection to the mail server using server.quit():
server.sendmail(fromaddr,toaddrs,message.as_string()) server.quit()
Save that in a file called send_email.py, open a Terminal if you're on Linux or a Command Prompt if you're on Windows and launch it using python send_email.py and you should see this:
If nothing happens, well good news it's working!
You should have received an email to your destination email(s), here's what I got:
Sending email using Python and Gmail's free SMTP server is the easiest way you can start sending email within your Python code, this is exactly what I've done when I built my first online busines ever. You can read more about it here.
The above is the detailed content of How to Send Emails in Python Using Gmail's Free SMTP Mail Server API. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Python is suitable for data science, web development and automation tasks, while C is suitable for system programming, game development and embedded systems. Python is known for its simplicity and powerful ecosystem, while C is known for its high performance and underlying control capabilities.

You can learn basic programming concepts and skills of Python within 2 hours. 1. Learn variables and data types, 2. Master control flow (conditional statements and loops), 3. Understand the definition and use of functions, 4. Quickly get started with Python programming through simple examples and code snippets.

Python excels in gaming and GUI development. 1) Game development uses Pygame, providing drawing, audio and other functions, which are suitable for creating 2D games. 2) GUI development can choose Tkinter or PyQt. Tkinter is simple and easy to use, PyQt has rich functions and is suitable for professional development.

Python is easier to learn and use, while C is more powerful but complex. 1. Python syntax is concise and suitable for beginners. Dynamic typing and automatic memory management make it easy to use, but may cause runtime errors. 2.C provides low-level control and advanced features, suitable for high-performance applications, but has a high learning threshold and requires manual memory and type safety management.

You can learn the basics of Python within two hours. 1. Learn variables and data types, 2. Master control structures such as if statements and loops, 3. Understand the definition and use of functions. These will help you start writing simple Python programs.

To maximize the efficiency of learning Python in a limited time, you can use Python's datetime, time, and schedule modules. 1. The datetime module is used to record and plan learning time. 2. The time module helps to set study and rest time. 3. The schedule module automatically arranges weekly learning tasks.

Python is widely used in the fields of web development, data science, machine learning, automation and scripting. 1) In web development, Django and Flask frameworks simplify the development process. 2) In the fields of data science and machine learning, NumPy, Pandas, Scikit-learn and TensorFlow libraries provide strong support. 3) In terms of automation and scripting, Python is suitable for tasks such as automated testing and system management.

Python excels in automation, scripting, and task management. 1) Automation: File backup is realized through standard libraries such as os and shutil. 2) Script writing: Use the psutil library to monitor system resources. 3) Task management: Use the schedule library to schedule tasks. Python's ease of use and rich library support makes it the preferred tool in these areas.
