2
votes

I run this script to send to each user an email. Mails are sent ok(one to each user), but in the email sent all the others adresses are shown in the headers.

$all_users = array(); // intialzie array

while ($usuario = mysqli_fetch_array($resultado, MYSQLI_ASSOC)) {

$usermail = $usuario['email']; 
$hash = $usuario['hash'];
mysqli_query($dbc, "UPDATE newsletter SET enviado = '1' WHERE email='$usermail' "); 

$all_users[] = $usermail; // push all emails first
}

// then send

try {
$email->setTos($all_users)
->setFrom("[email protected]")
->setFromName("test")
->setReplyTo("[email protected]")
->setSubject("Test")
->setHtml('test');

$result = $sendgrid->send($email);
echo "enviado";

} catch(\SendGrid\Exception $e) {
echo $e->getCode() . "\n";
foreach($e->getErrors() as $er) {
    echo $er;
}
}

?>

How can i hide other emails while sending them individually using sendgrid (bcc - blind carbon copy)?

1
Already tried, error 400 Missing destination emailaguisa
you still have to have at least ONE To:. send it to yourself, or a blackhole address.Marc B

1 Answers

1
votes

Have you updated your SendGrid PHP library? The newest version uses v3 of the WebAPI, which corrects the confusing To: array functionality.

The issue is that what you're doing right now is populating the native SMTP-level To, which is why you can see all the addresses. In the v2 API, you'd need to instead populate the x-smtpapi field's to array, to make sure SendGrid splits out all the addresses.

I'd strongly recommend updating your PHP Library & leveraging the v3 API, which removes this confusion, and lets you setup each recipient in a more logical manner.