积淀                                            

Python读取数据库发送邮件脚本

程序语言 赵浮云 220℃ 0评论

Python读取数据库发送邮件脚本

[[email protected] ~]# cat sedemail.py
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from email import encoders
from email.header import Header
from email.mime.text import MIMEText
from email.utils import parseaddr, formataddr

import smtplib
import mysql.connector
import time

conn = mysql.connector.connect(user='xxxxxxxxx', password='xxxxxxx', database='xxxxxxx')
cursor = conn.cursor()
cursor.execute('select email from user')
values = cursor.fetchall()
print values
#print type(values)
#for i in values:
#       print i;
#print values[1]
#values = ['[email protected]','[email protected]']


def _format_addr(s):
    name, addr = parseaddr(s)
    return formataddr((Header(name, 'utf-8').encode(), addr))

from_addr = '[email protected]'
password = 'xxxxxxxxx'
to_addr = values
smtp_server = 'smtp.qq.com'

msg = MIMEText('hello, 你好,你申请的每月100G翻墙流量以装载,期待你的使用!祝你愉快!来自http://ss.zfuyun.top', 'plain', 'utf-8')
msg['From'] = _format_addr('ss-zfuyun专注翻墙 <%s>' % from_addr)
msg['To'] = _format_addr('管理员 <%s>' % to_addr)
msg['Subject'] = Header('来自ss-zfuyun科学上网的重要通知', 'utf-8').encode()

server = smtplib.SMTP(smtp_server, 25)
server.set_debuglevel(1)
server.login(from_addr, password)
#server.sendmail(from_addr, [to_addr], msg.as_string())
#server.quit()


for i in to_addr:
	print type(i)
	print i
	server.sendmail(from_addr,i, msg.as_string())
	time.sleep(2)
server.quit()

 

转载请注明:赵浮云的blog » Python读取数据库发送邮件脚本

喜欢 (0)or分享 (0)
发表我的评论
取消评论
表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
(2)个小伙伴在吐槽
  1. You're very eloquent.
    • I very thank you!!!
      赵浮云2017-01-14 22:30 回复