Flask массовая рассылка сообщений
from flask import Flask, render_template, make_response
from flask_sqlalchemy import SQLAlchemy
from flask_mail import Mail, Message
import os
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///subscribe.db'
app.config['MAIL_SERVER'] = 'mail.mail.com'
app.config['MAIL_PORT'] = 25
app.config['MAIL_USERNAME'] = 'name'
app.config['MAIL_PASSWORD'] = 'secret'
db = SQLAlchemy(app)
mail = Mail(app)
class Subscribe(db.Model):
id = db.Column('id', db.Integer, primary_key=True)
email = db.Column('email', db.String(50), unique=True, nullable=False)
company = db.Column(db.String(50), nullable=False)
def __repr__(self):
return f"Subscribe('{self.company}', '{self.email}')"
@app.route('/sendmail', methods=['GET', 'POST'])
def index():
theuser = Subscribe.query.filter(Subscribe.id).all()
msg = Message('Hello', sender='flask@mail.com', recipients=[theuser.email for theuser in Subscribe.query.all()])
with app.open_resource(r"C:\py\image.png") as fp:
msg.attach("image.png", "image/png", fp.read())
for theuser in Subscribe.query.all():
mail.send(msg)
return 'Message sent!'
if __name__ == '__main__':
app.run(debug=True)