python邮件处理

 

SMTP

SMTP(Simple Mail Transfer Protocol)即简单邮件传输协议,它是一组用于由源地址到目的地址传送邮件的规则,由它来控制信件的中转方式。
Python对SMTP支持有smtplib和email两个模块,email负责构造邮件,smtplib负责发送邮件。
Python创建 SMTP 对象语法如下:

smtpObj = smtplib.SMTP( [host [, port [, local_hostname]]] )

参数说明:

port: 如果你提供了 
host 参数, 你需要指定
SMTP 服务使用的端口号,一般情况下
SMTP 端口号为25。
local_hostname: 如果 SMTP 在你的本机上,你只需要指定服务器地址为 localhost 即可。

 

Python SMTP 对象使用 sendmail 方法发送邮件,语法如下:

SMTP.sendmail(from_addr, to_addrs, msg[, mail_options, rcpt_options])

参数说明:

from_addr: 邮件发送者地址。
to_addrs: 字符串列表,邮件发送地址。
msg: 发送消息
这里要注意一下第三个参数,msg 是字符串,表示邮件。我们知道邮件一般由标题,发信人,收件人,邮件内容,附件等构成,发送邮件的时候,要注意 msg 的格式。这个格式就是 smtp 协议中定义的格式。

 

通过python发邮件步骤

前提:开通了第三方授权,可以使用smtp服务
1.创建smtp对象
2.连接smtp服务器,默认端口号都是25
3.登陆自己的邮箱账号
4.调用发送消息函数,参数:发件人、收件人、消息内容
5.关闭连接

发送邮件的例子

import email.mime.multipart
import email.mime.text
import smtplib


msg = email.mime.multipart.MIMEMultipart()  #通过这个类创建消息
msg['from'] = '97001@qq.com'  # 邮件发件人
msg['to'] =  '14148@qq.com'  # 邮件收件人
msg['subject'] = 'It's a test mail'  # 邮件主题

context = '''  # 邮件正文
    <h1>老师好</h1>
    你好,
     这是一封自动发送的邮件。
      www.ustchacker.com hello
    '''
text = email.mime.text.MIMEText(_text=context, _subtype="html")  # 定义文本以后,标明将context指定成什么格式,html,txt
msg.attach(text)  # 邮件消息注册

em = smtplib.SMTP_SSL()
em.connect("smtp.qq.com", 465)
em.login("97001@qq.com", '123456')
em.sendmail(from_addr='97001@qq.com', to_addrs='14148@qq.com', msg=msg.as_string())
em.quit()  # 关闭连接

 

发送带附件的邮件

import smtplib
import email.mime.multipart
import email.mime.text
import os
from email import encoders
msg = email.mime.multipart.MIMEMultipart()
msg['from'] = '18910148469@163.com'
msg['to'] = '97001@qq.com;14148@qq.com'
msg['subject'] = 'mail'

context = '''  # 邮件正文
    <h1>老师好</h1>
    你好,
     这是一封自动发送的邮件。
      www.ustchacker.com hello
    '''
    
txt = email.mime.text.MIMEText(_text=content, _subtype="html")
msg.attach(txt)
#############
attfile = 'test.py'
basename = os.path.basename(attfile)
fp = open(attfile, 'rb')  #打开文件,读出文件字符串
att = email.mime.text.MIMEText(fp.read(), 'html', 'utf-8')  #通过MIMT    ext()类来创建一个对象att,传入文件读出内容
att["Content-Type"] = 'application/octet-stream'
# 增加att的头部信息,并指定文件名字
att.add_header('Content-Disposition', 'attachment',filename=('utf-8', '', basename)) #three-tuple of (charset, language, value),encoders.encode_base64(att)
msg.attach(att)  #添加到msg消息中msg.attach(att)
##########
smtp = smtplib.SMTP()
smtp.connect('smtp.163.com', '25')
smtp.login('3456346@163.com', '123456')
smtp.sendmail(from_addr='18910148469@163.com', to_addrs=['97001@qq.com', '14148@qq.com'], msg=msg.as_string())
smtp.quit()

 

使用第三方模块

yagmail库的源代码只有几百行代码,它是对SMTP以及email模块的一个智能封装。

安装

pip install yagmail

 

官方例子

import yagmail
yag = yagmail.SMTP()
contents = [
    "This is the body, and here is just text http://somedomain/image.png",
    "You can find an audio file attached.", '/local/path/to/song.mp3'
]
yag.send('to@someone.com', 'subject', contents)

# Alternatively, with a simple one-liner:
yagmail.SMTP('mygmailusername').send('to@someone.com', 'subject', contents)

 

我们自己写一个

import yagmail


args={
    "user": "12123123@163.com",  # 邮箱账号
    "password": "123456",        # 邮箱密码
    "host": "smtp.163.com",      # SMTP服务器
    "port": "465"                # SMTP服务器端口
}


emailList=["13300000@qq.com","217400007@qq.com", "97400001@qq.com"]      # 可以一次性发送邮件给多个人
email = yagmail.SMTP(**args)
email.send(to='971010101@qq.com',subject="mail",contents="test mail",cc="97460001@qq.com")

详解:

to  发送给谁

subject  主题

content  内容 它传递的是一个列表,这里还可以带附件 contents=[body, 'imag.png', 'test.pdf'] 其中body是正文

cc   抄送给谁

 

posted @ 2018-06-06 22:48  quartzite  阅读(648)  评论(0编辑  收藏  举报