Home >Backend Development >Python Tutorial >How to send emails in Python?

How to send emails in Python?

PHP中文网
PHP中文网Original
2017-06-21 16:48:151300browse

import smtplib
from email.mime.text import MIMEText
_user = "1147016115@qq.com"#Sender
_pwd = "wcpxldrtuthagjbc"#qqmail authorization code
_to = "1208832227@qq.com"#Recipient

msg = MIMEText("Hellow,This is my first Email!")#Email content
msg["Subject"] = "come form xieolei! "#The email subject displayed by the recipient
msg["From"] = _user#The sender displayed by the recipient
msg["To"] = _to#The recipient displayed by the recipient

try:
s = smtplib.SMTP_SSL("smtp.qq.com", 465)
s.login(_user, _pwd)
s.sendmail(_user, _to, msg. as_string())
 s.quit()

 print ("Success!")

except smtplib.SMTPException.e:

 print ("Falied,%s"%e)

Run result:

## Attached: Code source code:

import smtplibfrom email.mime.text import MIMEText
_user = "1147016165@qq.com"#发件人_pwd = "hhvzrkmhnrfgicee"#qq邮箱授权码_to = "1208832367@qq.com"#收件人msg = MIMEText("Hellow,This is my first Email!")#邮件内容msg["Subject"] = "come form xieolei!"#收件方显示的邮件主题msg["From"] = _user#收件方显示的发件人msg["To"] = _to#收件方显示的收件人try:
     s = smtplib.SMTP_SSL("smtp.qq.com", 465)
     s.login(_user, _pwd)
     s.sendmail(_user, _to, msg.as_string())
     s.quit()     print ("Success!")except smtplib.SMTPException.e:     print ("Falied,%s"%e)

View Code

How to obtain QQ authorization code:

Enter personal QQ mailbox》Settings》Account

Figure 1: Open IMAP/SMTP as shown

Figure 2: Generate authorization code

############ ### ###

The above is the detailed content of How to send emails in Python?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn