How to send emails in Python
Answered
37
I need to send automated emails from my Python script. How do I do this?
D
Asked by
db_admin
Silver
•
415 rep
1 Answer
5
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)
Y
Platinum
•
311 rep
Your Answer
You need to be logged in to answer questions.
Log In to Answer