1

我想在一次通话中使用 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.

我很感激任何帮助。

4

5 回答 5

2

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

CakePHP 1.3 食谱:电子邮件

它应该给你你需要的东西:例如,有一个bcc字段允许你向多个收件人发送邮件。

这本书还有一章是关于在循环中发送多条消息的。

于 2011-12-03T12:02:19.743 回答
1

我对 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";
}
于 2019-06-19T06:42:38.317 回答
0

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

于 2011-12-03T13:17:43.037 回答
0
foreach($recipients as $recipient) {
    $this->Email->to .= $recipient['email'] . ",";
}

如果您不希望收件人看到您将密件抄送给谁,那么发送密件抄送是一种更好的方法。在这种情况下,它将是

$this->Email->bcc

代替

$this->Email->to
于 2011-12-03T13:29:45.953 回答
0

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

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

cc : 将消息抄送至的地址数组。

bcc:要密送(密件抄送)邮件的地址数组。

例子:

$recipients = array("email1@gmail.com", "email2@yahoo.com");
$this->Email->to = implode(",", $recipients);
$this->Email->cc = $recipient;
$this->Email->bcc = $recipient;
于 2012-10-02T13:37:43.597 回答