python 自动发送邮件

ps:第一次写博客,仅供个人学习参考

python 自动发送邮件

python库自带的两个模块,email和smtplib模块,直接import就能用

smtplib:主要用于连接邮箱服务器,登录邮箱,发送邮件(指定发件人,收件人,内容)

email:主要用于构建邮件内容(文本、html、图片文件附件等)

1.smtplib

	# 普通连接,明文传输
    smtp = smtplib.SMTP('email_server', port)
    # SSL加密,用于QQ等邮件发送
    smtp = smtplib.SMTP_SSL('email_server', port)
    smtp.login('username', 'password')
    # receiver可以是一个列表;msg.as_string()是email模块所构建的内容
    smtp.sendmail('sender', 'receiver', msg.as_string())
    # 结束回话
    smtp.quit()

邮箱开启smtp服务才能成功连接,普通连接端口是25,SSL加密连接端口是其他,如下图
python 自动发送邮件_第1张图片

注意:这里的password并非邮箱密码,而是开启smtp服务后的授权码

python 自动发送邮件_第2张图片

2.email

常用的模块有:MIMEText,MIMEImage,MIMEMultipart

2.1 MIMEText
纯文本

# 构造文字内容
text = "this is a test"
text_plain = MIMEText(text, 'plain', 'utf-8')

超文本

 	# html
    html = """
        
            
                

this is a test,don't scared!

click it plz! """ msg_html = MIMEText(html, 'html', 'utf-8')

文件附件

	file = open(u'数字权利激活.rar','rb').read()
    msg_file = MIMEText(file,'base64','utf-8')
    msg_file['Content-Disposition'] = "attachment;filename='数字权利激活.rar'"
    msg_file['Content-Type'] = 'application/otcet-stream'

2.2 MIMEImage

	photo_file = open(rage your dream.jpg', 'rb').read()
    msg_image = MIMEImage(photo_file)
    msg_image['Content-Disposition'] = "attachment;filename='rage your dream.jpg'"

2.3 MIMEMultipart

常见的multipart类型有三种:multipart/alternative,multipart/related和multipart/mixed。

  • 邮件类型为"multipart/alternative"的邮件包括纯文本正文(text/plain)和超文本正文(text/html)。
  • 邮件类型为"multipart/related"的邮件正文中包括图片,声音等内嵌资源。
  • 邮件类型为"multipart/mixed"的邮件包含附件。向上兼容,如果一个邮件有纯文本正文,超文本正文,内嵌资源,附件,则选择mixed类型。
#我们一般用
msg = MIMEMultipart(‘mixed’)

我们必须把Subject,From,To,Date添加到MIMEText对象或者MIMEMultipart对象中,邮件中才会显示主题,发件人,收件人,时间(若无时间,就默认一般为当前时间,该值一般不设置)。

	sender = '[email protected]'
    password = 'ddlwzfotwvtwbeeg'
    fromer = 'xx'
    subject = u'这只是一个测试'
    receiver = ['[email protected]']
    server = 'smtp.qq.com'
    port = 465
    msg = MIMEMultipart('mixed')
    msg['Subject'] = subject
    msg['To'] = ';'.join(receiver)
    msg['From'] = fromer
    msg['Date'] = '2019-2-20'

说明:

  • msg.add_header(_name,_value,**_params):添加邮件头字段。
  • msg.as_string():是将msg(MIMEText对象或者MIMEMultipart对象)变为str,如果只有一个html超文本正文或者plain普通文本正文的话,一般msg的类型可以是MIMEText;如果是多个的话,就都添加到MIMEMultipart,msg类型就变为MIMEMultipart。
  • msg.attach(MIMEText对象或MIMEImage对象):将MIMEText对象或MIMEImage对象添加到MIMEMultipart对象中。MIMEMultipart对象代表邮件本身,MIMEText对象或MIMEImage对象代表邮件正文。

以上的构造的文本,超文本,附件,图片都何以添加到MIMEMultipart(‘mixed’)中:

msg.attach(msg_plain)    
msg.attach(msg_html)    
msg.attach(msg_file)    
msg.attach(msg_image)

附完整代码:

# coding: utf-8 
# @Time    : 2019/2/19 19:58
# @Author  : lsn
# @Email   : [email protected]
# @File    : emali_auto_send.py
# @Software: PyCharm
# function : 自动发送邮件,文本图片附件
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
import smtplib

if __name__ == '__main__':
    username = '[email protected]'
    password = 'ddlwzfotwvtwbeeg'
    fromer = 'xx'
    subject = u'这只是一个测试'
    receiver = ['[email protected]']
    server = 'smtp.qq.com'
    port = 465
    msg = MIMEMultipart('mixed')
    msg['Subject'] = subject
    msg['To'] = ';'.join(receiver)
    msg['From'] = fromer
    msg['Date'] = '2019-2-20'
    # html
    html = """
        
            
                

this is a test,don't scared!

click it plz! """ msg_html = MIMEText(html, 'html', 'utf-8') msg.attach(msg_html) # 图片 photo_file = open('rage panda.jpg', 'rb').read() msg_image = MIMEImage(photo_file) msg_image['Content-Disposition'] = "attachment;filename='rage your dream.jpg'" msg.attach(msg_image) # file 文件 file = open(u'数字权利激活.rar','rb').read() msg_file = MIMEText(file,'base64','utf-8') msg_file['Content-Disposition'] = "attachment;filename='数字权利激活.rar'" msg_file['Content-Type'] = 'application/otcet-stream' msg.attach(msg_file) # 普通连接,明文传输 # smtp = smtplib.SMTP(server, port) # SSL加密,用于QQ等邮件发送 smtp = smtplib.SMTP_SSL(server, port) smtp.login(username, password) # receiver可以是一个列表;msg.as_string()是email模块所构建的内容 smtp.sendmail(username, receiver, msg.as_string()) # 结束回话 smtp.quit() pass

参考:https://blog.csdn.net/xc_zhou/article/details/81021525

你可能感兴趣的:(正在路上)