Exemplo n.º 1
0
def enqueue_trusted_emails(users, subject, body, **kwargs):
    """Queue an email for sending by the background email worker."""
    job = EmailJob(
        subject,
        format_trusted_plaintext_email(body, **kwargs),
        format_trusted_html_email(body, subject, **kwargs),
    )
    db.session.add(job)

    for user in users:
        db.session.add(EmailJobRecipient(job, user))

    db.session.commit()
Exemplo n.º 2
0
def email():
    form = EmailComposeForm()
    if form.validate_on_submit() and form.preview.data is True:
        return render_template('admin/email.html', html=format_html_email(form.text.data, form.subject.data),
                               form=form)
    elif form.validate_on_submit() and 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)
        users = User.query.join(Ticket).filter(Ticket.paid == True).group_by(User.id).all()  # noqa
        for user in users:
            db.session.add(EmailJobRecipient(job, user))
        db.session.commit()
        flash("Email queued for sending to %s users" % len(users))
        return redirect(url_for('.email'))
    return render_template('admin/email.html', form=form)
Exemplo n.º 3
0
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)