I created a simple Craft plugin to allow for custom action URLs. Specifically, I have a path "actions/api/sendEmail" that hits a method in my plugin's controller. It sends an email from a contact form via Ajax.

This works perfectly when I'm logged into Craft in the browser. But if I am not logged in, Ajax hits the "actions/api/sendEmail" URL and then gets redirected to "/login". Is my plugin not set to be a public-facing, front-end plugin? I'm pretty new to Craft. I've read through a lot of the docs about plugins and routes but can't seem to find the answer.

Thanks in advance for any help!

Here's my plugin controller class file:

namespace Craft;

class apiController extends BaseController {

  function actionSendEmail() {

    $name = $_POST['first_name'] . ' ' . $_POST['last_name'];
    $email_address = $_POST['email'];
    $phone = $_POST['phone'];
    $company = $_POST['company'];
    $title = $_POST['title'];

    $email = new EmailModel();

    $email->toEmail = '[email protected]';
    $email->subject = 'Contact Form Submission';
    $email->htmlBody = '<style>div, p, h2 { font-family: "Helvetica Neue", "Helvetica", sans-serif; font-size: 14px; line-height: 22px; color: #4b4b4b; font-weight: 400; }</style>
                                            <div style="margin: 20px; padding: 20px 20px 26px; border: 1px solid #e6e6e6; box-shadow: inset 0 1px 0 #ffffff, 0 4px 2px -3px rgba(0,0,0,0.06); background: #f9f9f9;">
                                                <h2 style="margin: 0 0 20px 0; font-size: 20px;">Contact Form Submission</h2>
                                                <strong>Name:</strong><br>' . $name . '<br><br>
                                                <strong>Email:</strong><br>' . $email_address . '<br><br>
                                                <strong>Phone Number:</strong><br>' . $phone . '<br><br>
                                                <strong>Company:</strong><br>' . $company . '<br><br>
                                                <strong>Job Title:</strong><br>' . $title . '<br><br>


    // not returning anything at the moment
    echo json_encode(array());


1 Answer 1


You need to explicitly allow allow anonymous access to your controller's action.

More info on how to do that here: http://buildwithcraft.com/docs/plugins/controllers#allowing-anonymous-access-to-actions

  • Perfect! Exactly what I needed, it's working now. Thank you! Commented Oct 7, 2014 at 0:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.