私はCakePHPについてあまり知りませんが、Sendgridはphpで複数の受信者に単一のメールを送信する簡単な方法を提供し、このコードをCakephpに変換できます。
SendgridはMail::addTos
、メールを送信したい複数のメールを追加できる方法を提供します。
ユーザーの電子メールとユーザー名の連想配列をに渡す必要がありますaddTos
。
以下の例を参照してください。
$tos = [
//user emails => user names
"user1@example.com" => "Example User1",
"user2@example.com" => "Example User2",
"user3@example.com" => "Example User3"
];
$email->addTos($tos);
sendgrid-php githubライブラリで提供されている完全な例を見たい場合は、以下に含めたので、例全体を理解できます。
<?php
require 'vendor/autoload.php'; // If you're using Composer (recommended)
// Comment out the above line if not using Composer
// require("<PATH TO>/sendgrid-php.php");
// If not using Composer, uncomment the above line and
// download sendgrid-php.zip from the latest release here,
// replacing <PATH TO> with the path to the sendgrid-php.php file,
// which is included in the download:
// https://github.com/sendgrid/sendgrid-php/releases
$email = new \SendGrid\Mail\Mail();
$email->setFrom("test@example.com", "Example User");
$tos = [
"test+test1@example.com" => "Example User1",
"test+test2@example.com" => "Example User2",
"test+test3@example.com" => "Example User3"
];
$email->addTos($tos);
$email->setSubject("Sending with Twilio SendGrid is Fun");
$email->addContent("text/plain", "and easy to do anywhere, even with PHP");
$email->addContent(
"text/html", "<strong>and easy to do anywhere, even with PHP</strong>"
);
$sendgrid = new \SendGrid(getenv('SENDGRID_API_KEY'));
try {
$response = $sendgrid->send($email);
print $response->statusCode() . "\n";
print_r($response->headers());
print $response->body() . "\n";
} catch (Exception $e) {
echo 'Caught exception: '. $e->getMessage(). "\n";
}