最近看到了一个防止DDOS的python脚本,不过其邮件通知功能一直不成功,而最近又在学习python 。打开源码查看,发现有调用smtplib 进行邮件发送 。不过因为里面有调用配置文件参数,略有点复杂。先从网上查了查smtplib的用法 。通过smtplib进行gmail发送代码示例:
#!/usr/bin/env python import smtplib import sys import email.mime.text # my test mail mail_username='361way@gmail.com' mail_password='test' from_addr = mail_username to_addrs=('test@361way.com') # HOST & PORT HOST = 'smtp.gmail.com' PORT = 25 # Create SMTP Object smtp = smtplib.SMTP() print 'connecting ...' # show the debug log smtp.set_debuglevel(1) # connet try: print smtp.connect(HOST,PORT) except: print 'CONNECT ERROR ****' # gmail uses ssl smtp.starttls() # login with username & password try: print 'loginning ...' smtp.login(mail_username,mail_password) except: print 'LOGIN ERROR ****' # fill content with MIMEText's object msg = email.mime.text.MIMEText('Hi ,this is a test mail') msg['From'] = from_addr msg['To'] = ';'.join(to_addrs) msg['Subject']='hello , today is a special day' print msg.as_string() smtp.sendmail(from_addr,to_addrs,msg.as_string()) smtp.quit()
代码挺简单,使用时,只需要把用户、密码、收件人、主题及内容更换即可。