首页 > 代码库 > 基于Python实现邮件发送

基于Python实现邮件发送

实现功能:

邮件发送,支持文字,音频文件,文本文件,图形文件,应用程序及其它类型文件的发送;

支持不同的邮箱;

支持一次性往多个邮箱发送;

支持一次性发送n个附件;

支持中文命名的附件发送;

 

mail.conf配置:

[SMTP]
login_user = laiyuhenshuai@163.com
login_pwd = xxxxx
from_addr =  laiyuhenshuai@163.com
to_addrs = [‘mrxxx@163.com‘,‘1033553122@qq.com‘]

host = smtp.163.com
port = 25
 

说明:不同类型的邮箱(发件人邮箱),需要修改配置文件为对应的host和端口

smtp.163.com:25

smtp.qq.com:465

 

 

实践代码:

#!/usr/bin/env python
# -*- coding:GBK -*-
__author__ = ‘shouke‘
 
import ConfigParser
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.audio import MIMEAudio
from email.mime.application import MIMEApplication
import mimetypes
import os
 
class MyMail:
    def __init__(self, mail_config_file):
        config = ConfigParser.ConfigParser()
        config.read(mail_config_file)
 
        self.smtp = smtplib.SMTP()
        self.login_user = config.get(‘SMTP‘, ‘login_user‘)
        self.login_pwd = config.get(‘SMTP‘, ‘login_pwd‘)
        self.from_addr = config.get(‘SMTP‘, ‘from_addr‘)
        self.to_addrs = config.get(‘SMTP‘, ‘to_addrs‘)
        self.host = config.get(‘SMTP‘, ‘host‘)
        self.port = config.get(‘SMTP‘, ‘port‘)
 
    # 连接到服务器
    def connect(self):
        self.smtp.connect(self.host, self.port)
 
    # 登陆邮件服务器
    def login(self):
        try:
            self.smtp.login(self.login_user, self.login_pwd)
        except Exception as e:
            print(‘%s‘ % e)
 
    # 发送邮件
    def send_mail(self, mail_subject, mail_content, attachment_path_set):
         # 构造MIMEMultipart对象做为根容器
        msg = MIMEMultipart()
        msg[‘From‘] = self.from_addr
        # msg[‘To‘] = self.to_addrs        
    msg[‘To‘] = ‘,‘.join(eval_r(self.to_addrs))
        msg[‘Subject‘] = mail_subject
 
        # 添加邮件内容
        content = MIMEText(mail_content, _charset=‘gbk‘)
        msg.attach(content)
 
        for attachment_path in attachment_path_set:
            if os.path.isfile(attachment_path): # 如果附件存在
                type, coding = mimetypes.guess_type(attachment_path)
                if type == None:
                    type = ‘application/octet-stream‘
 
                major_type, minor_type = type.split(‘/‘, 1)
                with open(attachment_path, ‘rb‘) as file:
                    if major_type == ‘text‘:
                        attachment = MIMEText(file.read(), _subtype=minor_type)
                    elif major_type == ‘image‘:
                        attachment = MIMEImage(file.read(),  _subtype=minor_type)
                    elif major_type == ‘application‘:
                        attachment = MIMEApplication(file.read(), _subtype=minor_type)
                    elif major_type == ‘audio‘:
                        attachment = MIMEAudio(file.read(), _subtype=minor_type)
 
                # 修改附件名称
                attachment_name = os.path.basename(attachment_path)
                attachment.add_header(‘Content-Disposition‘, ‘attachment‘, filename = (‘gbk‘, ‘‘, attachment_name))
 
                msg.attach(attachment)
 
        # 得到格式化后的完整文本
        full_text = msg.as_string()
 
        # 发送邮件
        self.smtp.sendmail(self.from_addr, eval_r(self.to_addrs), full_text)
 
    # 退出
    def quit(self):
        self.smtp.quit()
 
if __name__ == ‘__main__‘:
    mymail = MyMail(‘./mail.conf‘)
    mymail.connect()
    mymail.login()
    mail_content = ‘hello,亲,这是一封测试邮件,收到请回复^^ 2014‘
    mymail.send_mail(邮件标题--亲,收到一份邮件,请及时查收, mail_content, {‘d:\\shouke.csv‘, ‘d:\\2345haoya_3.1.1.9229.exe‘,
                                              ‘d:\\shouke.ini‘,‘d:\\shouke.ini‘, ‘d:\\test.mp3‘, ‘d:\\test.png‘, ‘d:\\report20150507204645.html‘,
                                              ‘d:\\1 - 副本.sql‘})
    mymail.quit()

 

pdf版本及mimetypes.py下载地址: http://pan.baidu.com/s/1P3C3W

基于Python实现邮件发送