We've retired the forum in favor of GitHub Discussions.

New conversations can be started on GitHub. Existing conversations will remain for a while longer.

Head over to GitHub →

Sending an email w/ API places template in AJAX response

Curtis Blackwell January 19, 2017 by Curtis Blackwell

My addon listens for the Form.submission.creating event. It looks up content with an ID from the submission (to get info like price) to create a Stripe charge.

Then it sends an email (with Statamic's API) if it was a specific product. Sending the email adds the email template to the AJAX response.data. This breaks the JSON syntax.

If I comment out the code that sends the email, everything works.

How can I send an email and ensure the response doesn't contain the email template?

Here's the relevant code:

$charge = Charge::create([...]);

// Send an email if this was a day pass.
if (strpos(array_get($charge, 'description'), 'Day Pass') > -1) {
    // Vars available in email templates.
    $data = [
        'stripe_object' => $charge->__toArray(true),
        'location'      => array_get($cdStripe, 'location'),
    ];

    // Create and send the email.
    (\Statamic\API\Email::create())
        ->subject('Thanks for buying a Day Pass!')
        ->from(env('APP_FORMSETS_EMAILS_DEFAULT_RECIPIENT'))
        ->to(array_get($cdStripe, 'email'))
        ->with($data)
        ->template('day-pass')
        ->send();
}

// Flash the Charge so it's available in templates.
$this->flash->put('charge', $charge->__toArray(true));

if (request()->ajax()) {
    echo json_encode($charge->__toArray(true));
    exit;
}
Answered by Curtis Blackwell!
>>>>>>> Answered <<<<<<<
1 Reply
1 Follower