读取mongodb利用mailgun发送邮件--python学习

# -*- coding=utf-8 -*-
import smtplib
import time
from pymongo import MongoClient
from email.mime.text import MIMEText
"""
people
[{"id":"贝贝","address":"lghher1991@163.com"},{"id":"航航","address":"lghher1990@163.com"}] 


email
{"sub":"标题","html":"<html><h1>贝贝</h1></html>"}
"""


def sendEmail(u_list,sub,content):
    smtp_host = 'smtp.mailgun.com'
    account = 'postmaster@sandbox4f84c40de65646d191670e618197453b.mailgun.org'
    password='b7504a9360d5239e30b3114b921a7cda'
    msg = MIMEText(content,'html','utf-8')
    msg["Accept-Language"]="zh-CN"
    msg["Accept-Charset"]="ISO-8859-1,utf-8"
    msg['Subject']=sub
    msg['From']='postmaster@sandbox4f84c40de65646d191670e618197453b.mailgun.org'
   # msg['To']=';'.join(u_list)
    msg['To']=(u_list)
    smtp = smtplib.SMTP(smtp_host)
    smtp.login(account,password)
    smtp.sendmail(account,u_list,msg.as_string())
    smtp.quit()
    print "发送成功".decode('utf-8').encode('mbcs')




u_list=[]    
#读取mongo数据库
client=MongoClient('localhost',27017)
mydb=client.smtp
people=mydb.people
email=mydb.email
for elist in email.find():
   for plist in people.find():
     # print plist['address']
     # u_list.append(plist['address'])
 #  sendEmail(u_list,elist['sub'],elist['html'])  
      sendEmail(plist['address'],elist['sub'],elist['html'])
      












 mongodb里面的数据库名为smtp,结构为两个collection

分别为

people
[{"id":"贝贝","address":"lghher1991@163.com"},{"id":"航航","address":"lghher1990@163.com"}] 

email
{"sub":"标题","html":"<html><h1>贝贝</h1></html>"}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值