使用Python自动发送电子邮件
有一天,小明发现每天给团队成员发送电子邮件通知任务进展非常繁琐,于是决定用python来简化这个过程。
- import smtplib
- from email.mime.text import MIMEText
-
- def send_email(to, subject, body):
- smtp_server = 'smtp.example.com'
- smtp_port = 587
- username = 'your_email@example.com'
- password = 'your_password'
-
- msg = MIMEText(body)
- msg['Subject'] = subject
- msg['From'] = username
- msg['To'] = to
-
- with smtplib.SMTP(smtp_server, smtp_port) as server:
- server.starttls()
- server.login(username, password)
- server.send_message(msg)
-
- 使用示例
- send_email('receiver@example.com', '任务进展通知', '大家好,任务已完成!')