python-定時發送生日郵件祝福
阿新 • • 發佈:2019-03-27
導入 sendmai htm tab database 文件 arc exce Coding
前言:按照人事的想法,能不能幫助他們定時發送員工生日祝福郵件。
正好學到python ,打算練下手
直接附上代碼
個人想法:第一步數據庫建立一張員工信息表;用於記錄員工的名字,生日,郵箱。對於表沒有專門考慮長度,直接用。
CREATE TABLE `birth` (
`id` varchar(255) DEFAULT NULL,
`name` varchar(255) DEFAULT NULL,
`birthtime` varchar(255) DEFAULT NULL,
`mail` varchar(255) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
python-代碼。
創建一個函數,用來查詢用戶當天用戶生日信息
def selectmysql():
db = pymysql.connect("ip", "user", "passwd", "Schema", charset=‘utf8‘ )
cursor = db.cursor()
cursor.execute("select name,DATE_FORMAT(birthtime,‘%m-%d‘),mail from birth where DATE_FORMAT(birthtime,‘%m-%d‘) = DATE_FORMAT(NOW(),‘%m-%d‘)")##查詢當天生日的用戶data = cursor.fetchall()
print("Database version : %s " % (data,))##打印查詢的數據
return data
db.close()
selectmysql()
def sendmail(fmail,fname):
# 第三方 SMTP 服務
#mail_host = "smtp.qq.com" # 設置服務器
#mail_user = "*****@qq.com" # 發件人用戶名
#mail_pass = "*******" # 發件人口令,QQ郵箱是輸入授權碼,在qq郵箱設置裏用驗證過的手機發送短信獲得,不含空格
sender = ‘*****@qq.com‘ # 與發件人用戶名保持一致
#receivers = "*****@qq.com" # 收件人郵箱地址,可設置為你的QQ郵箱或者其他郵箱
receivers = fmail
message = MIMEMultipart()
message[‘From‘] = Header("生日祝福小分隊", ‘utf-8‘)
message[‘To‘] = Header(fname, ‘utf-8‘)#收件人,寫變量
subject = ‘生日快樂‘
message[‘Subject‘] = Header(subject, ‘utf-8‘)
msgAlternative = MIMEMultipart(‘alternative‘)
message.attach(msgAlternative)
mail_msg ="""
<p><font size="2">Dear:<br />撥動祝福的琴弦,為你唱出世界上最美妙的生日歌;<br />點亮幸福的燭光,為你送上精美的禮品;<br />祝生日快樂,一帆風順;<br />愛你的 新雲聯集團<br />PS:小禮物已備好,記得到人資部領取哦</font></p>
<p><img src="cid:image1"></p>
"""
msgAlternative.attach(MIMEText(mail_msg,‘html‘,‘utf-8‘))##文本內容
fp = open(r‘/app/mail/birth2.png‘,‘rb‘)##附件的一張圖片
msgImage = MIMEImage(fp.read())#添加圖片
fp.close()
msgImage.add_header(‘Content-ID‘,‘<image1>‘)
message.attach(msgImage)
try:
#smtpObj = smtplib.SMTP_SSL(mail_host, 465)#根據自己使用的郵箱來設置
smtpObj = smtplib.SMTP_SSL(mail_host,994)#根據自己使用的郵箱來設置
smtpObj.login(mail_user, mail_pass)
smtpObj.sendmail(sender, receivers, message.as_string())
smtpObj.quit()
u"郵件發送成功"
except smtplib.SMTPException as e:
print(‘e‘)
time.sleep(10) # 延時十秒
print(‘End‘)
以上是主要文件
如下是能正常使用,需要導入一些模塊
#!/usr/bin/python
# -*- coding:utf-8 -*-
import smtplib
from email.mime.text import MIMEText
from email.header import Header
import time # 延時設置
import pymysql
import sys
reload(sys)
sys.setdefaultencoding(‘utf8‘)
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
datenow = time.strftime("%m-%d")
print(datenow)
def selectmysql():
db = pymysql.connect("ip", "user", "passwd", "Schema", charset=‘utf8‘ )
cursor = db.cursor()
cursor.execute("select name,DATE_FORMAT(birthtime,‘%m-%d‘),mail from birth where DATE_FORMAT(birthtime,‘%m-%d‘) = DATE_FORMAT(NOW(),‘%m-%d‘)")##查詢當天生日的用戶
data = cursor.fetchall()
print("Database version : %s " % (data,))##打印查詢的數據,(data,)逗號是表示查詢的數據不止一條
return data
db.close()
selectmysql()
def sendmail(fmail,fname):
# 第三方 SMTP 服務
#mail_host = "smtp.qq.com" # 設置服務器
#mail_user = "*****@qq.com" # 發件人用戶名
#mail_pass = "*******" # 發件人口令,QQ郵箱是輸入授權碼,在qq郵箱設置裏用驗證過的手機發送短信獲得,不含空格
sender = ‘*****@qq.com‘ # 與發件人用戶名保持一致
#receivers = "*****@qq.com" # 收件人郵箱地址,可設置為你的QQ郵箱或者其他郵箱
receivers = fmail
message = MIMEMultipart()
message[‘From‘] = Header("生日祝福小分隊", ‘utf-8‘)
message[‘To‘] = Header(fname, ‘utf-8‘)#收件人,寫變量
subject = ‘生日快樂‘
message[‘Subject‘] = Header(subject, ‘utf-8‘)
msgAlternative = MIMEMultipart(‘alternative‘)
message.attach(msgAlternative)
mail_msg ="""
<p><font size="2">Dear:<br />撥動祝福的琴弦,為你唱出世界上最美妙的生日歌;<br />點亮幸福的燭光,為你送上精美的禮品;<br />祝生日快樂,一帆風順;<br />愛你的 新雲聯集團<br />PS:小禮物已備好,記得到人資部領取哦</font></p>
<p><img src="cid:image1"></p>
"""
msgAlternative.attach(MIMEText(mail_msg,‘html‘,‘utf-8‘))##文本內容
fp = open(r‘/app/mail/birth2.png‘,‘rb‘)##附件的一張圖片
msgImage = MIMEImage(fp.read())#添加圖片
fp.close()
msgImage.add_header(‘Content-ID‘,‘<image1>‘)
message.attach(msgImage)
try:
#smtpObj = smtplib.SMTP_SSL(mail_host, 465)#根據自己使用的郵箱來設置
smtpObj = smtplib.SMTP_SSL(mail_host,994)#根據自己使用的郵箱來設置
smtpObj.login(mail_user, mail_pass)
smtpObj.sendmail(sender, receivers, message.as_string())
smtpObj.quit()
u"郵件發送成功"
except smtplib.SMTPException as e:
print(‘e‘)
time.sleep(10) # 延時十秒
print(‘End‘)
if __name__ == ‘__main__‘:
data = selectmysql()
print(data)
for row in data:
fname = row[0]
fdate = row[1]
fmail = row[2]
print("name=%s,birthdate=%s,mail=%s" % (fname,fdate,fmail))
#compare
if fdate == datenow:
sendmail(fmail,fname)###調用sendmail函數,把查詢的數據作為變量發送
python-定時發送生日郵件祝福