python 发送邮件脚本
一、该脚本适合在 linux 中做邮件发送测试用,只需要填写好 发送账号和密码以及发送人即可,然后使用 python ./filename.py (当前目录下)即可。如果发送出错,会将错误详情抛出来。
#!/usr/bin/env python
# -*- coding: utf-8 -*- __author__ = 'Apollo' import time
import smtplib
from email.mime.text import MIMEText
_user = "" # 发送账号
_pwd = "" # 账号密码
_to = "" # 发送人 def send_email(content):
text = '[%s] Reporting:' % (time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())) try:
msg = MIMEText(content)
msg["Subject"] = text
msg["From"] = _user
msg["To"] = _to #s = smtplib.SMTP("smtp.dc.mydorma.com", timeout=30) # 使用 25 号端口(普通邮件发送)
s = smtplib.SMTP_SSL(host='smtp.qq.com', port=465) # 使用 465 号端口(SSL加密发送)
s.set_debuglevel(1)
s.login(_user, _pwd)
s.sendmail(_user, _to, msg.as_string())
s.quit()
except (smtplib.SMTPAuthenticationError,
smtplib.SMTPConnectError,
smtplib.SMTPDataError,
smtplib.SMTPException,
smtplib.SMTPHeloError,
smtplib.SMTPRecipientsRefused,
smtplib.SMTPResponseException,
smtplib.SMTPSenderRefused,
smtplib.SMTPServerDisconnected) as e:
print 'Warning: %s was caught while trying to send email.\nContent:%s\n' % (e.__class__.__name__, e.message) if __name__ == '__main__':
send_email("Prepare to work:") # 邮件内容
二、该脚本适合使用其它语言(例如PHP)外部执行改 python 脚本来实际发送电子邮件,需要填写好 发送账号和密码即可,其它的参数从 外部传进来,例如php这样调用:
exec("/data/programdir/filename.py $to $subject $content $cc",$out,$result)
若 $result == 0 则发送成功,$result == 1 则发送失败。
#!/usr/bin/env python
# -*- coding: utf-8 -*- __author__ = 'Apollo' import time
import smtplib
import sys # 使用外部传参,必须引入 sys 类库
from email.mime.text import MIMEText
_user = "xxxxx@xx.com" # 发件账号
_pwd = "" # 密码
_to = sys.argv[1] # 发送人
_cc = sys.argv[4] # 转呈人 if _cc.strip()=='':
rcpt = _to
else:
rcpt = [_to] + _cc.split(",") def send_email(content):
text = '[%s] Reporting:' % (time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())) try:
msg = MIMEText(content)
msg["Subject"] = sys.argv[2]
msg["From"] = _user
msg["To"] = _to
msg["Cc"] = _cc s = smtplib.SMTP("xxxx@xx.com", timeout=30)
#s = smtplib.SMTP_SSL(host='smtp.qq.com', port=465)
s.set_debuglevel(1)
#s.login(_user, _pwd) # 当不需要做身份认证的时候,可以屏蔽该行
s.sendmail(_user,rcpt, msg.as_string())
s.quit()
except (smtplib.SMTPAuthenticationError,
smtplib.SMTPConnectError,
smtplib.SMTPDataError,
smtplib.SMTPException,
smtplib.SMTPHeloError,
smtplib.SMTPRecipientsRefused,
smtplib.SMTPResponseException,
smtplib.SMTPSenderRefused,
smtplib.SMTPServerDisconnected) as e:
print 'Warning: %s was caught while trying to send email.\nContent:%s\n' % (e.__class__.__name__, e.message) if __name__ == '__main__':
send_email(sys.argv[3]) # 邮件内容
如有转载,请注明出处:http://www.cnblogs.com/chrdai/p/7791693.html
python 发送邮件脚本的更多相关文章
- Python发送邮件脚本
import smtplib from email.mime.text import MIMEText mailserver = "smtp.163.com" username_s ...
- python发送邮件脚本ssl 465端口
#coding:utf8 from smtplib import SMTP_SSL from email.header import Header from email.mime.text impor ...
- Zabbix调用外部脚本发送邮件:python编写脚本
Zabbix调用外部脚本发送邮件的时候,会在命令行传入两个参数,第一个参数就是要发送给哪个邮箱地址,第二个参数就是邮件信息,为了保证可以传入多个参数,所以假设有多个参数传入 #!/usr/bin/en ...
- Zabbix日志监视的汇总报警(更新发送邮件脚本)
Zabbix的用户一定会碰到这种情况: 日志报警一般设置的是multiple模式,有错误大量写入的时候,每写入一行就会触发一次action,导致出现大量的报警邮件. 特别是ora的报警,经常一出就是上 ...
- python 发送邮件例子
想到用python发送邮件 主要是服务器 有时候会产生coredump文件 ,然后因为脚本重启原因,服务器coredump产生后会重启 但是没有主动通知开发人员 想了下可以写个脚本一旦产生cored ...
- 【转】【Python】Python发送邮件(常见四种邮件内容)
在写脚本时,放到后台运行,想知道执行情况,会通过邮件.SMS(短信).飞信.微信等方式通知管理员,用的最多的是邮件.在linux下,Shell脚本发送邮件告警是件很简单的事,有现成的邮件服务软件或者调 ...
- Python发送邮件(最全)
简单邮件传输协议(SMTP)是一种协议,用于在邮件服务器之间发送电子邮件和路由电子邮件. Python提供smtplib模块,该模块定义了一个SMTP客户端会话对象,可用于使用SMTP或ESMTP侦听 ...
- python接口自动化(三十二)--Python发送邮件(常见四种邮件内容)番外篇——上(详解)
简介 本篇文章与前边没有多大关联,就是对前边有关发邮件的总结和梳理.在写脚本时,放到后台运行,想知道执行情况,会通过邮件.SMS(短信).飞信.微信等方式通知管理员,用的最多的是邮件.在linux下, ...
- Python发送邮件(常见四种邮件内容)
Python发送邮件(常见四种邮件内容) 转载 2017年03月03日 17:17:04 转自:http://lizhenliang.blog.51cto.com/7876557/1875330 ...
随机推荐
- 织梦dedeCMS数据库结构字段说明-简略说明
dede_addonarticle 附加文章表 aid int(11) 文章编号typeid int(11) 分类栏目编号body mediumtext 文章内容dede_addonflash 附加F ...
- MFC CStdioFile
读Text文件 void CNWiReworkDlg::ReadHexFile() { using namespace std; CStdioFile file; file.Open(hexFileP ...
- angularjs入门(二)
angularJs是一个mvc模式, m-model-->当前视图中可用的数据, v-view--> HTML. c-controller-->即 JavaScript 函数,可以添 ...
- 解决Jmeter插件ERROR: java.io.IOException: Agent is unreachable via TCP的错误
今天在centos上搭建jmeter监控服务,服务正常启动,我点击run,就在一切看起来很美好的时候,报错了,ERROR: java.io.IOException: Agent is unreacha ...
- 微信支付之JsApi支付
常见问题:金额错误,微信金额是int类型,最小单位为分,即是1 客户端调用微信支付的时候一闪而过:这个原因是因为微信商户后台支付目录地址没设置对,导致js调用的时候验证没通过 .aspx页面设置: x ...
- PHP: POST Content-Length of xxx bytes exceeds the limit of 8388608 bytes
用户上传了 4 个附件,每个小于 5M,但是总大小超过了 15 M. 在 Nginx 日志中找到了如下错误信息,还没有到 Laravel 日志那一层. 2018/08/13 10:14:38 [err ...
- Codeforces 999F Cards and Joy(二维DP)
题目链接:http://codeforces.com/problemset/problem/999/F 题目大意:有n个人,n*k张卡牌,每个人会发到k张卡牌,每个人都有一种喜欢的卡牌f[i],当一个 ...
- 通配符(WildCard)的使用
一.关于WildCard:一个web应用,有成千上万个action声明,可以利用struts2提供的映射机制把多个彼此相似的映射关系简化成一个映射关系,即通配符. 1.新建类 ActionWildCa ...
- python 全栈开发,Day69(Django的视图层,Django的模板层)
昨日内容回顾 相关命令: 1 创建项目 django-admin startproject 项目名称 2 创建应用 python manage.py startapp app名称 3 启动项目 pyt ...
- python 全栈开发,Day56(jQuery的ajax)
昨日内容回顾 事件流: 1.事件捕获 从最外层到最内层 2.事件目标阶段 3.事件冒泡 从最内层到最外层 每个事件都会事件对象 event 属性和方法 属性: event.target 目标节点(冒泡 ...