Reputation: 10121
I'm sending an email in order to notify my users about certain changes in the system.
In order to improve deliverability I would like to send each email individually rather then as one email with a long cc\bcc list.
All of the tutorials which I've encountered speak of plugins, and sophisticated ways of binding value arrays to a placeholder within a message.
I am seeking a rather simple way to do this, using a single call to the SendGrid server, preferably using XSMTP-API header, and without any unneeded complications.
I've been searching the web for a solution for a couple of hours now, and I can confidently say that there seems to be no suitable solution for me out there, so I'm forced to open a new thread, although I am well aware that this is a common topic, and I've read multiple threads on similar questions. Unfortunately non of them were helpful for me.
I am limited to the older versions of sendgrid, or the swiftMailer library, since my production server doers't support namespacing.
Thank in advance, O.
Pseudo code:
$conn = $PDOStaticClass::getInstance();
$dbHelper = new DbPdoHelper($conn);
$mailList = $dbHelper->getMailinglist();
//$mailList now contains an associative array with the email addresses with of my clients, what's next?
Upvotes: 1
Views: 1481
Reputation: 13188
This is super easy with the SendGrid SMTPAPI header. All you have to do is add an array of to
addresses in the X-SMTPAPI
header when you're sending a message and it will automatically generate a new email to each person in the list (not BCC or CC). For example:
"X-SMTPAPI": {
"to": [
"[email protected]",
"Joe Smith <[email protected]>"
]
}
Both Ben and Joe will receive separate emails with the same data. If you want to customize the email that each of them gets, you can use substitutions to accomplish that. See the documentation for more info: http://sendgrid.com/docs/API_Reference/SMTP_API/index.html.
You can also use the SendGrid PHP library to simplify this process:
$conn = $PDOStaticClass::getInstance();
$dbHelper = new DbPdoHelper($conn);
$sendgrid = new SendGrid('username', 'password');
$mail = new SendGrid\Mail();
$mail->setRecipientsInHeader(true);
$mailList = $dbHelper->getMailinglist();
foreach($mailList as $email) {
$mail->addTo($email);
}
// ...
$sendgrid->smtp->send($mail);
Upvotes: 2