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网络编程之TCP通信实例和socketserver框架使用例子
Apr 25 Python
利用matplotlib+numpy绘制多种绘图的方法实例
May 03 Python
详谈python read readline readlines的区别
Sep 22 Python
python判断数字是否是超级素数幂
Sep 27 Python
pycharm运行程序时在Python console窗口中运行的方法
Dec 03 Python
在Anaconda3下使用清华镜像源安装TensorFlow(CPU版)
Apr 19 Python
python和c语言哪个更适合初学者
Jun 22 Python
python操作ini类型配置文件的实例教程
Oct 30 Python
Python Http请求json解析库用法解析
Nov 28 Python
python爬取企查查企业信息之selenium自动模拟登录企查查
Apr 08 Python
Django如何与Ajax交互
Apr 29 Python
pytorch fine-tune 预训练的模型操作
Jun 03 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 MYSQL乱码问题,使用SET NAMES utf8校正
2009/11/30 PHP
php文字水印和php图片水印实现代码(二种加水印方法)
2013/12/25 PHP
JQuery入门——用bind方法绑定事件处理函数应用介绍
2013/02/05 Javascript
防止浏览器记住用户名及密码的简单实用方法
2013/04/22 Javascript
JavaScript中用getDate()方法返回指定日期的教程
2015/06/09 Javascript
javascript实现方法调用与方法触发小结
2016/03/26 Javascript
jQuery ajaxForm()的应用
2016/10/14 Javascript
jQuery自定义图片上传插件实例代码
2017/04/04 jQuery
JS实现二叉查找树的建立以及一些遍历方法实现
2017/04/17 Javascript
AngularJS 单选框及多选框的双向动态绑定
2017/04/20 Javascript
使用Browserify来实现CommonJS的浏览器加载方法
2017/05/14 Javascript
javascript 作用于作用域链的详解
2017/09/27 Javascript
JavaScript原型对象、构造函数和实例对象功能与用法详解
2018/08/04 Javascript
vue-auto-focus: 控制自动聚焦行为的 vue 指令方法
2018/08/25 Javascript
微信小程序实现预览图片功能
2020/10/22 Javascript
bootstrap table实现iview固定列的效果实例代码详解
2019/09/30 Javascript
python开启多个子进程并行运行的方法
2015/04/18 Python
Python中pygame的mouse鼠标事件用法实例
2015/11/11 Python
python爬虫 正则表达式使用技巧及爬取个人博客的实例讲解
2017/10/20 Python
Python编程实现双链表,栈,队列及二叉树的方法示例
2017/11/01 Python
Python实现购物车程序
2018/04/16 Python
Python爬取qq空间说说的实例代码
2018/08/17 Python
Python多继承原理与用法示例
2018/08/23 Python
Django框架orM与自定义SQL语句混合事务控制操作
2019/06/27 Python
python判断一个对象是否可迭代的例子
2019/07/22 Python
安装docker-compose的两种最简方法
2019/07/30 Python
用pandas划分数据集实现训练集和测试集
2020/07/20 Python
django跳转页面传参的实现
2020/09/17 Python
Python爬虫如何破解JS加密的Cookie
2020/11/19 Python
用python批量移动文件
2021/01/14 Python
小学少先队活动方案
2014/02/18 职场文书
十佳青年事迹材料
2014/08/21 职场文书
2014年应急工作总结
2014/12/11 职场文书
大学生,三分钟即兴演讲稿
2019/07/22 职场文书
SQL Server中常用截取字符串函数介绍
2022/03/16 SQL Server
详解Python中的for循环
2022/04/30 Python