Home >Backend Development >Python Tutorial >Quick Tip: Sending Email via Gmail with Python

Quick Tip: Sending Email via Gmail with Python

Lisa Kudrow
Lisa KudrowOriginal
2025-02-10 09:31:12714browse

This tutorial demonstrates how to send emails using Python and your Gmail account. Perfect for automated reports or simple notifications!

Quick Tip: Sending Email via Gmail with Python

Sending emails from scripts is incredibly useful, especially for scheduled tasks. Imagine receiving automated summaries of data processed by your scripts, ensuring everything runs smoothly. While services like SendGrid, Mandrill, and Mailgun offer robust solutions for large-scale email sending, using Gmail directly is a simpler approach for personal or small-scale needs.

Although Google provides a Gmail API, a more straightforward method utilizes Python's built-in smtplib and email modules. This avoids complex API setup. However, remember that sending mass emails this way isn't recommended; for large volumes, dedicated email services are far superior.

Setting up App Passwords:

Before you begin, you'll need to generate an App Password for your Gmail account. This enhances security. Navigate to your Google account's App Passwords settings, select "Mail" as the app, "Other (custom name)" as the device, and create a descriptive name (e.g., "My Python Script"). Record this generated password—it's crucial for authentication.

Quick Tip: Sending Email via Gmail with Python

Coding the Email:

Let's craft a simple email using Python:

<code class="language-python">email_text = f"""
Hi! This is a test email from my Python script.

The result of 1 + 2 is: {1 + 2}

Regards,
Your Script
"""

GMAIL_USERNAME = "your_gmail_address"  # Replace with your Gmail address
GMAIL_APP_PASSWORD = "your_app_password"  # Replace with your App Password

recipients = ["recipient_email@example.com"]  # Replace with recipient's email
msg = MIMEText(email_text)
msg["Subject"] = "Python Email Test"
msg["To"] = ", ".join(recipients)
msg["From"] = f"{GMAIL_USERNAME}@gmail.com"

smtp_server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
smtp_server.login(GMAIL_USERNAME, GMAIL_APP_PASSWORD)
smtp_server.sendmail(msg["From"], recipients, msg.as_string())
smtp_server.quit()</code>

Remember to replace placeholders like your_gmail_address, your_app_password, and recipient_email@example.com with your actual details.

This code defines the email content, sets recipient and sender information, and uses smtplib to connect to Gmail's SMTP server, log in securely using your App Password, and send the email. Error handling and input validation are omitted for brevity, but are crucial for production environments.

This tutorial is an excerpt from Useful Python, available on SitePoint Premium and ebook retailers. For more advanced email functionality (attachments, HTML emails, etc.), consult the full resource or explore the email.mime module's capabilities.

The above is the detailed content of Quick Tip: Sending Email via Gmail with Python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn