前言:按照人事的想法,能不能帮助他们定时发送员工生日祝福邮件。
正好学到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 ="""
Dear:
拨动祝福的琴弦,为你唱出世界上最美妙的生日歌;
点亮幸福的烛光,为你送上精美的礼品;
祝生日快乐,一帆风顺;
爱你的 新云联集团
PS:小礼物已备好,记得到人资部领取哦
"""
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','')
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 ="""
Dear:
拨动祝福的琴弦,为你唱出世界上最美妙的生日歌;
点亮幸福的烛光,为你送上精美的礼品;
祝生日快乐,一帆风顺;
爱你的 新云联集团
PS:小礼物已备好,记得到人资部领取哦
"""
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','')
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函数,把查询的数据作为变量发送