Answers.Body: <p>So, this was being caused by selecting my action url from Chrome's url complete suggestions. Apparently, this submits the page twice. If the action url is typed in, pasted in or hit by my Chron job it works as expected. Firefox url suggest selection causes no problems.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-08-10T18:37:57.710Z
Answers.LastActivityDate: 2015-08-10T18:37:57.710Z
Answers.UserDisplayName: Herb Himes
Body: <p>For some reason that I cannot figure out, my plugin is sending 2 emails to each user instead on one. It sends the email on hit of an action url. Below is the function in my controller. Any help is greatly appreciated.</p>
  
  <pre><code>public function actionSendEmail()
  {
  
      $date = new DateTime();
      $date-&gt;modify('-7 days');
  
      // Format datetime for use in query
      $timezone = new \DateTimeZone(DateTime::UTC);
      $date = $date-&gt;format(DateTime::MYSQL_DATETIME, $timezone);
  
      $criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::Entry);
      $criteria-&gt;section = 'guide';
      $criteria-&gt;order = 'postDate desc';
      $criteria-&gt;dateUpdated = '&gt; '.$date;
  
      $entries = $criteria-&gt;find();
  
      if ($entries) {
  
          $user_criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::User);
          $user_criteria-&gt;groupId = '9';
          $users = $user_criteria-&gt;find();
  
          //Send to each User
          foreach ($users as $user) {
              //Send Mail
              $email = new EmailModel();
              $email-&gt;toEmail = $user-&gt;email;
              $email-&gt;subject = 'Updated in the Last 7 Days';
              $email-&gt;htmlBody    = 'Body Goes Here';
  
              craft()-&gt;email-&gt;sendEmail($email);
          }
  
      // Redirect after sending to each user in the loop to avoid 404
      $this-&gt;redirect('/');
  
      }
      else {
          // do nothing and redirect to avoid 404
         $this-&gt;redirect('/');
      }
  }
  </code></pre>
  
Tags: <plugin-development><emails>
Title: Help! My plugin is sending my email twice
ThreadText: # Help! My plugin is sending my email twice
  
  (Asked by: username_0 on 2015-08-10)
  
  For some reason that I cannot figure out, my plugin is sending 2 emails to each user instead on one. It sends the email on hit of an action url. Below is the function in my controller. Any help is greatly appreciated.
  
  ```
  public function actionSendEmail()
  {
  
      $date = new DateTime();
      $date->modify('-7 days');
  
      // Format datetime for use in query
      $timezone = new \DateTimeZone(DateTime::UTC);
      $date = $date->format(DateTime::MYSQL_DATETIME, $timezone);
  
      $criteria = craft()->elements->getCriteria(ElementType::Entry);
      $criteria->section = 'guide';
      $criteria->order = 'postDate desc';
      $criteria->dateUpdated = '> '.$date;
  
      $entries = $criteria->find();
  
      if ($entries) {
  
          $user_criteria = craft()->elements->getCriteria(ElementType::User);
          $user_criteria->groupId = '9';
          $users = $user_criteria->find();
  
          //Send to each User
          foreach ($users as $user) {
              //Send Mail
              $email = new EmailModel();
              $email->toEmail = $user->email;
              $email->subject = 'Updated in the Last 7 Days';
              $email->htmlBody    = 'Body Goes Here';
  
              craft()->email->sendEmail($email);
          }
  
      // Redirect after sending to each user in the loop to avoid 404
      $this->redirect('/');
  
      }
      else {
          // do nothing and redirect to avoid 404
         $this->redirect('/');
      }
  }
  
  ```
  
  **Tags:** plugin-development, emails
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2015-08-10. Score: 2
  
  So, this was being caused by selecting my action url from Chrome's url complete suggestions. Apparently, this submits the page twice. If the action url is typed in, pasted in or hit by my Chron job it works as expected. Firefox url suggest selection causes no problems.
  
  
  ---
  
  