首先安装sendmail
yum install
sendmail
service sendmail
start
安装完成后,sendmail默认可以用localhost来向外面发送邮件。
用python发送邮件:
参考
实例代码如下:
(send_mail.py)
import smtplib
import email
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import datetime
import logging
import config
LOG = logging.getLogger("email")
def _send_mail(to_list,sub,content):
"""
to_list: email to who?
sub: email subject
content: email content
send_mail("aaa@126.com","sub","content")
"""
me=config.mail_user+"<"+config.mail_user+"@"+config.mail_postfix+">"
#msg = MIMEText(content)
msgRoot = MIMEMultipart('related')
msgRoot['Subject'] = sub
msgRoot['From'] = me
msgRoot['To'] = ";".join(to_list)
msgRoot['Date'] = email.Utils.formatdate()
msgRoot.preamble = 'This is a message from ***'
msgAlt = MIMEMultipart('alternative')
msgRoot.attach(msgAlt)
html = content
html_part = MIMEText(html, 'html', 'utf-8')
msgAlt.attach(html_part)
try:
s = smtplib.SMTP()
s.connect(config.mail_host)
#s.login(config.mail_user,config.mail_pass)
s.sendmail(me, to_list, msgRoot.as_string())
s.close()
return True
except Exception, e:
LOG.exception(str(e))
print str(e)
return False
def fill_table(content):
text =""" <p>Hi,</p>
<p>The following messages are from inconsistency check :</p>
<p></p>
"""
head = """<table border=1 style="font-size:10.0pt;border-collapse:collapse;"
bordercolor="black" cellpadding=4>"""
end = "</table>"
sub = """<tr>
<th>Time</th>
<th>Inconsistency Item</th>
<th>Database Value</th>
<th>Actual Check Result</th>
</tr>"""
return text + head + sub + content + end
def send_email(content):
t = str(datetime.datetime.now())
subject = "check result: %s" % t
content = fill_table(content)
if _send_mail(config.mailto_list,subject,content):
print "send email success."
LOG.info("send email:%s success." % subject)
else:
print "send email fail."
LOG.error("send email:%s fail" % subject)
return
其中config.py的配置如下:
# mail to, multi destinations must split with ','
mailto_list = ["aaa@126.com, bbb@126.com"]
# mail from
mail_host="localhost"
mail_user="root"
mail_postfix="bbb.com" #域名
发送email的content的格式如下:
直接调用send_email(self.email_content)即可发送。
def _add_email_content(self, output):
t = str(datetime.datetime.now())
content = """<tr>
<td>%s</td>
<td>%s</td>
<td>%s</td>
<td>%s</td>
</tr>""" % (t, str(output[0]), str(output[1]), str(output[2]))
self.email_content = self.email_content + content
分享到:
相关推荐
Python发邮件代码
详细展示了各种使用python发邮件的程序实例
python发送邮件代码,服务器配置请自行修改,脚本中的地址是内网搭建的邮件服务器。可以结合系统定时任务配置邮件定时发送。希望帮到你
简单的python发送邮件的示例,基于python2.7
python发邮件程序,发送方法:./mail.py 收件人 主题 html内容 图片文件 附件文件'
半天时间写了个python发送邮件的脚本 作者: jeffery ( email:dungeonsnd@126.com, msn:dungeonsnd@hotmail.com, csdn blog:http://blog.csdn.net/dungeonsnd) 时间: 2011-06-19 地点: SH --------------------------...
python发送邮件报表
使用python3发送QQ电子邮件,发送人为自己,接收人可以一个可以多个。
python发送邮件源码有注释
基于Python实现多进程的发送邮件.zip
使用python发送邮件,使用的是smtplib库
这是python发送邮件的源码,支持群发和添加邮件, 文章中有免费下载地址,这里是土豪专用下载地址,
发邮件代码,Python,亲测可用。用pycharm写,直接更改发件人邮箱、密码,和收件人邮箱即可
python发送邮件---包含重发(三次)和定时统计
python发送邮件的脚本 作者: jeffery ( email:dungeonsnd@126.com, msn:dungeonsnd@hotmail.com, csdn blog:http://blog.csdn.net/dungeonsnd) 时间: 2011-07-10 地点: GZ ----------------------------------------...
python发邮件示例
该文档介绍了两种利用python发送邮件的方式,包括响应模块导入,邮件正文等。
当抓取的信息更新的时候,可以通过邮件进行动态通知