The code is as follows:
#! /usr/bin/env python
Import Smtplib
From Email.mime.text import Mimetext
Import PSYCOPG2
Import datetime
Mail_host= "SMTP. xxx.com "mail_user=" xxx "mail_pass=" xxx "mail_postfix=" xxx.com "
Def send_mail (to_list,sub,content): me= "Remind Email" + "<" +mail _user+ "@" +mail_postfix+ ">" msg = mimetext (content,_subtype= ' Plain ', _charset= ' gb2312 ') msg[' Subject '] = sub msg[' from '] = me msg[' to '] = ; ". Join (to_list) try: 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 ## Compare local time to database project time # #today =strftime ('%y-%m-%d ', localtime ()) today_date =datetime.datetime (Today, '%y-%m-%d ') ## links to PostgreSQL database ## pgdb_conn = psycopg2. CONNECT (database = ' test ',user = ' username ', password = ' 123456 ', host = ' 1.1.1.1 ') pg_cursor = pgdb_conn.cursor () sql_desc = "Select * from tbl_product3 "Pg_cursor.execute (SQL_DESC) for row in pg_cursor: date=row[0] to_list=row[1] sub= row[2] content=row[3] other_date=datetime.datetime (date, '%y-%m-%d ') delta=other _date-today_date if delta.days == 1: send_mail (to_list,sub,content) pg_cursor.close () pgdb_conn.close ()
This article is from the "Blue Fin Dolphin" blog, please be sure to keep this source http://likunheng.blog.51cto.com/9527645/1569445
Python reads the PostgreSQL database and sends the relevant reminder message