向其他人发送电子邮件是一件重要的事情,在开发中它可以用来发送一些代码,如 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("[email protected]", "yyaz pgow khtd xeqn") # Create a multipart message msg = MIMEMultipart() msg['From'] = "[email protected]" msg['To'] = "[email protected]" 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()
如果您遇到任何困难,请随时提问:)
来源:
免责声明: 提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发到邮箱:[email protected] 我们会第一时间内为您处理。
Copyright© 2022 湘ICP备2022001581号-3