使用sendgrid将邮件发送给多个收件人

时间:2011-12-03 11:58:56

标签: php cakephp cakephp-1.3 sendgrid

我想在一次通话中使用sendmail向多个收件人发送邮件。我正在使用cakephp 1.3。向单个收件人发送邮件工作正常。我想知道我是否可以向多个收件人发送邮件(通常约有2-5个收件人)。

$this->Email->smtpOptions = array('port'=>'587', 'timeout'=>'30', 'host' => 'smtp.sendgrid.net', 'username'=>'****', 'password'=>'****', 'client' => '***');
$this->Email->delivery = 'smtp';
$this->Email->from = 'Swiso Support <support@swiso.net>';
$this->Email->to = $user['email'];
$this->Email->subject = $title;
$this->Email->template = 'email';
$this->Email->sendAs = 'both';
$this->Email->send();

我可以将收件人数组传递给$this->Email->to

我感谢任何帮助。

5 个答案:

答案 0 :(得分:2)

谷歌搜索“cakephp电子邮件”揭示了这一点:

CakePHP 1.3 cookbook: Email

它应该为您提供所需内容:例如,bcc字段允许您向多个收件人发送邮件。

这本书还有一章sending multiple messages in a loop.

答案 1 :(得分:0)

简答:不。 我一直在研究这个问题(使用Sendgrid),除非你想使用密件抄送字段,否则在同一个调用中没有办法做到这一点。 你不应该担心只是在循环中发送它们。

答案 2 :(得分:0)

foreach($recipients as $recipient) {
    $this->Email->to .= $recipient['email'] . ",";
}

如果您不希望收件人能够看到您将其发送给谁,则发送BCC是一种更好的方法。在那种情况下,它将是

$this->Email->bcc

而不是

$this->Email->to

答案 3 :(得分:0)

http://book.cakephp.org/1.3/en/view/1284/Class-Attributes-and-Variables

to :地址消息将转到(字符串)。如果要将电子邮件发送给多个收件人,请用逗号分隔地址。

cc :要将邮件发送到的地址数组。

bcc :地址数组以bcc(盲目抄送)消息为。

示例:

$recipients = array("email1@gmail.com", "email2@yahoo.com");
$this->Email->to = implode(",", $recipients);
$this->Email->cc = $recipient;
$this->Email->bcc = $recipient;

答案 4 :(得分:0)

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