Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Python & Email,附上用 Python 发送 QQ 邮箱邮件的代码
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()
@binderclip

This comment has been minimized.

Copy link
Owner Author

@binderclip binderclip commented Aug 4, 2015

用这个可以交互式的填写邮件,然后用自己的 QQ 邮箱发送一个简单的邮件出去。From 地址可以自己填的,但是如果和登陆的不一样会被 QQ 邮箱的服务器阻止掉。

如果对 Email 的协议之类不熟悉的话建议先看一下 Wiki,花不了很多时间,一劳永逸。

其他参考资料:

发送有格式或者带附件的邮件可以参考下面的文档:

@binderclip

This comment has been minimized.

Copy link
Owner Author

@binderclip binderclip commented Apr 4, 2016

Email Wiki Reading Notes

Email - Wikipedia, the free encyclopedia

An Internet email message consists of three components

  • envelope
  • header
  • body

Originally ASCII text-only
Multipurpose Internet Mail Extensions (MIME)
i18n email address using UTF-8

RFC

  • RFC 561 in 1973
  • SMTP RFC 821 in 1982

using a message envelope separate from the message (header and body) itself

Origin

历史和起源大概是从上世纪六十年代开始的。

Operation

  1. 在电脑上填写的内容通过 MUA 发给 MSA
  2. MSA 去分析并寻找域名
  3. 对方返回 IP 地址
  4. 通过 MTA 发送到对方的 MTA
  5. MDA 把收到的递送给用户
  6. 用户的 MUA 用 POP3 或者 IMAP 协议来接收

其他可选:

  • 可能两人用的是一个整体的邮件系统
  • 不用 MUA,而是 webmail
  • A 的机器可能本身就可以 MTA
  • B 也有很多读取邮件的方法,登陆 mx.b.org 或者也用 webmail
  • 域名可能有好几个邮件中转服务器

Message format

  • 现在的 5322
  • 多媒体附件的 2045 - 2049 MIME
  • 5322 替代了 2822 (2008) 替代了 822 (2001) 用了将近 20 年。
  • 822 基础是 733

header fields body

Message Header

one fields name-vaule
printable character
separator
MIME encoded words

Must include:

  • From
  • Date

Should include:

  • Message-ID
  • In-Reply-To

Other:

  • To
  • Subject
  • Bcc
  • Cc
  • Content-Type, usually MIME
  • Precedence
  • References
  • Reply-To
  • Sender
  • Archived-At

Message body

MIME 标准

Plain text and HTML,用 HTML 似乎比较好呢

Servers and client applications

  • MTA
  • MDA, LDA (POP, IMAP)
  • MUA

Filename extensions

  • eml
  • emlx
  • msg
  • mbx

URI schema mailto

Types

  • Web-based email
  • POP3 email services,似乎比较传统
  • IMAP,似乎更适用与多设备的情况
  • MAPI,微软的

Use

  • Flaming,随意发泄
  • Email bankruptcy,看不完
  • In business,
  • Pros
  • Cons
  • Research on email marketing
  • Mobile

Problems

  • Attachment size limitation,本来没有限制的,但是可能会被服务商加上限制
  • Tracking of sent mail,靠一些机制来完成状态跟踪

U.S. government

See also

还有一些其他的小主题。

Reference

Further reading

External links

零碎感兴趣的链接

@binderclip

This comment has been minimized.

Copy link
Owner Author

@binderclip binderclip commented Apr 10, 2016

@linbay

This comment has been minimized.

Copy link

@linbay linbay commented Apr 22, 2016

2016-04-22_10h12_51
2016-04-22_10h14_36

您好,請教一下,看不出來如何使用,是要在script裡先宣告什麼嗎?
謝謝

@Choumingzhao

This comment has been minimized.

Copy link

@Choumingzhao Choumingzhao commented Dec 30, 2016

回复 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
You can’t perform that action at this time.