Python发送email的3种方法


Posted in Python onApril 28, 2015

python发送email还是比较简单的,可以通过登录邮件服务来发送,linux下也可以使用调用sendmail命令来发送,还可以使用本地或者是远程的smtp服务来发送邮件,不管是单个,群发,还是抄送都比较容易实现。
先把几个最简单的发送邮件方式记录下,像html邮件,附件等也是支持的,需要时查文档即可
1、登录邮件服务

#!/usr/bin/env python  

# -*- coding: utf-8 -*-  

#python2.7x  

#send_simple_email_by_account.py  @2014-07-30  

#author: orangleliu  

  

''''' 

使用python写邮件 simple 

使用126 的邮箱服务 

'''  

  

import smtplib  

from email.mime.text import MIMEText  

  

SMTPserver = 'smtp.126.com'  

sender = 'liuzhizhi123@126.com'  

password = "xxxx"  

  

message = 'I send a message by Python. 你好'  

msg = MIMEText(message)  

  

msg['Subject'] = 'Test Email by Python'  

msg['From'] = sender  

msg['To'] = destination  

  

mailserver = smtplib.SMTP(SMTPserver, 25)  

mailserver.login(sender, password)  

mailserver.sendmail(sender, [sender], msg.as_string())  

mailserver.quit()  

print 'send email success' 

2、调用sendmail命令 (linux)

# -*- coding: utf-8 -*-  

#python2.7x  

#send_email_by_.py  

#author: orangleliu  

#date: 2014-08-15  

''''' 

用的是sendmail命令的方式 

 

这个时候邮件还不定可以发出来,hostname配置可能需要更改 

'''  

  

from email.mime.text import MIMEText  

from subprocess import Popen, PIPE  

  

def get_sh_res():  

    p = Popen(['/Application/2.0/nirvana/logs/log.sh'], stdout=PIPE)  

    return str(p.communicate()[0])  

  

def mail_send(sender, recevier):  

    print "get email info..."  

    msg = MIMEText(get_sh_res())  

    msg["From"] = sender  

    msg["To"] = recevier  

    msg["Subject"] = "Yestoday interface log results"  

    p = Popen(["/usr/sbin/sendmail", "-t"], stdin=PIPE)  

    res = p.communicate(msg.as_string())  

    print 'mail sended ...'  

  

if __name__ == "__main__":  

    s = "957748332@qq.com"  

    r = "zhizhi.liu@chinacache.com"  

    mail_send(s, r) 

3、使用smtp服务来发送(本地或者是远程服务器)
#!/usr/bin/env python  

# -*- coding: utf-8 -*-  

#python2.7x  

#send_email_by_smtp.py  

#author: orangleliu  

#date: 2014-08-15  

''''' 

linux 下使用本地的smtp服务来发送邮件 

前提要开启smtp服务,检查的方法 

#ps -ef|grep sendmail 

#telnet localhost 25 

 

这个时候邮件还不定可以发出来,hostname配置可能需要更改 

'''  

import smtplib  

from email.mime.text import MIMEText  

from subprocess import Popen, PIPE  

  

  

def get_sh_res():  

    p = Popen(['/Application/2.0/nirvana/logs/log.sh'], stdout=PIPE)  

    return str(p.communicate()[0])  

  

def mail_send(sender, recevier):  

    msg = MIMEText(get_sh_res())  

    msg["From"] = sender  

    msg["To"] = recevier  

    msg["Subject"] = "Yestoday interface log results"  

    s = smtplib.SMTP('localhost')  

    s.sendmail(sender, [recevier], msg.as_string())  

    s.quit()  

    print 'send mail finished...'  

  

if __name__ == "__main__":  

    s = "zhizhi.liu@chinacache.com"  

    r =  s  

    mail_send(s, r) 
Python 相关文章推荐
python实现的jpg格式图片修复代码
Apr 21 Python
python 调用win32pai 操作cmd的方法
May 28 Python
Python中常用信号signal类型实例
Jan 25 Python
python hook监听事件详解
Oct 25 Python
对json字符串与python字符串的不同之处详解
Dec 19 Python
python从子线程中获得返回值的方法
Jan 30 Python
Python中整数的缓存机制讲解
Feb 16 Python
python打包成so文件过程解析
Sep 28 Python
python 实现字符串下标的输出功能
Feb 13 Python
Python控制台输出时刷新当前行内容而不是输出新行的实现
Feb 21 Python
pandas按条件筛选数据的实现
Feb 20 Python
Python实现批量自动整理文件
Mar 16 Python
Python中使用partial改变方法默认参数实例
Apr 28 #Python
调试Python程序代码的几种方法总结
Apr 28 #Python
解析Python中的异常处理
Apr 28 #Python
python调用java模块SmartXLS和jpype修改excel文件的方法
Apr 28 #Python
Python EOL while scanning string literal问题解决方法
Sep 18 #Python
python中尾递归用法实例详解
Apr 28 #Python
在Python中使用元类的教程
Apr 28 #Python
You might like
php HtmlReplace输入过滤安全函数
2010/07/03 PHP
对text数据类型不支持代码页转换 从: 1252 到: 936
2011/04/23 PHP
php中对2个数组相加的函数
2011/06/24 PHP
一个简单的网页密码登陆php代码
2012/07/17 PHP
PHP提示Cannot modify header information - headers already sent by解决方法
2014/09/22 PHP
php实现扫描二维码根据浏览器类型访问不同下载地址
2014/10/15 PHP
PHP基础之输出缓冲区基本概念、原理分析
2019/06/19 PHP
PHP safe_mode开启对于PHP系统函数有什么影响
2020/11/10 PHP
加载jQuery后$冲突的解决办法
2010/07/09 Javascript
TreeView 用法(有代码)(asp.net)
2011/07/15 Javascript
jquery实现商品拖动选择效果代码(自写)
2013/05/28 Javascript
JavaScript中访问节点对象的方法有哪些如何使用
2013/09/24 Javascript
JavaScript通过正则表达式实现表单验证电话号码
2014/03/07 Javascript
js控制iframe的高度/宽度让其自适应内容
2014/04/09 Javascript
JS对象与json字符串格式转换实例
2014/10/28 Javascript
AngularJS实现单一页面内设置跳转路由的方法
2017/06/28 Javascript
JS HTML图片显示Canvas 压缩功能
2017/07/21 Javascript
d3.js实现自定义多y轴折线图的示例代码
2018/05/30 Javascript
javascript实现抢购倒计时程序
2019/08/26 Javascript
解决Vue @submit 提交后不刷新页面问题
2020/07/18 Javascript
Python多线程学习资料
2012/12/19 Python
使用C#配合ArcGIS Engine进行地理信息系统开发
2016/02/19 Python
Python设计模式之建造者模式实例详解
2019/01/17 Python
python 切换root 执行命令的方法
2019/01/19 Python
对django layer弹窗组件的使用详解
2019/08/31 Python
python 列表、字典和集合的添加和删除操作
2019/12/16 Python
Python&&GDAL实现NDVI的计算方式
2020/01/09 Python
python调用有道智云API实现文件批量翻译
2020/10/10 Python
python编程的核心知识点总结
2021/02/08 Python
会计专业毕业生自荐信范文
2013/12/20 职场文书
中药专业自荐信范文
2014/03/18 职场文书
品牌转让协议书
2014/08/20 职场文书
党性观念心得体会
2014/09/03 职场文书
特岗教师个人总结
2015/02/10 职场文书
英语投诉信范文
2015/07/03 职场文书
golang 实现对Map进行键值自定义排序
2021/04/28 Golang