Python发送邮件


:Mr.zhou  阅读: 2,103 次

  通过 Python 脚本使用第三方SMTP服务器发送邮件。

  此脚本可以通过传参的形式使用已经配置好的第三方SMTP服务器发送邮件。

  写脚本使用 Python 3.5.1 ,CentOS 7 自带的 Python 2.7.5 测试可以正常使用。

  使用脚本时需要传入三个参数,分别是:’收件人’ ‘邮件主题’ ‘邮件正文’ 。收件人可以是多个,格式为: ‘收件人1,收件人2’

  使用脚本发送的邮件记录都写到了一个文件中,文件路径可以在脚步中定义。

  SMTP 的账号和密码也需要在脚本中定义。

[root@z-dig scripts]# ./send_mail.py 
Usage : ./send_mail.py 'ToUser1,ToUser2' 'Subject' 'Body'
[root@z-dig scripts]# 

  脚本内容:

#!/usr/bin/python
#/root/python/Python/bin/python3
# admin@z-dig.com
# www.z-dig.com

import smtplib
import os
import socket
from sys import argv
from email.message import Message
from datetime import datetime

# Define Log Path

log_path='/tmp/sendmail.log'

# Define SMTP Server Port Account Password

smtp=dict()
smtp['host']='smtp.z-dig.com'
smtp['port']=25
smtp['account']='service@z-dig.com'
smtp['password']='password'

if len(argv) != 4 :

  print("Usage : {} 'ToUser1,ToUser2' 'Subject' 'Body'".format(argv[0]))
  exit()

msg=Message()
msg['To']=argv[1]
msg['From']=smtp['account']
msg['Subject']=argv[2]
msg.set_payload(argv[3])
msg.set_charset('utf-8')

log=dict()
log['time']=datetime.today()
log['to']=msg['To']
log['subject']=msg['Subject']
log['body']=argv[3]

try :
  socket.setdefaulttimeout(10)
  server=smtplib.SMTP(smtp['host'],smtp['port'])
  server.login(smtp['account'],smtp['password'])
  server.sendmail(smtp['account'],msg['To'].split(','),msg.as_string())
  server.quit()
  log['status']='Success'
except :
  log['status']='Failed'
  print("Failed")

logs=("Time:{}\tTo:{}\tSubject:{}\tBody:{}\tStatus:{}\n".format(log['time'],log['to'],log['subject'],log['body'],log['status']))

f=open(log_path,"a")
f.write(logs)
f.close()
[root@z-dig scripts]#  

 示例:

[root@z-dig scripts]# cat /tmp/sendmail.log
cat: /tmp/sendmail.log: No such file or directory
[root@z-dig scripts]#
[root@z-dig scripts]# ./send_mail.py 'admin@z-dig.com' 'Send mail through python' 'Hello world!'
[root@z-dig scripts]#
[root@z-dig scripts]# cat /tmp/sendmail.log
Time:2016-05-17 23:33:01.682830 To:admin@z-dig.com      Subject:Send mail through python        Body:Hello world!       Status:Success
[root@z-dig scripts]# 

  将脚本中的 smtp[‘host’] 改为错误的值,模拟网络错误,修改smtp[‘password’]的值模拟认证错误:

[root@z-dig scripts]# ./send_mail.py 'admin@z-dig.com' 'smtp server host is wrong' 'Hello world!'
Failed
[root@z-dig scripts]#
[root@z-dig scripts]# cat /tmp/sendmail.log
Time:2016-05-17 23:33:01.682830 To:admin@z-dig.com      Subject:Send mail through python        Body:Hello world!       Status:Success
Time:2016-05-17 23:35:30.244009 To:admin@z-dig.com      Subject:smtp server host is wrong       Body:Hello world!       Status:Failed
[root@z-dig scripts]# 
[root@z-dig scripts]# ./send_mail.py 'admin@z-dig.com' 'password is wrong' 'Hello world!'
Failed
[root@z-dig scripts]# cat /tmp/sendmail.log 
Time:2016-05-17 23:33:01.682830 To:admin@z-dig.com      Subject:Send mail through python        Body:Hello world!       Status:Success
Time:2016-05-17 23:35:30.244009 To:admin@z-dig.com      Subject:smtp server host is wrong       Body:Hello world!       Status:Failed
Time:2016-05-17 23:40:35.975172 To:admin@z-dig.com      Subject:password is wrong       Body:Hello world!       Status:Failed
[root@z-dig scripts]# 

  使用这个脚本发送告警邮件还是可以的…而且还有记录。

  后期可以写成模块。方便调用。


转载请注明原文链接:http://www.z-dig.com/python-email.html



正文部分到此结束