How can I write a Python script to automate sending emails?
How can I write a Python script to automate sending emails?
How can I write a Python script to automate sending emails?
solveurit24@gmail.com Changed status to publish February 13, 2025
Use the smtplib library to send emails. Here’s a basic example:
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
# Create the message
msg = MIMEMultipart()
msg['From'] = 'your.email@example.com'
msg['To'] = 'recipient.email@example.com'
msg['Subject'] = 'Test Email'
# Add body to the email
body = 'This is a test email.'
msg.attach(MIMEText(body, 'plain'))
# Connect to the SMTP server
server = smtplib.SMTP('smtp.example.com', 587)
server.starttls()
# Login to the server
server.login('your.email@example.com', 'your_password')
# Send the email
server.send_message(msg)
server.quit()Explanation:
- Replace the SMTP server details and email credentials with your actual email provider’s settings.
solveurit24@gmail.com Changed status to publish February 13, 2025