python smtplib email

监控系统须要触发报警邮件, 简单笔记一下的用到的库.html

 

smtplib

class smtplib.SMTP([host[, port[, local_hostname[, timeout]]]])

返回一个 smtp 实例, 若是指定了 host 和 port, 会调用 SMTP.connect() 进行链接, timout 指定超时时间python

 

class smtplib.SMTP_SSL([host[, port[, local_hostname[, keyfile[, certfile[, timeout]]]]]])

返回一个 ssl 模式的 smtp 实例, 仅在 SMTP.starttls() 没法或不推荐时使用服务器

 

SMTP.set_debuglevel(level)

设置 debug 输出级别. 若是 level 设定为真值, 则会输出全部的调试信息和整个链接过程当中收到和发送的信息.ide

 

SMTP.connect([host[, port]])

链接到指定 host 的端口. 默认 host 为 localhost, 默认端口为25测试

 

SMTP.helo([hostname])

跟 SMTP 邮件服务器介绍下本身, 通常状况下不须要直接执行此命令, 直接调用 SMTP.sendmail()ui

 

SMTP.ehlo([hostname])

跟 ESMTP 邮件服务器 say hellothis

 

SMTP.verify(address)

确认邮件地址的有效性, 若是有效会返回 code 250 和完成的有点地址, 大部分邮件服务器会屏蔽此命令以防垃圾邮件spa

 

SMTP.login(userpassword)

登陆到邮件服务器debug

 

SMTP.starttls([keyfile[, certfile]])

 

将 smtp 链接切换到 tls 模式调试

 

SMTP.sendmail(from_addrto_addrsmsg[, mail_optionsrcpt_options])

发邮件

from_addr    string    发件人

to_addrs       list        收件人

msg             string     信息

 

SMTP.quit()

终止 smtp 会话, 关闭连接.

 

SMTP Example

import smtplib

def prompt(prompt):
    return raw_input(prompt).strip()

fromaddr = prompt("From: ")
toaddrs  = prompt("To: ").split()
print "Enter message, end with ^D (Unix) or ^Z (Windows):"

# Add the From: and To: headers at the start!
msg = ("From: %s\r\nTo: %s\r\n\r\n"
       % (fromaddr, ", ".join(toaddrs)))
while 1:
    try:
        line = raw_input()
    except EOFError:
        break
    if not line:
        break
    msg = msg + line

print "Message length is " + repr(len(msg))

server = smtplib.SMTP('localhost')
server.set_debuglevel(1)
server.sendmail(fromaddr, toaddrs, msg)
server.quit()

注意到 msg 须要添加一个邮件头, 格式是 

"""

From: test@gmail.com

To: test@gmail.com, test1@gmail.com, test2@gmail.com



邮件正文

"""

显然这样攒 msg 不是一个好办法, 因此 python 提供了 email

 

邮件发送 html 

#!/usr/bin/env python

import smtplib

from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

# me == my email address
# you == recipient's email address
me = "my@email.com"
you = "your@email.com"

# Create message container - the correct MIME type is multipart/alternative.
msg = MIMEMultipart('alternative')
msg['Subject'] = "Link"
msg['From'] = me
msg['To'] = you

# Create the body of the message (a plain-text and an HTML version).
text = "Hi!\nHow are you?\nHere is the link you wanted:\nhttps://www.python.org"
html = """\
<html>
  <head></head>
  <body>
    <p>Hi!<br>
       How are you?<br>
       Here is the <a href="https://www.python.org">link</a> you wanted.
    </p>
  </body>
</html>
"""

# Record the MIME types of both parts - text/plain and text/html.
part1 = MIMEText(text, 'plain')
part2 = MIMEText(html, 'html')

# Attach parts into message container.
# According to RFC 2046, the last part of a multipart message, in this case
# the HTML message, is best and preferred.
msg.attach(part1)
msg.attach(part2)

# Send the message via local SMTP server.
s = smtplib.SMTP('localhost')
# sendmail function takes 3 arguments: sender's address, recipient's address
# and message to send - here it is sent as one string.
s.sendmail(me, you, msg.as_string())
s.quit()

1. 注意到 MIMEMultipart 和 MIMEText, 从返回对象的关系看, MIMEText 的对象能够被 attach 到 MIMEMultipart 返回的对象上

2. 若是实际测试这段代码, 会发现虽然 attach 两次, 可是收到的只有一个 html 的内容, 这跟 MIMEMultipart("alternative") 有关

3. 若是初始化时选择 MIMEMultipart("mixed"), 会发现邮件内容是 text 文本, 同时携带一个 .html 的附件

4. 若是只选择 attach(part2), 发现邮件内容是 html

5. MIMEMultipart 的实例, 包含邮件标题 Subject, 发件人 From, 收件人 To, 最后 attach MIMEText 的实例

6. 这里注意, 若是有多个收件人, To 应该是以 ; 间隔的字符串,  虽然使用 , 间隔也能够成功, 可是 ; 是标准的写法

7. 这跟 sendmail 中的收件人是不一样的, sendmail 中的收件人是 list 列表, 若是把 To 做为参数传给 sendmail, 那么只会有第一我的收到邮件 

 

class email.mime.multipart.MIMEMultipart([_subtype[, boundary[, _subparts[, _params]]]])

_subtype

multipart/mixed

A number of different resources are combined in a single message.             

 

multipart/alternative

The section 5.1.4 of RFC 2046 defines multipart/alternative MIME type to allow the sender to provide different, interchangeable representations of the same message and to leave it up to the receiver to chose the form of presentation most suitable for its capabilities

 

class email.mime.text.MIMEText(_text[, _subtype[, _charset]])

若是发送中文, 须要指定 _charset="utf8"

相关文章
相关标签/搜索