python邮件发送smtplib使用详解

时间:2019-06-14 12:52来源:计算机教程
1 发送文本信息 本文实例为大家分享了python邮件发送smtplib使用具体代码,供大家参考,具体内容如下 '''加密发送文本邮件'''def sendEmail(from_addr,password,to_addr,smtp_server): try: msg = MIMEText('你

1 发送文本信息

本文实例为大家分享了python邮件发送smtplib使用具体代码,供大家参考,具体内容如下

'''加密发送文本邮件'''
def sendEmail(from_addr,password,to_addr,smtp_server):
 try:
  msg = MIMEText('你好,来自信息化工程所的问候...', 'plain', 'utf-8') # 文本邮件
  # msg = MIMEText('<html><body><h1>你好</h1>'   '<p>send by <a href="http://www.python.org" rel="external nofollow" rel="external nofollow" >信息化工程所
    </a>...</p>'  '</body></html>', 'html', 'utf-8') # 网页文件
  msg['From'] = _format_addr('信息化工程所 <%s>' % from_addr)
  msg['To'] = _format_addr('收件人 <%s>' % to_addr)
  msg['Subject'] = Header('邮件的主题:问候', 'utf-8').encode()
  server = smtplib.SMTP(smtp_server, 25)
  server.starttls() # 调用starttls()方法,就创建了安全连接
  # server.set_debuglevel(1) # 记录详细信息
  server.login(from_addr, password) # 登录邮箱服务器
  server.sendmail(from_addr, [to_addr], msg.as_string()) # 发送信息
  server.quit()
  print("加密后邮件发送成功!")
 except Exception as e:
  print("发送失败:"   e)

文件形式的邮件

2 发送带图片附件的邮件

#!/usr/bin/env python3 
#coding: utf-8 
import smtplib 
from email.mime.text import MIMEText 
from email.header import Header 

sender = '***' 
receiver = '***' 
subject = 'python email test' 
smtpserver = 'smtp.163.com' 
username = '***' 
password = '***' 

msg = MIMEText('你好','text','utf-8')#中文需参数‘utf-8',单字节字符不需要 
msg['Subject'] = Header(subject, 'utf-8') 

smtp = smtplib.SMTP() 
smtp.connect('smtp.163.com') 
smtp.login(username, password) 
smtp.sendmail(sender, receiver, msg.as_string()) 
smtp.quit() 
'''发送带图片附件的邮件'''
def sendFileEmail(from_addr,password,to_addr,smtp_server):
 try:
  msg = MIMEMultipart()
  msg['From'] = _format_addr('信息化工程所 <%s>' % from_addr)
  msg['To'] = _format_addr('收件人 <%s>' % to_addr)
  msg['Subject'] = Header('邮件的主题:问候', 'utf-8').encode()
  # 邮件正文是MIMEText:
  msg.attach(MIMEText('send with file...', 'plain', 'utf-8'))
  # msg.attach(MIMEText('<html><body><h1>你好</h1>'   '<p>send by <img src=cid:0"></p>'  '</body></html>', 'html', 'utf-8')) # 网页文件

  # 添加附件就是加上一个MIMEBase,从本地读取一个图片:
  with open(r'./file/图片.png', 'rb') as f:
   mime = MIMEBase('image', 'png', filename='图片.png') # 设置附件的MIME和文件名,这里是png类型:
   mime.add_header('Content-Disposition', 'attachment',filename=('gbk', '', '图片.png')) # 加上必要的头信息,解决中文附件名乱码
   mime.add_header('Content-ID', '<0>')
   mime.add_header('X-Attachment-Id', '0')
   mime.set_payload(f.read()) # 把附件的内容读进来:
   encoders.encode_base64(mime) # 用Base64编码:
   msg.attach(mime) # 添加到MIMEMultipart:
  server = smtplib.SMTP(smtp_server, 25)
  # server.set_debuglevel(1) # 记录详细信息
  server.login(from_addr, password) # 登录邮箱服务器
  server.sendmail(from_addr, to_addr, msg.as_string()) # 发送信息
  server.quit()
  print("带图片邮件发送成功!")
 except Exception as e:
  print("发送失败:"   e)

vnsc5858威尼斯城官网,HTML形式的邮件

3 发送带图片附件的邮件

#!/usr/bin/env python3 
#coding: utf-8 
import smtplib 
from email.mime.text import MIMEText 

sender = '***' 
receiver = '***' 
subject = 'python email test' 
smtpserver = 'smtp.163.com' 
username = '***' 
password = '***' 

msg = MIMEText('<html><h1>你好</h1></html>','html','utf-8') 

msg['Subject'] = subject 

smtp = smtplib.SMTP() 
smtp.connect('smtp.163.com') 
smtp.login(username, password) 
smtp.sendmail(sender, receiver, msg.as_string()) 
smtp.quit() 
'''发送带图片附件的邮件'''
def sendFilesEmail(from_addr,password,to_addr,smtp_server):
 try:
  msg = MIMEMultipart()
  msg['From'] = _format_addr('信息化工程所 <%s>' % from_addr)
  msg['To'] = _format_addr('收件人 <%s>' % to_addr)
  msg['Subject'] = Header('邮件的主题:问候', 'utf-8').encode()
  # 邮件正文是MIMEText:
  msg.attach(MIMEText('发送多附件邮件...', 'plain', 'utf-8'))
  #---这是附件部分---
  #xlsx类型附件
  part = MIMEApplication(open(r'./file/foo.xlsx','rb').read())
  part.add_header('Content-Disposition', 'attachment', filename="foo.xlsx")
  msg.attach(part)
  #jpg类型附件
  part = MIMEApplication(open(r'./file/图片.png','rb').read())
  part.add_header('Content-Disposition', 'attachment', filename=('gbk', '', '图片.png'))
  msg.attach(part)
  #pdf类型附件
  part = MIMEApplication(open(r'./file/foo.pdf','rb').read())
  part.add_header('Content-Disposition', 'attachment', filename="foo.pdf")
  msg.attach(part)
  # #mp3类型附件
  # part = MIMEApplication(open('foo.mp3','rb').read())
  # part.add_header('Content-Disposition', 'attachment', filename="foo.mp3")
  # msg.attach(part)
  server = smtplib.SMTP(smtp_server, 25,timeout=30)
  # server.set_debuglevel(1) # 记录详细信息
  server.login(from_addr, password) # 登录邮箱服务器
  server.sendmail(from_addr, to_addr, msg.as_string()) # 发送信息
  server.quit()
  print("带图片邮件发送成功!")
 except Exception as e:
  print("发送失败:"   e)

带图片的HTML邮件

4 完整代码

#!/usr/bin/env python3 
#coding: utf-8 
import smtplib 
from email.mime.multipart import MIMEMultipart 
from email.mime.text import MIMEText 
from email.mime.image import MIMEImage 

sender = '***' 
receiver = '***' 
subject = 'python email test' 
smtpserver = 'smtp.163.com' 
username = '***' 
password = '***' 

msgRoot = MIMEMultipart('related') 
msgRoot['Subject'] = 'test message' 

msgText = MIMEText('<b>Some <i>HTML</i> text</b> and an image.<br><img src="cid:image1"><br>good!','html','utf-8') 
msgRoot.attach(msgText) 

fp = open('h:\python\1.jpg', 'rb') 
msgImage = MIMEImage(fp.read()) 
fp.close() 

msgImage.add_header('Content-ID', '<image1>') 
msgRoot.attach(msgImage) 

smtp = smtplib.SMTP() 
smtp.connect('smtp.163.com') 
smtp.login(username, password) 
smtp.sendmail(sender, receiver, msgRoot.as_string()) 
smtp.quit() 
from email import encoders
from email.header import Header
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email.mime.application import MIMEApplication
from email.utils import parseaddr, formataddr
import smtplib
def _format_addr(s):
 name, addr = parseaddr(s)
 return formataddr((Header(name, 'utf-8').encode(), addr))
'''加密发送文本邮件'''
def sendEmail(from_addr,password,to_addr,smtp_server):
 try:
  msg = MIMEText('你好,来自信息化工程所的问候...', 'plain', 'utf-8') # 文本邮件
  # msg = MIMEText('<html><body><h1>你好</h1>'   '<p>send by <a href="http://www.python.org" rel="external nofollow" rel="external nofollow" >
    信息化工程所</a>...</p>'  '</body></html>', 'html', 'utf-8') # 网页文件
  msg['From'] = _format_addr('信息化工程所 <%s>' % from_addr)
  msg['To'] = _format_addr('收件人 <%s>' % to_addr)
  msg['Subject'] = Header('邮件的主题:问候', 'utf-8').encode()
  server = smtplib.SMTP(smtp_server, 25)
  server.starttls() # 调用starttls()方法,就创建了安全连接
  # server.set_debuglevel(1) # 记录详细信息
  server.login(from_addr, password) # 登录邮箱服务器
  server.sendmail(from_addr, [to_addr], msg.as_string()) # 发送信息
  server.quit()
  print("加密后邮件发送成功!")
 except Exception as e:
  print("发送失败:"   e)

'''发送带图片附件的邮件'''
def sendFileEmail(from_addr,password,to_addr,smtp_server):
 try:
  msg = MIMEMultipart()
  msg['From'] = _format_addr('信息化工程所 <%s>' % from_addr)
  msg['To'] = _format_addr('收件人 <%s>' % to_addr)
  msg['Subject'] = Header('邮件的主题:问候', 'utf-8').encode()
  # 邮件正文是MIMEText:
  msg.attach(MIMEText('send with file...', 'plain', 'utf-8'))
  # msg.attach(MIMEText('<html><body><h1>你好</h1>'   '<p>send by <img src=cid:0"></p>'  '</body></html>', 'html', 'utf-8')) # 网页文件

  # 添加附件就是加上一个MIMEBase,从本地读取一个图片:
  with open(r'./file/图片.png', 'rb') as f:
   mime = MIMEBase('image', 'png', filename='图片.png') # 设置附件的MIME和文件名,这里是png类型:
   mime.add_header('Content-Disposition', 'attachment',filename=('gbk', '', '图片.png')) # 加上必要的头信息,解决中文附件名乱码
   mime.add_header('Content-ID', '<0>')
   mime.add_header('X-Attachment-Id', '0')
   mime.set_payload(f.read()) # 把附件的内容读进来:
   encoders.encode_base64(mime) # 用Base64编码:
   msg.attach(mime) # 添加到MIMEMultipart:
  server = smtplib.SMTP(smtp_server, 25)
  # server.set_debuglevel(1) # 记录详细信息
  server.login(from_addr, password) # 登录邮箱服务器
  server.sendmail(from_addr, to_addr, msg.as_string()) # 发送信息
  server.quit()
  print("带图片邮件发送成功!")
 except Exception as e:
  print("发送失败:"   e)

'''发送带图片附件的邮件'''
def sendFilesEmail(from_addr,password,to_addr,smtp_server):
 try:
  msg = MIMEMultipart()
  msg['From'] = _format_addr('信息化工程所 <%s>' % from_addr)
  msg['To'] = _format_addr('收件人 <%s>' % to_addr)
  msg['Subject'] = Header('邮件的主题:问候', 'utf-8').encode()
  # 邮件正文是MIMEText:
  msg.attach(MIMEText('发送多附件邮件...', 'plain', 'utf-8'))
  #---这是附件部分---
  #xlsx类型附件
  part = MIMEApplication(open(r'./file/foo.xlsx','rb').read())
  part.add_header('Content-Disposition', 'attachment', filename="foo.xlsx")
  msg.attach(part)
  #jpg类型附件
  part = MIMEApplication(open(r'./file/图片.png','rb').read())
  part.add_header('Content-Disposition', 'attachment', filename=('gbk', '', '图片.png'))
  msg.attach(part)
  #pdf类型附件
  part = MIMEApplication(open(r'./file/foo.pdf','rb').read())
  part.add_header('Content-Disposition', 'attachment', filename="foo.pdf")
  msg.attach(part)
  # #mp3类型附件
  # part = MIMEApplication(open('foo.mp3','rb').read())
  # part.add_header('Content-Disposition', 'attachment', filename="foo.mp3")
  # msg.attach(part)
  server = smtplib.SMTP(smtp_server, 25,timeout=30)
  # server.set_debuglevel(1) # 记录详细信息
  server.login(from_addr, password) # 登录邮箱服务器
  server.sendmail(from_addr, to_addr, msg.as_string()) # 发送信息
  server.quit()
  print("带图片邮件发送成功!")
 except Exception as e:
  print("发送失败:"   e)

if __name__ == '__main__':
 from_addr = 'webter@tccxfw.com' # 邮箱登录用户名
 password = 'TCC123'    # 登录密码
 to_addr = ['1943840362463@qq.com','jason1423@vip.qq.com','jason14449905@126.com'
]  # 发送对象地址,可以多个邮箱
 smtp_server='172.16.254.46'   # 服务器地址,默认端口号25
 sendEmail(from_addr,password,to_addr,smtp_server)

带附件的邮件

以上这篇Python实现邮件的批量发送的示例代码就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

#!/usr/bin/env python3 
#coding: utf-8 
import smtplib 
from email.mime.multipart import MIMEMultipart 
from email.mime.text import MIMEText 
from email.mime.image import MIMEImage 

sender = '***' 
receiver = '***' 
subject = 'python email test' 
smtpserver = 'smtp.163.com' 
username = '***' 
password = '***' 

msgRoot = MIMEMultipart('related') 
msgRoot['Subject'] = 'test message' 

#构造附件 
att = MIMEText(open('h:\python\1.jpg', 'rb').read(), 'base64', 'utf-8') 
att["Content-Type"] = 'application/octet-stream' 
att["Content-Disposition"] = 'attachment; filename="1.jpg"' 
msgRoot.attach(att) 

smtp = smtplib.SMTP() 
smtp.connect('smtp.163.com') 
smtp.login(username, password) 
smtp.sendmail(sender, receiver, msgRoot.as_string()) 
smtp.quit() 

编辑:计算机教程 本文来源:python邮件发送smtplib使用详解

关键词: