Python 基于Python实现邮件发送

简介: Python 基于Python实现邮件发送

基于Python实现邮件发送

 


测试环境:

Python版本:Python 2.7

 

注:需要修改mimetypes.py文件(该文件可通过文章底部的网盘分享连接获取),否则会报错,类似如下

mimetypes.guess_type 'ascii' codec can't decode byte 0xb0 in position 1: ord

 

 

实现功能:

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

支持不同的邮箱;

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

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

 

目录
相关文章
|
10月前
|
数据安全/隐私保护 Python
Python实现邮件发送(含详细设置方法),并总结自己遇到的错误
Python实现邮件发送(含详细设置方法),并总结自己遇到的错误
|
7月前
|
前端开发 JavaScript Java
【实操】SpringBoot监听Iphone15邮件提醒,Selenium+Python自动化抢购脚本
本文介绍了一个结合SpringBoot和Python的实用功能,旨在监控iPhone 15的库存状态并通过邮件提醒用户。系统采用SpringBoot监听苹果官网API,解析JSON数据判断是否有货,并展示最近的库存记录。此外,还能自动触发Selenium+Python脚本实现自动化购买。文中详细介绍了技术栈、接口分析、邮件配置及自动化脚本的设置方法。该项目不仅适用于熟悉后端开发的人员,也适合回顾Layui和Jquery等前端技术。
93 0
【实操】SpringBoot监听Iphone15邮件提醒,Selenium+Python自动化抢购脚本
|
3月前
|
安全 API 文件存储
Yagmail邮件发送库:如何用Python实现自动化邮件营销?
本文详细介绍了如何使用Yagmail库实现自动化邮件营销。Yagmail是一个简洁强大的Python库,能简化邮件发送流程,支持文本、HTML邮件及附件发送,适用于数字营销场景。文章涵盖了Yagmail的基本使用、高级功能、案例分析及最佳实践,帮助读者轻松上手。
137 4
|
4月前
|
开发者 Python
使用Python实现自动化邮件通知:当长时程序运行结束时
本文介绍了如何使用Python实现自动化邮件通知功能,当长时间运行的程序完成后自动发送邮件通知。主要内容包括:项目背景、设置SMTP服务、编写邮件发送函数、连接SMTP服务器、发送邮件及异常处理等步骤。通过这些步骤,可以有效提高工作效率,避免长时间等待程序结果。
160 9
|
8月前
|
监控 网络协议 网络安全
SMTP操作使用详解并通过python进行smtp邮件发送示例
SMTP操作使用详解并通过python进行smtp邮件发送示例
222 3
|
9月前
|
Windows Python
每日自动发邮件(Python +QQ邮箱 + Windows 10定时任务)
每日自动发邮件(Python +QQ邮箱 + Windows 10定时任务)
每日自动发邮件(Python +QQ邮箱 + Windows 10定时任务)
|
8月前
|
机器学习/深度学习 数据采集 自然语言处理
Python基于词袋模型特征和TFIDF特征进行支持向量机模型中文邮件分类项目实战
Python基于词袋模型特征和TFIDF特征进行支持向量机模型中文邮件分类项目实战
|
10月前
|
安全 数据安全/隐私保护 开发者
Python实现简单的邮件发送系统
Python实现简单的邮件发送系统
111 3
|
10月前
|
运维 Shell Linux
第十四章 Python发送邮件(常见四种邮件内容)
第十四章 Python发送邮件(常见四种邮件内容)
|
10月前
|
API Python
Python邮件与日历管理
【4月更文挑战第13天】Python 通过 `smtplib` 和 `email` 发送邮件,`imaplib` 接收邮件。`google-api-python-client` 库用于管理 Google Calendar,示例代码展示了列出日历事件的功能。要使用 Google Calendar API,需设置服务帐户凭据和范围。
86 1