Python Send mail

Source: Internet
Author: User

Scenario: When a Web monitor matches a corresponding keyword, the matching content is put into the Redis database. The Redis database is constantly increasing over time.
Function: Realize when the data in Redis database increases, want the user to send an email, remind to view.
Code:

# -*- coding: utf-8 -*-' @note:  to enable monitoring of the Redis database with changes to send mail ' Import redisimport  timeimport smtplib  from email.mime.text import mimetext red=redis. Strictredis ("172.16.2.156", 6379,8)  mailto_list=[' [email protected] ']  #邮件接收方mail_host = " Smtp.163.com "   #设置邮件服务器mail_user =" ABCD "     #用户名mail_pass =" abcd123 "     #口令  mail_postfix= "163.com"    #发件箱的后缀   def send_mail (to_list,sub,content):       me= "Scan_result" + "<" +mail_user+ "@" +mail_postfix+ ">"        msg = mimetext (content,_subtype= ' plain ', _charset= ' gb2312 ')        msg[' Subject '] = sub      msg[' from '] = me       msg[' to '] =  ";". Join (to_list)       try:    &nbsP;     server = smtplib. SMTP ()           server.connect (mail_host)            server.login (Mail_user,mail_pass)            server.sendmail (Me, to_list, msg.as_string ())            server.close ()            return true      except exception, e:           print str (e)            return false     def loop ():     lis=red.hgetall (' Alert '). VALUES ()     num=len (LIS)     time.sleep      lis=red.hgetall (' alert '). VALUES ()     num_2=len (LIS)    if num==num_2:        print  ' No  new keyword added '     else:         if send_mail (mailto_list, "keywords", "there have a keyword added  Please check inredis databases ... "):               print  "Send email success ..."            else:               print  "Send email false ..."      if __name__ = =  ' __main__ ':     while true:        try:               loop ()          excEpt:            continue 


This article is from the "Black Time" blog, so be sure to keep this source http://blacktime.blog.51cto.com/11722918/1795312

Python Send mail

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.