dc-mailer
Send an email alert using Python and Gmail
Requirements
- Python 3.11+ (required for reading toml files)
- A configured Gmail Account (see below)
Step 1. Install this Module
Run
pip install dc-mailer
Or: add dc-mailer to requirements.txt and install.
Step 2. Configure Application Settings
Local Development
For local development, we can configure our settings with a file. First, add .env.toml to .gitignore to keep it from being published. Then, create a .env.toml file in your project directory with the following.
outgoing_email_host = "smtp.gmail.com"
outgoing_email_port = 587
outgoing_email_address = "youremail@gmail.com"
outgoing_email_password = "aaaabbbbccccdddd"
GitHub Action Deployments
In a deployment environment like GitHub Actions, the .env.toml file won't be available. Instead, you'll need to set the configuration variables as environment variables.
First, add the following secrets to your GitHub repository:
- Navigate to your repository on GitHub.
- Click on Settings > Secrets and variables > Actions.
- Click on New repository secret and add the following secrets:
- OUTGOING_EMAIL_HOST: Set this to "smtp.gmail.com".
- OUTGOING_EMAIL_PORT: Set this to 587.
- OUTGOING_EMAIL_ADDRESS: Set this to your email address (e.g., "your-email@gmail.com").
- OUTGOING_EMAIL_PASSWORD: Set this to your application password.
Next, reference these secrets in your GitHub Actions workflow. For an example, see this deploy.yml.
Step 3. Configure Gmail
Enable IMAP
- Open Gmail. Click Settings or ⚙️ in the top-right.
- Click "See all settings". Navigate to "Forwarding and POP/IMAP".
- Under "IMAP access", select "Enable IMAP" and save changes.
Generate an App Password
- If you have 2-Step Verification enabled, create an app password for "dc-mailer".
- Copy the 16-character password displayed.
- Paste the 16-char as your password in .env.toml file. Remove spaces.
- For detailed instructions, refer to Google's support page.
Step 4. Import and Use in a Python Script
Once installed and your .env.toml file is ready, you can use it in your code.
from dc_mailer import send_mail
title = "Email from Data Analyst and Python Developer"
content = "Did you know the Python standard library enables emailing?"
recipient = "youremail@gmail.com"
try:
send_mail(subject=title, body=content, recipient=recipient)
print("SUCCESS: Email sent.")
except RuntimeError as e:
print(f"ERROR: Sending failed: {e}")
Metadata
Release files for dc-mailer 0.1.5
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| dc_mailer-0.1.5.tar.gz | 8.0 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| dc_mailer-0.1.5-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 15.3 kB
Release files / dc_mailer-0.1.5.tar.gz
| Download URL | dc_mailer-0.1.5.tar.gz |
|---|---|
| Size | 8.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
0f1ef2e399522356af5115fb5e6c020dbf06b01c70cb61ec95459b1fba8c3391
|
|
BLAKE2b-256 checksum How to use checksums |
31e8497edbbb1730e7705291f2d131e3d724dc6661fc464c884008c9550c0c59
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.11.11
|
Release files / dc_mailer-0.1.5-py3-none-any.whl
| Download URL | dc_mailer-0.1.5-py3-none-any.whl |
|---|---|
| Size | 7.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
a0de6e2abeb2bdd9a5715f23cf8d8522286b9062c5642a5d2c8fc14dc492909e
|
|
BLAKE2b-256 checksum How to use checksums |
67f6a679cf00baca688452a1dfbf4043894022b7ccd4f80bb2e909552713844c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.11.11
|