Overview
The inventory management system can automatically send Excel reports via email using Gmail’s SMTP server. This guide covers the complete setup process.Prerequisites
- Gmail account
- 2-Factor Authentication enabled on your Gmail account
- Access to Google Account settings
Gmail Configuration
Step 1: Enable 2-Factor Authentication
If you haven’t already enabled 2FA:Go to Google Account Security
Enable 2-Step Verification
Under “Signing in to Google”, click on “2-Step Verification” and follow the setup wizard
Step 2: Generate App Password
Access App Passwords page
Visit https://myaccount.google.com/apppasswordsOr navigate: Google Account → Security → 2-Step Verification → App passwords
Create new app password
- Select app: Mail
- Select device: Other (Custom name)
- Enter name:
Inventory Systemor similar - Click Generate
Environment File Configuration
Create .env File
Create a file named.env in the project root directory:
Add Email Credentials
Open.env and add your configuration:
EMAIL_REMITENTE: Your Gmail addressEMAIL_PASSWORD: The 16-character app password (spaces are optional)EMAIL_DESTINATARIO: Where reports should be sent
Verify Configuration
The system validates credentials before sending (fromsrc/main.py:23-40):
SMTP Settings
The system uses Gmail’s SMTP server with the following configuration (fromsrc/emailer.py:30):
| Setting | Value |
|---|---|
| Server | smtp.gmail.com |
| Port | 465 |
| Encryption | SSL/TLS |
| Authentication | Required |
These settings are hardcoded for Gmail. Other email providers require code modifications.
Email Content
The system sends emails with the following structure (fromsrc/emailer.py:6-28):
Email Headers
Email Body
Attachment
The Excel report is attached:- Type: Excel file (
.xlsx) - Name:
reporte_inventario.xlsx - Content: Multi-sheet report with complete inventory analysis
Testing Email Delivery
Quick Test
Run the system to test email delivery:Troubleshooting
⚠️ Envío de correo omitido (credenciales no configuradas)
⚠️ Envío de correo omitido (credenciales no configuradas)
Cause: Email configuration validation failedSolutions:
- Verify
.envfile exists in project root - Check all three variables are defined:
EMAIL_REMITENTE,EMAIL_PASSWORD,EMAIL_DESTINATARIO - Ensure variables don’t contain placeholder values
- Remove any quotes around values in
.envfile
SMTPAuthenticationError: Username and Password not accepted
SMTPAuthenticationError: Username and Password not accepted
Cause: Invalid Gmail credentials or app passwordSolutions:
- Verify you’re using an app password, not your regular Gmail password
- Check that 2-Factor Authentication is enabled on your Gmail account
- Generate a new app password and update
.env - Ensure there are no extra spaces in the password
- Verify the email address is correct
SMTPServerDisconnected: Connection unexpectedly closed
SMTPServerDisconnected: Connection unexpectedly closed
Cause: Network or firewall issuesSolutions:
- Check internet connection
- Verify firewall allows outbound connections on port 465
- Try again - this can be a temporary Gmail server issue
- Check if your organization blocks SMTP traffic
FileNotFoundError: output/reporte_inventario.xlsx
FileNotFoundError: output/reporte_inventario.xlsx
Cause: Report file doesn’t existSolutions:
- This should not happen as the email is sent AFTER report generation
- Check console output for errors in report generation step
- Verify the
output/directory exists and is writable
Email sent but not received
Email sent but not received
Cause: Emails caught by spam filters or delivery delaysSolutions:
- Check spam/junk folder in recipient email
- Wait a few minutes - Gmail can have delays
- Verify recipient email address is correct in
.env - Check Gmail’s “Sent” folder to confirm it was sent
- Add sender email to recipient’s contacts/safe senders
Less secure app access error
Less secure app access error
Cause: Attempting to use regular password instead of app passwordSolution:
Google has disabled “less secure app access”. You must use app passwords with 2FA enabled. Regular passwords no longer work for SMTP.
Using Different Email Providers
To use a different email provider (not Gmail), modifysrc/emailer.py:30:
Outlook/Office 365
Yahoo Mail
Custom SMTP Server
Security Best Practices
Use dedicated email account
Consider creating a separate Gmail account specifically for automated reporting
Limit app password scope
Generate separate app passwords for different applications to maintain granular control
Revoking App Passwords
If you need to revoke an app password:Visit App Passwords page
Automated Scheduling Considerations
When scheduling automated execution:- Cron jobs: Ensure the
.envfile path is accessible - Task schedulers: Verify environment variables are loaded correctly
- Docker containers: Mount
.envfile or pass variables as container environment variables - Cloud functions: Use platform-specific secret management (e.g., AWS Secrets Manager)
Email Delivery Confirmation
Success indicator (fromsrc/emailer.py:34):
- SMTP connection succeeded
- Authentication passed
- Email was accepted by Gmail’s server
- Attachment was sent successfully
This confirms the email was sent to Gmail’s server, not that it was delivered to the recipient’s inbox. Check the recipient’s inbox (including spam) to verify delivery.
Next Steps
Running the System
Learn how to execute the system and test email delivery
Understanding Outputs
Understand the email attachment contents and reports