Send Emails for Free With Python and Gmail SMTP Mail Server API
Tags: python, gmail, api
Published: 2023-09-22 10:16:00

## Sending Emails With Python
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 examples.

Let’s check out some 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()
```

## Code Walkthrough

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
```

Then, we call `MIMEMultiPart()` and instantiate it to the variable `Message`. We then give a string value to each variables in `Message`, `To`, `From` and `Subject`:

```
Message = MIMEMultipart()
Message["To"] = 'To line here.'
Message["From"] = 'From line here.'
Message["Subject"] = 'Subject line here.'
```

We will also give a title to our email through the variable `title`, add a message to our email through `MIMEText()` and instantiate it to the variable `messagetext` and finally attach our message to our main variable Message using the `attach()` function.

```
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](https://support.google.com/accounts/answer/185833?hl=en) if you don’t know how to get one:

```
email = 'Your Gmail address here.'
password = 'Your App password here.'
```

Then, we add Gmail’s SMTP server and port we will be connecting to with `smtplib.SMTP(smtp.gmail.com:587)` and instantiate it to the variable `server`, these are `smtp.gmail.com` and `587` respectively. (With ‘:’ in between them):

```
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 just put Gmail to keep it simple:

`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 login to the mail sever using `server.login(email,password)`:

`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()
```

## Result

Let’s launch it in a Terminal:

![Screenshot of a Terminal](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/382ez4ds3usut10apo9b.png)


Email sent directly to my inbox:

![Screenshot of an email in Gmail.](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j86tb7hxvmc5fan2qc8h.png)

Simple and easy!