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发腾讯微博代码分享
Jan 10 Python
python实现输入数字的连续加减方法
Jun 22 Python
python操作excel文件并输出txt文件的实例
Jul 10 Python
python 实现批量xls文件转csv文件的方法
Oct 23 Python
Python学习笔记之读取文件、OS模块、异常处理、with as语法示例
Jun 04 Python
Django url,从一个页面调到另个页面的方法
Aug 21 Python
Pycharm中Python环境配置常见问题解析
Jan 16 Python
详解Python3中的 input() 函数
Mar 18 Python
浅谈Selenium 控制浏览器的常用方法
Dec 04 Python
聊一聊python常用的编程模块
May 14 Python
Python 机器学习工具包SKlearn的安装与使用
May 14 Python
yolov5返回坐标的方法实例
Mar 17 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
ThinkPHP使用心得分享-上传类UploadFile的使用
2014/05/15 PHP
使用PHP函数scandir排除特定目录
2014/06/12 PHP
PHP面试题之文件目录操作
2015/10/15 PHP
PHP中file_exists使用中遇到的问题小结
2016/04/05 PHP
phpcmsv9.0任意文件上传漏洞解析
2020/10/20 PHP
jquery 查找iframe父级页面元素的实现代码
2011/08/28 Javascript
jquery聚焦文本框与扩展文本框聚焦方法
2012/10/12 Javascript
js中文逗号转英文实现
2014/02/11 Javascript
BootstrapValidator实现注册校验和登录错误提示效果
2017/03/10 Javascript
Vue 实现拖动滑块验证功能(只有css+js没有后台验证步骤)
2018/08/24 Javascript
iview的table组件自带的过滤器实现
2019/07/12 Javascript
js+springMVC 提交数组数据到后台的实例
2019/09/21 Javascript
微信小程序拖拽排序列表的示例代码
2020/07/08 Javascript
vue+swiper实现左右滑动的测试题功能
2020/10/30 Javascript
[14:03]2017DOTA2亚洲邀请赛开幕式:12神兵演绎水墨中华
2017/04/01 DOTA
Python中input和raw_input的一点区别
2014/10/21 Python
Python实现复杂对象转JSON的方法示例
2017/06/22 Python
Python实现的求解最小公倍数算法示例
2018/05/03 Python
python实现批量图片格式转换
2020/06/16 Python
利用pyecharts实现地图可视化的例子
2019/08/12 Python
python 遗传算法求函数极值的实现代码
2020/02/11 Python
Python接口测试文件上传实例解析
2020/05/22 Python
浅谈Python __init__.py的作用
2020/10/28 Python
详解css3中的伪类before和after常见用法
2020/11/17 HTML / CSS
Cecil Mode法国在线商店:女性时尚
2021/01/08 全球购物
大学生求职简历的自我评价范文
2013/10/12 职场文书
银行办理业务介绍信
2014/01/18 职场文书
学习雷锋寄语大全
2014/04/11 职场文书
学校搬迁方案
2014/06/15 职场文书
临床专业自荐信
2014/06/22 职场文书
中国梦演讲稿5分钟
2014/08/19 职场文书
党性分析自查总结
2014/10/14 职场文书
房屋产权共有协议书范本
2014/11/03 职场文书
教师个人总结范文
2015/02/11 职场文书
2016年学生会感恩节活动总结
2016/04/01 职场文书
SONY AN-LP1 短波有源天线放大器图
2022/04/05 无线电