向其他人发送电子邮件是一件很重要的事情,在开发中它可以用来发送一些代码,如 otp、pin、身份验证等
最近,我有一个项目,需要我能够向用户发送电子邮件以获取 otp 代码,结果非常简单。
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
# creates SMTP session
s = smtplib.SMTP('smtp.gmail.com', 587)
# start TLS for security
s.starttls()
# Authentication
s.login("your_email@gmail.com", "yyaz pgow khtd xeqn")
# Create a multipart message
msg = MIMEMultipart()
msg['From'] = "your_email@gmail.com"
msg['To'] = "send_to_email@gmail.com"
msg['Subject'] = "Subject of the Email"
message = "How are you mate? This is a test email sent using Python"
# Attach the message body
msg.attach(MIMEText(message, 'plain'))
# Send the email
s.send_message(msg)
# terminating the session
s.quit()

如果您遇到任何困难,请随时提问:)
来源:
立即学习“Python免费学习笔记(深入)”;
以上就是使用 Python SMPT 和 Gmail 发送电子邮件很简单!的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号