Last active
February 16, 2023 14:59
-
-
Save binderclip/6d993af3d24edae1cfc4 to your computer and use it in GitHub Desktop.
Python & Email,附上用 Python 发送 QQ 邮箱邮件的代码
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import smtplib | |
from getpass import getpass | |
def prompt(prompt): | |
return input(prompt).strip() | |
fromaddr = prompt("From: ") | |
toaddrs = prompt("To: ").split() | |
subject = prompt("Subject: ") | |
print("Enter message, end with ^D (Unix) or ^Z (Windows):") | |
# Add the From: To: and Subject: headers at the start! | |
msg = ("From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n" | |
% (fromaddr, ", ".join(toaddrs), subject)) | |
while True: | |
try: | |
line = input() | |
except EOFError: | |
break | |
if not line: | |
break | |
msg = msg + line | |
print("Message length is", len(msg)) | |
server = smtplib.SMTP_SSL('smtp.qq.com') | |
# 如果是其他的服务,只需要更改 host 为对应地址,port 对对应端口即可 | |
# server = smtplib.SMTP_SSL(host='smtp.qq.com', port=465) | |
server.set_debuglevel(1) # 开启调试,会打印调试信息 | |
print("--- Need Authentication ---") | |
username = prompt("Username: ") | |
password = getpass("Password: ") | |
server.login(username, password) | |
server.sendmail(fromaddr, toaddrs, msg) | |
server.quit() |
Email 相关资源
- 协议相关
- Flask 模块
- 邮箱的设置
- 用 Python 发送邮件的例程
- Python 相关的文档
回复 linbay : 看来你是在python 2 的环境下使用本脚本,这个脚本是python 3的, 在两个版本中,2的input会识别输入类型自动转换,而3中input返回的是str类型。此处也不应使用qq好号,都应该使用邮箱账号为好。
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Email Wiki Reading Notes
Email - Wikipedia, the free encyclopedia
An Internet email message consists of three components
Originally ASCII text-only
Multipurpose Internet Mail Extensions (MIME)
i18n email address using UTF-8
RFC
using a message envelope separate from the message (header and body) itself
Origin
历史和起源大概是从上世纪六十年代开始的。
Operation
其他可选:
Message format
header fields body
Message Header
one fields name-vaule
printable character
separator
MIME encoded words
Must include:
Should include:
Other:
Message body
MIME 标准
Plain text and HTML,用 HTML 似乎比较好呢
Servers and client applications
Filename extensions
URI schema mailto
Types
Use
Problems
U.S. government
See also
还有一些其他的小主题。
Reference
Further reading
External links
零碎感兴趣的链接