【问题标题】:Send mail to multiple recipients using sendgrid使用 sendgrid 向多个收件人发送邮件
【发布时间】:2012-01-12 03:22:10
【问题描述】:

我想在一次通话中使用 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-&gt;Email-&gt;to

感谢您的帮助。

【问题讨论】:

  • 为什么不看看 CakePHP 的手册呢?
  • 您是否尝试过在to 字段中使用逗号分隔的电子邮件?

标签: php cakephp cakephp-1.3 sendgrid


【解决方案1】:

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

CakePHP 1.3 cookbook: Email

它应该可以满足您的需求:例如,bcc 字段允许您将邮件发送给多个收件人。

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        【解决方案4】:
        foreach($recipients as $recipient) {
            $this->Email->to .= $recipient['email'] . ",";
        }
        

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

        $this->Email->bcc
        

        而不是

        $this->Email->to
        

        【讨论】:

          【解决方案5】:

          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-23
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2015-12-18
            • 1970-01-01
            • 2020-09-15
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多