基于Python实现邮件发送

import smtplib
from email.mime.text import MIMEText
email_host = 'smtp.163.com' # 邮箱地址
email_user = 'szz_mail@163.com' # 发送者账号
email_pwd ='woshinige123' # 发送者密码

maillist = '1204044738@qq.com'
# 收件人邮箱,多个账号的话,用逗号隔开
msg = MIMEText('hai all:'
'这是邮件的内容请查收,请看附件') # 邮件内容
msg['Subject'] = '测试邮件' # 邮件主题
msg['From'] = email_user # 发送者账号
msg['To'] = maillist # 接收者账号列表
smtp = smtplib.SMTP(email_host, port=25)
# 连接邮箱,传入邮箱地址,和端口号,smtp的端口号是25
smtp.login(email_user, email_pwd) # 发送者的邮箱账号,密码
smtp.sendmail(email_user, maillist, msg.as_string())
# 参数分别是发送者,接收者,第三个是把上面的发送邮件的内容变成字符串
smtp.quit() # 发送完毕后退出smtp
print('成功')

实现功能:(这是转发的):

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

支持不同的邮箱;

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

支持一次性发送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

转载于:https://www.cnblogs.com/zln1021/p/6068817.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值