Laravel电子邮件,发送多个电子邮件

时间:2018-02-02 13:07:27

标签: php laravel email laravel-5

我试图向多个提供商发送邮件

$email['success']等于:

0 => {
  'name' => 'Quimer Comercial Ltda.',
  'email' => 'anapaula.vendas@quimer.com.br'
},
1 => {
  'name' => 'Doce Aroma Industria e Comercio - 47'
  'email' => 'docearoma@docearoma.com.br'
},
2 => {
  'name' => 'Purifarma Distr.quimica e Farmac. Ltda.'
  'email' => 'purifarma@purifarma.com.br'
}

那是我的代码:

$mail = new NewCotacao($codigoempresa, $codigocotacao);
foreach($emails['success'] as $email){
    Mail::to($email['email'])->send($mail);
}

但看看发生了什么

enter image description here

我希望每个人看起来像这样

To: anapaula.vendas@quimer.com.br
To: docearoma@docearoma.com.br
To: purifarma@purifarma.com.br

1 个答案:

答案 0 :(得分:1)

您应该为每个用户创建单独的Mailable

foreach($emails['success'] as $email){
    $mail = new NewCotacao($codigoempresa, $codigocotacao);
    Mail::to($email['email'])->send($mail);
}

如果您使用相同的Mailable实例,则会收到附加的收件人。

// Extracted from vendor source code
// File: illuminate/mail/Mailable.php

/**
 * Set the recipients of the message.
 *
 * @param  object|array|string  $address
 * @param  string|null  $name
 * @return $this
 */
public function to($address, $name = null)
{
    return $this->setAddress($address, $name, 'to');
}

/**
 * Set the recipients of the message.
 *
 * All recipients are stored internally as [['name' => ?, 'address' => ?]]
 *
 * @param  object|array|string  $address
 * @param  string|null  $name
 * @param  string  $property
 * @return $this
 */
protected function setAddress($address, $name = null, $property = 'to')
{
    foreach ($this->addressesToArray($address, $name) as $recipient) {
        $recipient = $this->normalizeRecipient($recipient);
        $this->{$property}[] = [
            'name' => $recipient->name ?? null,
            'address' => $recipient->email,
        ];
    }
    return $this;
}