Python使用SMTP模块、email模块发送邮件

 

一、smtplib模块:

主要通过SMTP类与邮件系统进行交互。使用方法如下:

1.实例化一个SMTP对象:

  s = smtplib.SMTP(邮件服务地址,端口号)

  s = smtplib.SMTP_SSL(邮件服务地址,端口号)

2.登陆邮件,权限验证:

  s.login(用户名,密码)

3.发送邮件:

  s.sendmail(发件人邮箱,收件人邮箱,发送内容)

4.断开连接:

  s.close()

 

二、email模块:

  email模块:支持发送的邮件内容为纯文本、HTML内容、图片、附件。email模块中有几大类来针对不同的邮件内容形式,常用如下:

  MIMEText:(MIME媒体类型)内容形式为纯文本、HTML页面。

  MIMEImage:内容形式为图片。

  MIMEMultupart:多形式组合,可包含文本和附件。

 

每一类对应的导入方式:

  from email.mime.text import MIMEText

  from email.mime.image import MIMEImage

  from email.mime.multipart import MIMEMultipart

 

三、MIMEText:

  MIMEText(msg,type,chartset)

  msg:文本内容

  type:文本类型默认为plain(纯文本)

   发送HTML格式的时候,修改为html,但同时要求msg的内容也是html的格式。

  chartset:文本编码,中文为“utf-8”

  # 构造TEXT格式的消息

  msg = MIMEText("hello.text","plain","utf-8")

  msg["Subject"] = "xxxxx"

  msg["From"] = "xxxx"

  msg["To"] = "xxxx"

  #发送以上构造的邮件内容要使用as_string将构造的邮件内容转换为string形式。

  s.sendmail("xxx","xxx",msg.as_string)

 

四、MIMEImage、MIMEMultipart:

  msg = MIMEMultipart()

  #实例化一个文本对象 

  msg_sub = MIMEText("hello.text","plain","utf-8")

  #将text消息添加到MIMEMultipart中,作为邮件正文。

  msg.attach(msg_sub)

 

  #图片作为附件

  import os

  img_datas = open(os.getcwd()+ "/reports/xxxx.png","rb").read()

  msg_img = MIMEImage(img_data)

  msg_img.add_header(‘Content-Disposition‘,‘attachment‘, filename = "xxxx.png" )

  msg_img.add_header(‘Content-ID‘,‘<0>‘)

  #将图片添加到MIMEMultiplart中,作为附件发送。

  msg.attach(mag_img)

源代码如下:

发送文本邮件: 

import smtplib
from email.mime.text import MIMEText

sender = ‘‘   #发送人邮箱
passwd = ‘lkugmgywydhfff‘ #发送人邮箱授权码
receivers = ‘‘ #收件人邮箱

subject = ‘python发邮件测试‘ #主题
content = ‘这是我使用python smtplib模块和email模块自动发送的邮件‘    #正文

msg = MIMEText(content,‘plain‘,‘utf-8‘)
msg[‘Subject‘] = subject
msg[‘From‘] = sender
msg[‘TO‘] = receivers

try:
    s = smtplib.SMTP_SSL(‘smtp.qq.com‘,465)
    s.login(sender,passwd)
    s.sendmail(sender,receivers,msg.as_string())
    print(‘发送成功‘)

except Exception:
    print(‘发送失败‘)

 

 发送HTML邮件: 

import smtplib
from email.mime.text import MIMEText
from email.header import Header

sender = ‘[email protected]‘ #发件邮箱
passwd = ‘lkugmgywydhfff‘ #发送人邮箱授权码
receivers = ‘‘   #收件邮箱

subject = ‘python发邮Html邮件测试‘ #主题

content = """         #内容,HTML格式
<p>Python 邮件发送测试...</p>
<p><a href="http://www.baidu.com">这是一个链接</a></p>
"""

msg = MIMEText(content,‘html‘,‘utf-8‘)
# msg[‘Subject‘] = subject
msg[‘Subject‘] = Header(subject,‘utf-8‘)
# msg[‘From‘] = sender
msg[‘From‘] = Header(‘大傻子‘,‘utf-8‘)
# msg[‘To‘] = receivers
msg[‘To‘] = Header(‘二愣子‘,‘utf-8‘)
try:
    s = smtplib.SMTP_SSL(‘smtp.qq.com‘,465)
    s.login(sender,passwd)
    s.sendmail(sender,receivers,msg.as_string())
    print(‘Send Success‘)

except:
    print(‘Send Failure‘)

  

发送图片邮件: 

import smtplib
from email.mime.image import MIMEImage
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart

sender = ‘‘
passwd = ‘lkugmgywydhfff‘
receivers = ‘‘
subject = ‘python发邮带img的邮件测试‘ #主题

# 创建一个带附件的实例
msg = MIMEMultipart()
msg[‘Subject‘] = subject
msg[‘From‘] = sender
msg[‘To‘] = receivers

# 创建正文
msg.attach(MIMEText(‘使用python smtplib模块和email模块自动发送邮件测试‘,‘plain‘,‘utf-8‘))

# 创建图片附件
import os
img_file = open(os.getcwd()+"/a4.jpg",‘rb‘).read()
msg_img = MIMEImage(img_file)
msg_img.add_header(‘Content-Disposition‘,‘attachment‘, filename = "a4.jpg")
msg_img.add_header(‘Content-ID‘, ‘<0>‘)
msg.attach(msg_img)

try:
    s = smtplib.SMTP_SSL(‘smtp.qq.com‘,465)
    s.set_debuglevel(1) #输出发送邮件详细过程
    s.login(sender,passwd)
    s.sendmail(sender,receivers,msg.as_string())
    print(‘Send Succese‘)

except:
    print(‘Send Failure‘)

  

发送带附件的邮件:

import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.header import Header

sender = ‘‘  #发件邮箱
passwd = ‘lkugmgywydhfff‘  # 邮箱授权码
receivers = ‘‘  #收件邮箱

subject = ‘python发带附件的邮件测试‘ #主题
# 创建一个带附件的实例
msg = MIMEMultipart()
msg[‘Subject‘] = subject
msg[‘From‘] = sender
msg[‘To‘] = receivers

#创建正文,将文本文件添加到MIMEMultipart中
msg.attach(MIMEText(‘使用python smtplib模块和email模块自动发送邮件测试‘,‘plain‘,‘utf-8‘))

#构造附件1,传送当前目录下  文件
att1 = MIMEText(open(‘testdata.xlsx‘,‘rb‘).read(),‘base64‘,‘utf-8‘) # rb以二进制方式读取
# att1["Content-Type"] = ‘application/octet-stream‘
# filename为附件名称,可以任意写,写什么名字,邮件中显示什么名字
att1["Content-Disposition"] = ‘attachment; filename = "testdata.xlsx" ‘
#将附件添加到MIMEMultipart中
msg.attach(att1)

#构造附件2
att2 = MIMEText(open(‘db.cfg‘,‘rb‘).read(),‘base64‘,‘utf-8‘)
# att2["Content-Type"] = ‘application/octet-stream‘
att2["Content-Disposition"] = ‘attachment; filename = "db.cfg" ‘
#将附件添加到MIMEMultipart中
msg.attach(att2)

try:
    s = smtplib.SMTP_SSL(‘smtp.qq.com‘,465)
    s.set_debuglevel(1) #输出发送邮件详细过程
    s.login(sender,passwd)
    s.sendmail(sender,receivers,msg.as_string())
    print(‘Send Succese‘)

except:
    print(‘Send Failure‘)
 

相关推荐