The bare minimum api to send emails from your gmail account using Python.
- Easy API
- Common defaults for using smtp with Gmail so you don't have to think about it.
- Supports emojis in text by default
- Getting previous emails
- Getting contacts
- Changing email settings
- Etc.
- Must have a Gmail account
- Must have 2 factor authentication setup on your gmail account
- Must create an app password for your gmail account
Pip install this for your system:
pip install git+https://www.github.com/zackplauche/python-gmail
Create your instance and send an email
>>> from gmail import Client
>>> client = Client(email='[email protected]', app_password='aaaa aaaa aaaa aaaa')
>>> client.send_email(to='[email protected]', subject='Hello! 👋', body='How are you today? 👀❓')
or send to multiple emails separately
>>> client.send_email(to=['[email protected]', '[email protected]'], subject='Hello! 👋', body='How are you today? 👀❓')
That's it!
You can create a gmail instance from just your gmail account email and your gmail account app password
>>> from gmail import Client
>>> client = Client(email='[email protected]', app_password='aaaa aaaa aaaa aaaa')
If you wish to create instantiate a client more securely, you can create your instance from environment variables using the GMAIL_EMAIL
and GMAIL_APP_PASSWORD
environment variable names.
- Create a
.env
file in your directory and add the following:
GMAIL_EMAIL=[email protected]
GMAIL_APP_PASSWORD=aaaa aaaa aaaa aaaa
- Create your instance:
>>> client = Client.from_env()
You can send an email by writing the following:
>>> client.send_email(
... to='[email protected]',
... subject='Hello! 👋',
... body='How are you doing? 👀❓'
... )
This will send an email with a subject and body.
Important Note: Where usually you would see the name of your gmail account, you will only see your email unless you specify a name in the from_
field, like this:
>>> client.send_email(
... from_=f'Example Email <{client.email}>',
... to='[email protected]',
... subject='Howdy! 🤠',
... body='Good morning neighbor! ☀️🏠🥓'
... )
For convenience, you can also create this at the instantiation and not have to think about it later:
>>> # Name will be Bob <[email protected]>
>>> client = Client(email='[email protected]', name='Bob', app_password=...)
>>> # or from env vars
>>> client = gmail.from_env(name='Bob') # OR set GMAIL_NAME as an environment variable
Then you can send an email without the from_
argument and it will have your name in it.
Enjoy! 📨📨📨
- Website: https://www.zackplauche.com
- YouTube: https://www.youtube.com/@zackplauche