def send_emails(): """ Send queued emails """ count = 0 with mail.connect() as conn: for rec in EmailJobRecipient.query.filter( EmailJobRecipient.sent == False): # noqa: E712 count += 1 send_email(conn, rec) return count
def test_mail(): """ Allows testing mail sending to self """ from main import mail with mail.connect() as conn: logger.info("Mail verify: {}".format(conn.configure_host().vrfy)) msg = Message(recipients=["root@localhost"], body="test", subject="test2") conn.send(msg) return render_template("utility/success.html", action=_("Sent"), link="./testmail", title=_("Sent"))
def email(): form = EmailComposeForm() if form.validate_on_submit(): users = get_query(form.destination.data) if form.preview.data is True: return render_template( "admin/email.html", html=format_html_email(form.text.data, form.subject.data), form=form, count=users.count(), ) if form.send_preview.data is True: subject = "[PREVIEW] " + form.subject.data formatted_html = format_html_email(form.text.data, subject) preview_email = form.send_preview_address.data with mail.connect() as conn: msg = Message(subject, sender=app.config["CONTACT_EMAIL"]) msg.add_recipient(preview_email) msg.body = format_plaintext_email(form.text.data) msg.html = formatted_html conn.send(msg) flash("Email preview sent to %s" % preview_email) return render_template("admin/email.html", html=formatted_html, form=form, count=users.count()) if form.send.data is True: job = EmailJob( form.subject.data, format_plaintext_email(form.text.data), format_html_email(form.text.data, form.subject.data), ) db.session.add(job) for user in users: db.session.add(EmailJobRecipient(job, user)) db.session.commit() flash("Email queued for sending to %s users" % len(users.count())) return redirect(url_for(".email")) return render_template("admin/email.html", form=form)
def email(): form = EmailComposeForm() if form.validate_on_submit(): users = get_query(form.destination.data) if form.preview.data is True: return render_template( "admin/email.html", html=format_html_email(form.text.data, form.subject.data), form=form, count=users.count(), ) if form.send_preview.data is True: subject = "[PREVIEW] " + form.subject.data formatted_html = format_html_email(form.text.data, subject) preview_email = form.send_preview_address.data with mail.connect() as conn: msg = Message(subject, sender=app.config["CONTACT_EMAIL"]) msg.add_recipient(preview_email) msg.body = format_plaintext_email(form.text.data) msg.html = formatted_html conn.send(msg) flash("Email preview sent to %s" % preview_email) return render_template( "admin/email.html", html=formatted_html, form=form, count=users.count() ) if form.send.data is True: job = EmailJob( form.subject.data, format_plaintext_email(form.text.data), format_html_email(form.text.data, form.subject.data), ) db.session.add(job) for user in users: db.session.add(EmailJobRecipient(job, user)) db.session.commit() flash("Email queued for sending to %s users" % len(users.count())) return redirect(url_for(".email")) return render_template("admin/email.html", form=form)
def run(self): with mail.connect() as conn: for rec in EmailJobRecipient.query.filter(EmailJobRecipient.sent == False): # noqa: E712 self.send_email(conn, rec)
def run(self): with mail.connect() as conn: for rec in EmailJobRecipient.query.filter( EmailJobRecipient.sent == False): # noqa: E712 self.send_email(conn, rec)