生活随笔
收集整理的這篇文章主要介紹了
python读取postgresql数据库并发送相关提醒邮件
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
代碼如下:
#!/usr/bin/env python
import smtplib
from email.mime.text import MIMEText
import psycopg2
import datetime
from time import *
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??##?取本地時間與數據庫項目時間作比較?##
today=strftime('%Y-%m-%d',localtime())
today_date=datetime.datetime.strptime(today,'%Y-%m-%d')##?與postgresql數據庫鏈接?##????????
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_dateif?delta.days?==?1:send_mail(to_list,sub,content)pg_cursor.close()??
pgdb_conn.close()
轉載于:https://blog.51cto.com/likunheng/1569445
總結
以上是生活随笔為你收集整理的python读取postgresql数据库并发送相关提醒邮件的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。