Description
Symfony version(s) affected: 4.3.2
Description
symfony/sendgrid-mailer generate the next Exception when I add BCC field using API:
"Unable to send an email: Each email address in the personalization block should be unique between to, cc, and bcc. We found the first duplicate instance of [contacto@123.cl] in the personalizations.0.bcc field. (code 400)."
How to reproduce
.env
SENDGRID_KEY=Your KEY
MAILER_DSN=api://$SENDGRID_KEY@sendgrid
IndexController
/**
* @route("/notify", name="notify")
*/
public function sendEmailMessage(MailerInterface $mailer): void {
$email = new \Symfony\Bridge\Twig\Mime\TemplatedEmail();
$email->from('no-reply@asdf.cl')
->to('juanito@palotes.cl')
->bcc('contacto@123.cl')
->subject("Pedido 123")
->htmlTemplate('email/pedido.html.twig')
->context([
])
;
$mailer->send($email);
}
Possible Solution
#SendgridTransport.php
/* Before
$personalization = [
'to' => array_map($addressStringifier, $this->getRecipients($email, $envelope)),
'subject' => $email->getSubject(),
];
*/
// After
$personalization = [
'to' => array_map($addressStringifier, $email->getTo()),
'subject' => $email->getSubject(),
];
Additional context
Generated JSON
before
array:3 [▼
"personalizations" => array:1 [▼
0 => array:3 [▼
"to" => array:1 [▼
0 => array:1 [▼
"email" => "juanito@palotes.cl"
],
1 => array:1 [▼
"email" => "contacto@123.cl"
]
]
"subject" => "Pedido 123"
"bcc" => array:1 [▼
0 => array:1 [▼
"email" => "contacto@123.cl"
]
]
]
]
"from" => array:1 [▼
"email" => "no-reply@asdf.cl"
]
"content" => array:2 [▶]
]
after
array:3 [▼
"personalizations" => array:1 [▼
0 => array:3 [▼
"to" => array:1 [▼
0 => array:1 [▼
"email" => "juanito@palotes.cl"
]
]
"subject" => "Pedido 123"
"bcc" => array:1 [▼
0 => array:1 [▼
"email" => "contacto@123.cl"
]
]
]
]
"from" => array:1 [▼
"email" => "no-reply@asdf.cl"
]
"content" => array:2 [▶]
]