<p>I've got an even notifier plugin I have been working on for a specific client project. Sending an email to a single address works fine, but when I loop through a table containing email addresses it is sending duplicate addresses.</p>

<p>I have a global table field (adminEmails) which has two columns, one for a name and the other for an email address.</p>

<pre><code>          $adminGlobal = craft()-&gt;globals-&gt;getSetByHandle('admin');
          foreach($adminGlobal-&gt;adminEmails as $key =&gt; $admin){
            $email-&gt;toEmail = $admin['emailAddress'];
            $body .= "Manager ".$manager-&gt;fullName." has submitted an event for approval.\n\n";
            $body .= "Start Date &amp; Time: ".$entry-&gt;eventStartDate-&gt;format('m/d/Y g:i A'). "\n\n";
            $body .= "House: ".$house-&gt;houseName."\n\n";
            $body .= "School: ".$college-&gt;collegeName."\n\n";
            $body .= $entry-&gt;url;
            $email-&gt;body = $body;
            if($email-&gt;toEmail != ""){
              craft()-&gt;email-&gt;sendEmail($email);
            }
          }
</code></pre>

<p>I have been testing with two email addresses and both addresses receive the same email twice. Not sure what could be causing this. I tried creating a new email model object inside each foreach loop but that didn't help either. Any ideas what's happening here?</p>

<p>EDIT:</p>

<p>If I dump $admin you see the following,</p>

<pre><code>array
(
'col1' =&gt; 'John Doe'
'col2' =&gt; 'some.email@gmail.com'
'adminName' =&gt; 'John Doe'
'emailAddress' =&gt; 'some.email@gmail.com'
) 0 array
(
'col1' =&gt; 'Andrew Clinton'
'col2' =&gt; 'some.email@anecka.com'
'adminName' =&gt; 'Andrew Clinton'
'emailAddress' =&gt; 'some.email@anecka.com'
) 1
</code></pre>

<p>This is what I was expecting..so why is it sending the same email twice to both addresses?</p>

CC BY-SA 3.0
2015-07-23T00:39:31.330Z
If you `Craft::dump($key)` and `Craft::dump($admin)`, what do they look like?
Brad Bell
CC BY-SA 3.0
2015-07-24T18:18:11.063Z
@BradBell I added the Craft::dump.
Panda4Man
CC BY-SA 3.0
2015-07-24T21:05:41.283Z
I'd guess that loop is being triggered twice then.  Maybe it's inside an event that's getting fired multiple times?  You can always `Craft::log()` to help verify.
Brad Bell
<plugin-development><emails>
Plugin Emails Being Sent Twice
# Plugin Emails Being Sent Twice

(Asked by: username_0 on 2015-07-22)

I've got an even notifier plugin I have been working on for a specific client project. Sending an email to a single address works fine, but when I loop through a table containing email addresses it is sending duplicate addresses.
I have a global table field (adminEmails) which has two columns, one for a name and the other for an email address.
          $adminGlobal = craft()->globals->getSetByHandle('admin');
          foreach($adminGlobal->adminEmails as $key => $admin){
            $email->toEmail = $admin['emailAddress'];
            $body .= "Manager ".$manager->fullName." has submitted an event for approval.\n\n";
            $body .= "Start Date & Time: ".$entry->eventStartDate->format('m/d/Y g:i A'). "\n\n";
            $body .= "House: ".$house->houseName."\n\n";
            $body .= "School: ".$college->collegeName."\n\n";
            $body .= $entry->url;
            $email->body = $body;
            if($email->toEmail != ""){
              craft()->email->sendEmail($email);
            }
          }

I have been testing with two email addresses and both addresses receive the same email twice. Not sure what could be causing this. I tried creating a new email model object inside each foreach loop but that didn't help either. Any ideas what's happening here?
EDIT:
If I dump $admin you see the following,
array
(
'col1' => 'John Doe'
'col2' => 'some.email@gmail.com'
'adminName' => 'John Doe'
'emailAddress' => 'some.email@gmail.com'
) 0 array
(
'col1' => 'Andrew Clinton'
'col2' => 'some.email@anecka.com'
'adminName' => 'Andrew Clinton'
'emailAddress' => 'some.email@anecka.com'
) 1

This is what I was expecting..so why is it sending the same email twice to both addresses?

**Tags:** plugin-development, emails

> **Comments:**
> **username_1**: If you `Craft::dump($key)` and `Craft::dump($admin)`, what do they look like?  
> **username_0**: @BradBell I added the Craft::dump.  
> **username_1**: I'd guess that loop is being triggered twice then.  Maybe it's inside an event that's getting fired multiple times?  You can always `Craft::log()` to help verify.  

---

## 0 answers

