python2.7实现邮件发送功能

yipeiwu_com5年前Python基础

要想实现一个能够发送带有文本、图片、附件的python程序,首先要熟悉两大模块:

email以及smtplib

然后对于MIME(邮件扩展)要有一定认知,因为有了扩展才能发送附件以及图片这些媒体或者非文本信息

最后一个比较细节的方法就是MIMEMultipart,要理解其用法以及对应参数所实现的功能区别

发送邮件三部曲:

创建协议对象
连接邮件服务器
登陆并发送邮件

from email.header import Header
from email.mime.base import MIMEBase
from email.mime.text import MIMEText
import mimetypes

from email.mime.multipart import MIMEMultipart
import os
import smtplib

from email import Encoders as email_encoders


class Message(object):
 def __init__(self, from_addr, to_addr, subject="", html="", text=None, cc_addr=[], attachment=[]):

  self.from_addr = from_addr
  self.subject = subject

  if to_addr:
   if isinstance(to_addr, list):
    self.to_addr = to_addr
   else:
    self.to_addr = [d for d in to_addr.split(',')]
  else:
   self.to_addr = []

  if cc_addr:
   if isinstance(cc_addr, list):
    self.cc_addr = cc_addr
   else:
    self.cc_addr = [d for d in cc_addr.split(',')]
  else:
   self.cc_addr = []

  if html is not None:
   self.body = html
   self.body_type = "html"
  else:
   self.body = text
   self.body_type = "plain"

  self.parts = []
  if isinstance(attachment, list):
   for file in attachment:
    self.add_attachment(file)

 def add_attachment(self, file_path, mimetype=None):
  """
   If *mimetype* is not specified an attempt to guess it is made. If nothing
   is guessed then `application/octet-stream` is used.
  """
  if not mimetype:
   mimetype, _ = mimetypes.guess_type(file_path)

  if mimetype is None:
   mimetype = 'application/octet-stream'

  type_maj, type_min = mimetype.split('/')
  with open(file_path, 'rb') as fh:
   part_data = fh.read()

   part = MIMEBase(type_maj, type_min)
   part.set_payload(part_data)
   email_encoders.encode_base64(part)

   part_filename = os.path.basename(file_path)
   part.add_header('Content-Disposition', 'attachment; filename="%s"'
       % part_filename)
   part.add_header('Content-ID', part_filename)

   self.parts.append(part)

 def __to_mime_message(self):
  """Returns the message as
  :py:class:`email.mime.multipart.MIMEMultipart`."""

  ## To get the message work in iOS, you need use multipart/related, not the multipart/alternative
  msg = MIMEMultipart('related')
  msg['Subject'] = self.subject
  msg['From'] = self.from_addr
  msg['To'] = ','.join(self.to_addr)

  if len(self.cc_addr) > 0:
   msg['CC'] = ','.join(self.cc_addr)

  body = MIMEText(self.body, self.body_type)
  msg.attach(body)

  # Add Attachment
  for part in self.parts:
   msg.attach(part)

  return msg

 def send(self, smtp_server='localhost'):

  smtp = smtplib.SMTP()
  smtp.connect(smtp_server)
  smtp.sendmail(from_addr=self.from_addr, to_addrs=self.to_addr + self.cc_addr, msg=self.__to_mime_message().as_string())
  smtp.close()

对于实际发送程序,要注意个参数的类型,比如from_addr是字符串,to_addr和cc_addr以及attachment都是列表

from mail_base import Message
import datetime
from_addr = 'xxx'
mail_to = 'xxx'


def send_go():
 time_now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M')
 attach_files = ['testcsv.xlsm','test1.jpg','test2.jpg','test3.jpg']
 mail_msg = """
  <p>Hi Lockey:</p>
  <p><img src="cid:test1.jpg"></p>####要特别注意这里,正文插入图片的特殊格式!!!
  <hr/>
  <p style="text-indent:16px">Here is the latest paper link from The Economist, you can click <a href="https://lockeycheng.github.io/iooi/index.html" rel="external nofollow" >Go</a> for a full view!</p>
  <hr/>
  <p>Best Regards</p>
  <p>
   Any question please mail to <a href='mailto:iooiooi23@163.com'>Lockey23</a>.
  </p>
  <p>Sent at {} PST</p>
  """.format(time_now)
 subject = '[Halo] - ' + 'A new paper published!'
 msg = Message(from_addr=from_addr,
     to_addr=[mail_to],
     cc_addr=[mail_to],
     subject=subject,
     attachment=attach_files,
     html=mail_msg
     )
 msg.send()

if __name__ == '__main__':
 send_go()

对于测试程序我们命名为sendGo.py,运行测试程序

~$ python sendGo.py

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python使用正则来处理各种匹配问题

正则表达式是一个特殊的字符序列,它能帮助你方便的检查一个字符串是否与某种模式匹配。本文给大家介绍python使用正则来处理各种匹配问题,具体代码如下所述: import re ##匹...

Python多进程入门、分布式进程数据共享实例详解

本文实例讲述了Python多进程入门、分布式进程数据共享。分享给大家供大家参考,具体如下: python多进程入门 https://docs.python.org/3/library/m...

深入浅析Python字符编码

Python的字符串编码规则一直让我很头疼,花了点时间研究了下,并不复杂。主要涉及的内容有常用的字符编码的特点,并介绍了在python2.x中如何与编码问题作战,本文关于Python的内...

使用Python搭建虚拟环境的配置方法

python 的虚拟环境可以为一个 python 项目提供独立的解释环境、依赖包等资源,既能够很好的隔离不同项目使用不同 python 版本带来的冲突,而且还能方便项目的发布。 virt...

pandas ix &iloc &loc的区别

一开始自学Python的numpy、pandas时候,索引和切片把我都给弄晕了,特别是numpy的切片索引、布尔索引和花式索引,简直就是大乱斗。但是最近由于版本的问题,从之前的Pytho...