yii邮件程序扩展:如何一次发送多个电子邮件地址

时间:2012-09-04 05:15:57

标签: php yii sendmail mailer yii-extensions

问:如何一次发送多个地址?

状态:我正在使用邮件程序扩展名。当我发送到单个地址时,它正在工作。但是当我发送到多个地址时。它不起作用。

这个正在运作。

$mailer->AddAddress("aa@gmail.com");

以下不起作用。

$mailer->AddAddress("aaa@gmail.com, bbbb@gmail.com");
$mailer->AddAddress("'aaa@gmail.com', 'bbbb@gmail.com'");
$mailer->AddAddress("\"aaa@gmail.com\", \"bbbb@gmail.com\"");

4 个答案:

答案 0 :(得分:4)

您只需多次调用“addAddress”函数:

$mailer->AddAddress("aaa@gmail.com");
$mailer->AddAddress("bbbb@gmail.com");

答案 1 :(得分:0)

修改Mailer类如下。 Visit this thread for more information

<?php

Yii::import('application.extensions.PHPMailer_v5.1.*');

class Mailer {

    private $mail;

    public function initialise() {
        try {
            require Yii::getPathOfAlias('application.extensions') . '/PHPMailer_v5.1/class.phpmailer.php';
            $this->mail = new PHPMailer(TRUE);
            $this->mail->IsSMTP();                           // tell the class to use SMTP
            $this->mail->SMTPDebug = 0;
            $this->mail->SMTPAuth = true;                  // enable SMTP authentication
            $this->mail->Port = 25;                    // set the SMTP server port
            $this->mail->Host = "smtp.test.net"; // SMTP server
            $this->mail->Username = "test.com";      // SMTP server username
            $this->mail->Password = "test";            // SMTP server password
            $this->mail->Mailer = "smtp";
            $this->mail->From = 'info@test.com';
            $this->mail->FromName = 'test@net.com';
        } catch (Exception $e) {
            echo $e->getTraceAsString();
        }
    }

    public function email($message, $sendTo, $subject) {
        try {
            $this->mail->AddAddress($sendTo);
            $this->mail->Subject = $subject;
            $body = $message;
            $this->mail->MsgHTML($body);
            $this->mail->IsHTML(true); // send as HTML
            $this->mail->Send();
            $this->mail->ClearAllRecipients();
        } catch (Exception $e) {
            echo $e->getTraceAsString();
        }
    }

}

?>

答案 2 :(得分:0)

理解单个电子邮件的简单方法......

$emailaddress="johndoe@domain.com"
$username="John Doe"

$mail->AddAddress($emailaddress,$username);

对于多封电子邮件......

$mail->AddAddress("johndoe@domain.com");
$mail->AddAddress("johnsmith@domain.com");

或者你需要在阵列中发送多封电子邮件......

foreach ($array as $value) {

$mail->AddAddress($array[$value]);

}

并且符合您要求的任何循环条件。

答案 3 :(得分:-1)

此外,您可以通过Yii :: app-&gt; mailer-&gt; newMessage创建消息。 这允许您设置电子邮件消息值。 例如:

$emailAddresses = array(
    'to'    => array('email@blah.com','email2@blah.com'),
    'bcc'   => array('multiple emails','separated','by','commas'),
    'reply' => $replyEmail,
);

// Generate the message with appropriate fields
$message = Yii::app->mailer->newMessage; //Swift_Message::newInstance()
$message->setSubject($subject);
$message->setFrom(array($emailAddress => 'administration'));
$message->setTo( $emailAddresses['to'] );
$message->setBcc( $emailAddresses['bcc'] );
$message->setReplyTo( $emailAddresses['reply'] );
$message->setBody('<h1>'.$header.'</h1><p>'.$bodyHtml,'text/html');

//Send message
$mailer = Yii::app()->mailer->getInstance($email);

$mailer->send($message,$failures);