有一个简单的数据库,其中包含需要发送消息的用户
class Subscribe(db.Model):
id = db.Column('id', db.Integer, primary_key=True)
company = db.Column('company', db.String(50))
email = db.Column('email', db.String(50))
有一个发送消息的脚本,在这种情况下发送给数据库的第一个用户
@app.route('/sendmail', methods=['POST'])
def index():
theuser = Subscribe.query.filter(Subscribe.id).first()
msg = Message('Hello', sender='flask@mail.com', recipients=[theuser.email])
with app.open_resource(r"C:\py\image.jpg") as fp:
msg.attach("image.png", "image/png", fp.read())
mail.send(msg)
return 'Message sent!'
告诉我如何向所有 db 用户发送消息
要从表中获取所有记录
Subscribe,您需要调用all,例如:要从 table 获取所有内容
email,然后:或在一行中作为列表: