37
I need to send automated emails from my Python script. How do I do this?
Loading...
Q&A · Answered
I need to send automated emails from my Python script. How do I do this?
Accepted answer
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
# Email configuration
smtp_server = "smtp.gmail.com"
port = 587
sender = "your@gmail.com"
password = "app_password"
# Create message
msg = MIMEMultipart()
msg['From'] = sender
msg['To'] = "recipient@example.com"
msg['Subject'] = "Test Email"
body = "This is a test email from Python."
msg.attach(MIMEText(body, 'plain'))
# Send email
with smtplib.SMTP(smtp_server, port) as server:
server.starttls()
server.login(sender, password)
server.send_message(msg)Say what you tried, not only what worked — the next person needs the reasoning. Code goes in a fenced block.