# Cakephp 4 Mailer

**URL:** <https://discourse.cakephp.org/t/cakephp-4-mailer/8539>\
**Category:** Need Help\
**Tags:** help\
**Created:** [October 22, 2020, 6:16pm UTC](https://discourse.cakephp.org/t/cakephp-4-mailer/8539 "2020-10-22T18:16:06Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![die7fox](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/die7fox/32/1970_2.png) [@die7fox](https://discourse.cakephp.org/u/die7fox)\
**Post date:** [October 22, 2020, 6:16pm UTC](https://discourse.cakephp.org/t/cakephp-4-mailer/8539/1 "2020-10-22T18:16:06Z")

</div>

Hello, everyone. I have a question about using the Cake Mailer to send to multiple recipients. Ideally, I would like each recipient to receive their own, individually-addressed email. In Cake 2, this was just a matter of looping through the to() and send() methods. The only way I’ve found to do it in Cake 4 is through the controller:

```
foreach($toArray as $t) {
    $this->getMailer('Mailer')->send('newMail', [$fulltitle, $t->email, $t->first_name]);
}

```

When I attempt the loop within the mailer itself, only the final recipient gets the message:

```
// Call Mailer in controller. Like above, but without the loop and passing the entire $toArray
$this->getMailer('Mailer')->send('newMail', [$fulltitle, $toArray]);

// newMail method in Mailer
public function newMail($fulltitle, $toArray)
{ 
    foreach ($toArray as $t) { 
        $this
        ->setEmailFormat('html')
        ->setTo($t->email)
        ->setFrom(['dba@whereiwork.org' => 'DBA'])
        ->setSubject(sprintf($fulltitle . " is ready for review"))
        ->setViewVars([
            'first_name' => $t->first_name,
            'fulltitle' => $fulltitle,
        ])
        ->viewBuilder()
            ->setTemplate('new_mail') 
            ->setLayout('default');
    }
}

```

So, if I have an array of, say, three email addresses, only the last one gets mailed. Is there a way to do this in the mailer itself, or am I stuck doing it in the controller (not my preference)?

---

<div class="post-metadata">

**Author:** ![dreamingmind](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/dreamingmind/32/1857_2.png) [@dreamingmind](https://discourse.cakephp.org/u/dreamingmind)\
**Post date:** [October 22, 2020, 6:54pm UTC](https://discourse.cakephp.org/t/cakephp-4-mailer/8539/2 "2020-10-22T18:54:47Z")

</div>

This is because your `newMail()` method is configuring the `Mail` object which is being returned to the `send()` method to be sent.

So, your `send` is waiting patiently while the object is configured in different ways, then it acts on the final configuration.

You could make a new class that has a method that accepts an array of recipients, then loops through then configuring and sending each email in turn; an extension of Mail perhaps.

I’m not sure if I explained that well…

Essentially,

`$this->getMailer('Mailer')->send('newMail', [$fulltitle, $toArray])` says pass the result of `newMail` to `send`.

---

<div class="post-metadata">

**Author:** ![die7fox](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/die7fox/32/1970_2.png) [@die7fox](https://discourse.cakephp.org/u/die7fox)\
**Post date:** [October 22, 2020, 7:09pm UTC](https://discourse.cakephp.org/t/cakephp-4-mailer/8539/3 "2020-10-22T19:09:28Z")

</div>

Ah, gotcha. Makes perfect sense after looking at the code again. 😃

For now, I think I’ll just drop the mailer in its own method in the same controller. It’s going to be used in several different parts of my app, and having a one-stop for it will be nice & tidy. Thanks!

---

<div class="post-metadata">

**Author:** ![FinlayDaG33k](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/finlaydag33k/32/1049_2.png) [@FinlayDaG33k](https://discourse.cakephp.org/u/FinlayDaG33k)\
**Post date:** [October 29, 2020, 9:39pm UTC](https://discourse.cakephp.org/t/cakephp-4-mailer/8539/4 "2020-10-29T21:39:43Z")

</div>

Instead of dropping it in it’s own controller, you could slap it into either a [component](https://book.cakephp.org/4/en/controllers/components.html#) or it’s own standalone class (a regular PHP class).

What you could also do is use the [email class](https://book.cakephp.org/4/en/core-libraries/email.html):

```php
// Set the recipients
$recipients = ['alice@example.com', 'bob@example.com', 'j.doe@example.com'];

// An array to hold our results
$results = [];

// Loop over all recipients and send em our spam... I mean mail.
foreach($recipients as $recipient):
  $mail = new \Cake\Mailer\Mailer();
  $mail->setTo($recipient);
  // ... Do more stuff you please here
  $results[$recipient] = $mail->deliver();
endforeach;

// ... Process the results here

```

Just going back to a more “basic” OOP 🙂
