向其他人发送电子邮件是一件很重要的事情,在开发中它可以用来发送一些代码,如 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中文网其它相关文章!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号