Answers.Body: <p>You need to explicitly allow allow anonymous access to your controller's action.</p>
  
  <p>More info on how to do that here: <a href="http://buildwithcraft.com/docs/plugins/controllers#allowing-anonymous-access-to-actions">http://buildwithcraft.com/docs/plugins/controllers#allowing-anonymous-access-to-actions</a></p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-10-07T00:07:51.810Z
Answers.Text: Perfect! Exactly what I needed, it's working now. Thank you!
Answers.UserDisplayName: Luke Peters
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-10-06T23:36:39.233Z
Answers.LastActivityDate: 2014-10-06T23:36:39.233Z
Answers.UserDisplayName: Brad Bell
Body: <p>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.</p>
  
  <p>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.</p>
  
  <p>Thanks in advance for any help!</p>
  
  <p>Here's my plugin controller class file:</p>
  
  <pre><code>&lt;?php
  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-&gt;toEmail = 'luke@graftonstudio.com';
      $email-&gt;subject = 'Contact Form Submission';
      $email-&gt;htmlBody = '&lt;style&gt;div, p, h2 { font-family: "Helvetica Neue", "Helvetica", sans-serif; font-size: 14px; line-height: 22px; color: #4b4b4b; font-weight: 400; }&lt;/style&gt;
                                              &lt;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;"&gt;
                                                  &lt;h2 style="margin: 0 0 20px 0; font-size: 20px;"&gt;Contact Form Submission&lt;/h2&gt;
                                                  &lt;strong&gt;Name:&lt;/strong&gt;&lt;br&gt;' . $name . '&lt;br&gt;&lt;br&gt;
                                                  &lt;strong&gt;Email:&lt;/strong&gt;&lt;br&gt;' . $email_address . '&lt;br&gt;&lt;br&gt;
                                                  &lt;strong&gt;Phone Number:&lt;/strong&gt;&lt;br&gt;' . $phone . '&lt;br&gt;&lt;br&gt;
                                                  &lt;strong&gt;Company:&lt;/strong&gt;&lt;br&gt;' . $company . '&lt;br&gt;&lt;br&gt;
                                                  &lt;strong&gt;Job Title:&lt;/strong&gt;&lt;br&gt;' . $title . '&lt;br&gt;&lt;br&gt;
                                              &lt;/div&gt;';
  
      craft()-&gt;email-&gt;sendEmail($email);
  
      // not returning anything at the moment
      echo json_encode(array());
    }
  
  }
  </code></pre>
  
Tags: <plugin-development><routes>
Title: Craft CMS plugin route redirects to login when not logged into Craft
ThreadText: # Craft CMS plugin route redirects to login when not logged into Craft
  
  (Asked by: username_0 on 2014-10-06)
  
  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:
  
  ```
  <?php
  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 = 'username_0@graftonstudio.com';
      $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>
                                              </div>';
  
      craft()->email->sendEmail($email);
  
      // not returning anything at the moment
      echo json_encode(array());
    }
  
  }
  
  ```
  
  **Tags:** plugin-development, routes
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2014-10-06. Score: 8
  
  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>
  
  > **Comments:**
  > **username_0**: Perfect! Exactly what I needed, it's working now. Thank you!  
  
  ---
  
  