Home > Backend Development > Python Tutorial > Python implements SMTP email sending

Python implements SMTP email sending

高洛峰
Release: 2017-03-02 16:45:49
Original
1208 people have browsed it

一直想着给框架添加邮件发送功能、所以整理下python下邮件发送功能

首先python是支持邮件的发送、内置smtp库、支持发送纯文本、HTML及添加附件的邮件。之后是邮箱、像163、qq、新浪等邮箱默认关闭SMTP服务,需要我们手动打开,打开后通过发件人邮箱、授权密码 通过发件人的SMTP服务发送

代码如下:

#!/usr/bin/env python
# -*- coding: utf_8 -*-

from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.multipart import MIMEBase
from email import encoders
from email.header import Header
from email.utils import parseaddr, formataddr
import smtplib


class SendEmail:
 outbox = "pythondldysl01@163.com"
 # 发件箱地址
 password = "wxqcl258258"
 # 授权密码 不是邮箱登录密码
 inbox = "xxx@qq.com"
 # 收件箱地址
 smtp_server = "smtp.163.com"
 # 发件箱服务器地址

 def __init__(self):
 pass

 @classmethod
 def _format_address(cls, text):
 name, address = parseaddr(text)
 return formataddr((Header(name, "utf-8").encode(), address))

 @classmethod
 def send_email_text(cls):
 msg = MIMEText("测试smtp邮件发送功能", "plain", "utf-8")
 # 第一个参数:邮件正文
 # 第二个参数:邮件类型 纯文本
 # 第三个参数:编码

 msg["From"] = SendEmail._format_address("来自163的一封邮件 <%s>" % SendEmail.outbox)
 # 发件人姓名与发件箱地址
 msg["To"] = SendEmail._format_address("管理员 <%s>" % SendEmail.inbox)
 # 收件人姓名与收件箱地址
 msg["Subject"] = Header("来自SMTP的问候", "utf-8").encode()
 # 邮件标题

 try:
  server = smtplib.SMTP(SendEmail.smtp_server, 25)
  # 构造smtp服务器连接
  # server.set_debuglevel(1)
  # debug输出模式 默认关闭
  server.login(SendEmail.outbox, SendEmail.password)
  # 登录smtp服务器
  server.sendmail(SendEmail.outbox, [SendEmail.inbox], msg.as_string())
  # 发送邮件
  server.quit()
  print "邮件发送成功"
 except Exception, e:
  print str(e)
  print "邮件发送失败"
  
if __name__ == &#39;__main__&#39;:
 SendEmail.send_email_text()
Copy after login

这只是纯文本的内容、可以支持HTML格式的内容、修改内容如下:

msg = MIMEText("测试smtp邮件发送功能", "plain", "utf-8")

内容修改成HTML格式、 “plain”改成 “html”

最后是添加附件的邮件

代码如下:

@classmethod
 def send_email_multipart(cls):
 msg = MIMEMultipart()

 msg["From"] = SendEmail._format_address("来自163的一封邮件 <%s>" % SendEmail.outbox)
 # 发件人姓名与发件箱地址
 msg["To"] = SendEmail._format_address("管理员 <%s>" % SendEmail.inbox)
 # 收件人姓名与收件箱地址
 msg["Subject"] = Header("来自SMTP的问候", "utf-8").encode()
 # 邮件标题

 msg.attach(MIMEText("测试添加附件的smtp邮件发送功能", "plain", "utf-8"))

 with open("E:\\work\\python project\\CreateProject\\20160421140953.xml", "rb") as f:
  # 设置附件的MIME和文件名
  mime = MIMEBase("xml", "xml", filename="测试报告.xml")
  # 加上必要的头信息
  mime.add_header(&#39;Content-Disposition&#39;, &#39;attachment&#39;, filename="测试报告.xml")
  mime.add_header(&#39;Content-ID&#39;, &#39;<0>&#39;)
  mime.add_header(&#39;X-Attachment-Id&#39;, &#39;0&#39;)
  # 把附件的内容读进来:
  mime.set_payload(f.read())
  # 用Base64编码:
  encoders.encode_base64(mime)
  # 添加到MIMEMultipart:
  msg.attach(mime)

 try:
  server = smtplib.SMTP(SendEmail.smtp_server, 25)
  # 构造smtp服务器连接
  # server.set_debuglevel(1)
  # debug输出模式 默认关闭
  server.login(SendEmail.outbox, SendEmail.password)
  # 登录smtp服务器
  server.sendmail(SendEmail.outbox, [SendEmail.inbox], msg.as_string())
  # 发送邮件
  server.quit()
  print "邮件发送成功"
 except Exception, e:
  print str(e)
  print "邮件发送失败"
Copy after login


以上就是python邮件发送功能的具体实现代码,希望对大家的学习有所帮助。

更多python实现SMTP邮件发送相关文章请关注PHP中文网!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template