Sendgrid PHP 发送邮件给多个收件人

4

我有一个简单的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();
}

1
为每个电子邮件地址调用该函数。 - muttonUp
请提供代码示例? - Sahbaz
1
真的吗?循环遍历您的地址并调用函数。 - muttonUp
2
你最终会得到100个请求,而不是一个。非常聪明。 - Mike Doe
5个回答

6
SendGrid\Mail类支持通过SendGrid\Personalization类添加多个to地址。
您可以在此处查看示例:https://github.com/sendgrid/sendgrid-php/blob/master/examples/helpers/mail/example.php#L31-L35Personalization视为电子邮件的信封。它保存收件人的地址和其他类似数据。每个Sendgrid\Mail对象必须至少有一个Personalization
通过您正在使用的构造函数,已经为您创建了一个Personalization对象,请参见此处:https://github.com/sendgrid/sendgrid-php/blob/master/lib/helpers/mail/Mail.php#L951-L958 您可以不使用此方法创建Mail对象,然后添加自己的Personalization

3
最终,这就是我设法做到的方法,并且它运行良好。
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
如果有人仍在寻找如何在To、Cc和Bcc中添加多个电子邮件的答案,并使用SendGrid,这里是帮助我的方法。
首先,您需要添加一个类似于以下的关联数组: 对于To(收件人)中的多个电子邮件:
$tos = [
"example1@example.com" => "User 1", 
"example1@example.com" => "User 2"
];

在你的SendMail类中,使用$email->addTos($tos);代替$email->addTo;。同样地,对于多个Cc,使用...
$email->addCcs($cc);

以及针对Bcc

$email->addBccs($bcc);

这是查看更多细节的链接sendgrid php

,与发送电子邮件相关的IT技术。


1
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);

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";
}

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接