Send bulk emails using sendgrid api in php
This is an example of how to send bulk emails to the users in chunks.
$users = DB::get("SELECT * FROM user;");
$chunks = array_chunk($users, 1000);
foreach ($chunks as $chunk) {
$personalizations = [];
foreach ($chunk as $user) {
$personalizations[] = [
"to"=> ["email"=>$user['email']],
"substitutions"=>[
"{name}" =>$user['name'],
]
];
}
$args = [
"personalizations"=>$personalizations,
"from"=>[
"name"=>"Sender Name",
"email"=>"sender@example.com"
],
"subject": "Example",
"content": [
{
"type": "text/plain",
"value": "Hello {name},"
},
{
"type": "text/html",
"value": "Hello {name}"
}
]
];
HttpPost(
'https://api.sendgrid.com/v3/mail/send',
$args,
[
"header"=> [
"Authorization"=> "Bearer $api_code"
]
]
);
}
More:
https://docs.sendgrid.com/ui/sending-email/substitution-and-section-tags#simple-name-substitution