Sendgrid php发送给多个收件人

时间:2017-04-25 18:28:13

标签: php sendgrid

我有简单的sendgrid php脚本来发送电子邮件,这里唯一的问题是我需要添加更多的收件人,所以这段代码只适用于一个收件人,我正在查看官方文档,但无法找到任何有用的信息,是有谁知道如何以及我需要在这里更改以添加更多收件人/电子邮件。

function sendEmail($subject, $to, $message) {
    $from = new SendGrid\Email(null, "sample@email.com");
    $subject = $subject;

    $to = new SendGrid\Email(null, $to);
    $content = new SendGrid\Content("text/html", $message);
    $mail = new SendGrid\Mail($from, $subject, $to, $content);

    $apiKey = 'MY_KEY';
    $sg = new \SendGrid($apiKey);

    $response = $sg->client->mail()->send()->post($mail);
    echo $response->statusCode();
}

4 个答案:

答案 0 :(得分:4)

SendGrid\Mail类支持通过to类添加多个SendGrid\Personalization地址。

您可以在此处查看示例:https://github.com/sendgrid/sendgrid-php/blob/master/examples/helpers/mail/example.php#L31-L35

Personalization视为电子邮件的信封。它包含收件人的地址和其他类似数据。每个Sendgrid\Mail对象必须至少有一个Personalization

通过您正在使用的构造函数,已经为您创建了Personalization对象,请参见此处:https://github.com/sendgrid/sendgrid-php/blob/master/lib/helpers/mail/Mail.php#L951-L958

您可以创建Mail对象without this及更高版本add your own Personalization

答案 1 :(得分:2)

最后,这就是我设法做到这一点并且它运作良好的方式。

function sendEmail($subject, $to, $message, $cc) {
$from = new SendGrid\Email(null, "sample@email.com");
$subject = $subject;

$to = new SendGrid\Email(null, $to);
$content = new SendGrid\Content("text/html", $message);
$mail = new SendGrid\Mail($from, $subject, $to, $content);

foreach ($cc as $value) {
    $to = new SendGrid\Email(null, $value);
    $mail->personalization[0]->addCC($to);
}

$apiKey = 'MY_KEY';
$sg = new \SendGrid($apiKey);

$response = $sg->client->mail()->send()->post($mail);
echo $response->statusCode();

}

答案 2 :(得分:2)

function makeEmail($to_emails = array(),$from_email,$subject,$body) {
    $from = new SendGrid\Email(null, $from_email);

    $to = new SendGrid\Email(null, $to_emails[0]);
    $content = new SendGrid\Content("text/plain", $body);
    $mail = new SendGrid\Mail($from, $subject, $to, $content);
    $to = new SendGrid\Email(null, $to_emails[1]);
    $mail->personalization[0]->addTo($to);

    return $mail;
}

function sendMail($to = array(),$from,$subject,$body) {

    $apiKey = 'your api key';
    $sg = new \SendGrid($apiKey);
    $request_body = makeEmail($to ,$from,$subject,$body);
    $response = $sg->client->mail()->send()->post($request_body);
    echo $response->statusCode();
    echo $response->body();
    print_r($response->headers());
}

$to = array('test1@example.com','test2@example.com');
$from = 'from@example.com';
$subject = "Test Email Subject";
$body = "Send Multiple Person";

sendMail($to ,$from,$subject,$body);

答案 3 :(得分:0)

现在,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";
}

参考: https://github.com/sendgrid/sendgrid-php/blob/master/USE_CASES.md#send-an-email-to-multiple-recipients