Beispiel #1
0
    def post(self):
        """Post."""
        data = request.json

        email = Email(data["smtp"])
        if data.get("template"):
            customer = customer_manager.get(document_id=data["customer_id"])
            target = {
                "email": data["email"],
                "first_name": data["first_name"],
                "last_name": data["last_name"],
                "position": data["position"],
            }
            template = data["template"]
            tracking_info = get_tracking_info(
                data["smtp"],
                "test",
                "test",
            )
            context = get_email_context(
                customer=customer,
                target=target,
                url=tracking_info["click"],
            )
            email_body = render_template_string(template["html"], **context)
            from_address = render_template_string(
                get_from_address(data["smtp"], template["from_address"]),
                **context)
            subject = render_template_string(template["subject"], **context)
            try:
                email.send(
                    to_recipients=[data["email"]],
                    from_email=from_address,
                    subject=subject,
                    body=email_body,
                )
            except Exception as e:
                return jsonify(str(e)), 400
        else:
            context = get_email_context()
            email_body = render_template("emails/test.html", **context)
            try:
                email.send(
                    to_recipients=[data["email"]],
                    from_email=data["smtp"]["from_address"],
                    subject="test",
                    body=email_body,
                )
            except Exception as e:
                return jsonify(str(e)), 400
        return jsonify({"success": True}), 200
def process_click_test(subscription_id, contact_id):
    """Process a click from the landing page."""
    subscription = subscription_manager.get(
        document_id=subscription_id,
        fields=["test_results", "customer_id", "landing_page_url"],
    )
    contact = next(
        filter(lambda x: x["test_uuid"] == contact_id, subscription["test_results"])
    )
    contact["timeline"] = contact.get("timeline", [])
    if len(contact["timeline"]) < 10:
        contact["timeline"].append(get_timeline_entry("clicked"))
    contact["clicked"] = True
    contact["opened"] = True
    subscription_manager.update_in_list(
        document_id=subscription_id,
        field="test_results.$",
        data=contact,
        params={"test_results.test_uuid": contact_id},
    )

    # If a landing page url exists for the subscription, redirect to it after click has been tracked
    if subscription.get("landing_page_url"):
        return redirect(subscription["landing_page_url"], 302)

    # Get landing page
    landing_page = get_landing_page(contact["template"]["_id"])

    # Get customer
    customer = customer_manager.get(document_id=subscription["customer_id"])

    # Get Jinja template context
    context = get_email_context(target=contact, customer=customer)

    return render_template_string(landing_page["html"], **context)
def process_contact(
    sending_profile, subscription, contact, customer, template, email: Email
):
    """Send test to contact."""
    # Get tracking info for opens/clicks
    tracking_info = get_tracking_info(
        sending_profile,
        f"test_{subscription['_id']}",
        contact["test_uuid"],
    )
    context = get_email_context(
        customer=customer,
        target=contact,
        url=tracking_info["click"],
    )

    html = template["html"] + tracking_info["open"]

    email_body = render_template_string(html, **context)
    from_address = render_template_string(
        get_from_address(sending_profile, template["from_address"]), **context
    )
    subject = render_template_string(template["subject"], **context)
    email.send(
        to_recipients=[contact["email"]],
        from_email=from_address,
        subject=subject,
        body=email_body,
    )
Beispiel #4
0
def process_target(sending_profile, target, customer, template, email: Email):
    """Send email to target."""
    tracking_info = get_tracking_info(
        sending_profile,
        target["cycle_id"],
        target["_id"],
    )
    context = get_email_context(
        customer=customer,
        target=target,
        url=tracking_info["click"],
    )

    html = template["html"] + tracking_info["open"]

    email_body = render_template_string(html, **context)
    from_address = render_template_string(
        get_from_address(sending_profile, template["from_address"]), **context
    )
    subject = render_template_string(template["subject"], **context)
    email.send(
        to_recipients=[target["email"]],
        from_email=from_address,
        subject=subject,
        body=email_body,
    )
Beispiel #5
0
def preview_template(data, customer):
    """Preview template subject, from_address and html for reports."""
    fake = Faker()
    target = {
        "email": fake.email(),
        "first_name": fake.first_name(),
        "last_name": fake.last_name(),
        "position": fake.job(),
    }
    context = get_email_context(customer=customer, target=target)
    return render_template_string(data, **context)
Beispiel #6
0
    def get(self, tracking_id):
        """Get."""
        decoded = decode_tracking_id(tracking_id)
        if len(decoded) > 2:
            if decoded[0] == "test":
                return process_click_test(decoded[1], decoded[2])
            else:
                return
        cycle_id, target_id = decoded
        cycle = cycle_manager.get(document_id=cycle_id,
                                  fields=["_id", "subscription_id"])
        target = target_manager.get(document_id=target_id)
        if not cycle or not target:
            return render_template_string("404 Not Found"), 404

        click_events = list(
            filter(lambda x: x["message"] == "clicked",
                   target.get("timeline", [])))
        if len(click_events) < 10:
            target_manager.add_to_list(
                document_id=target["_id"],
                field="timeline",
                data=get_timeline_entry("clicked"),
            )
            cycle_manager.update(document_id=cycle["_id"],
                                 data={"dirty_stats": True})

        # If a landing page url exists for the subscription, redirect to it after click has been tracked
        subscription = subscription_manager.get(
            document_id=cycle["subscription_id"],
            fields=["customer_id", "landing_page_url"],
        )

        if subscription.get("landing_page_url"):
            return redirect(subscription["landing_page_url"], 302)

        customer = customer_manager.get(
            document_id=subscription["customer_id"])
        landing_page = get_landing_page(target["template_id"])

        context = get_email_context(target=target, customer=customer)
        return render_template_string(landing_page["html"], **context)