【问题标题】:Zend Mailer error "From header set twice"Zend Mailer 错误“从头设置两次”
【发布时间】:2011-07-18 00:11:28
【问题描述】:

我的问题基于 Zend Mail

我有一个扩展 Zend Mail 的类。基本上它应该在注册时向用户发送两封不同的邮件。

它有两个函数 sendRegistrationMail 和 sendActivationMail 并且两个方法都使用在构造函数中初始化的相同传输。

调用这两个函数 sendRegistrationMail 有效,但第二个给出错误:**From Header Set Twice**

class Mailer_Register_SendMail extends Zend_Mail
{

    public $_config;
    public $_transport;
    public $_email;
    public $_fromEmail;
    public $_fromFullName;


    public function __construct($email)
    {
        parent::__construct();

        $this->_config=array('auth'=>'login','ssl'=>'tls','username'=>"$email",'password'=>'12345678','port'=>'25');
        $this->_transport=new Zend_Mail_Transport_Smtp('127.0.0.1',$this->_config);
        $this->_email=$email;
        $this->_fromEmail="administrator@rta.com";
        $this->_fromFullName="RTAsys.com";
    }



    public function sendRegistrationMail()

    {
        $emailmessage="<h3>Welcome to the Atanik Authorization</h3></br>".
                        "<p>You will soon receive your activation email as a separate message</p>";

                        $fromemail=$this->_fromEmail;
                        $fromfullname=$this->_fromFullName;
                        $to=$this->_email;
                        $subject="Welcome to RTA";

                        $this->setBodyHtml($emailmessage);
                        $this->setFrom($fromemail,$fromfullname);
                        $this->addTo($to);
                        $this->setSubject($subject);

                        try {
                        $this->send($this->_transport);

                        }
                        catch (Zend_Mail_Transport_Exception $ex)
                        {

                        }


    }

    public function sendActivationMail()
    {


                    $subjectActivation="Activate Your Angaza Account";

                    $emailActivationMessage="Thank you for taking time to join Atanik
                    Authorization Please click the link below to activate your account now
                    http://localhost/RTAsys/public/Account/activate?email=".$this->_email;


                    $fromActivationEmail=$this->_fromEmail;

                    $fromActivationFullName=$this->_fromFullName;

                    $to=$this->_email;


                    $this->setBodyText($emailActivationMessage);
                    $this->setFrom($fromActivationEmail,$fromActivationFullName);
                    $this->addTo($to);
                    $this->setSubject($subjectActivation);
                    try
                     {
                    $this->send($this->_transport);
                     }
                     catch (Zend_Mail_Transport_Exception $ex)
                     {

                     }          
    } 
}
?>

【问题讨论】:

    标签: email zend-framework zend-mail


    【解决方案1】:

    使用clearFrom() 在每次迭代期间清除您的标题,如果您确实必须为单次迭代使用相同的 Zend_Mail 对象:

    $mail = new Zend_Mail();
    foreach ($users as $key => $user) {
        $mail->clearFrom();
        $mail->setFrom('foo@bar');
        // do some more stuff
    }
    

    错误本身是不言自明的:您不止一次调用setFrom(),这不是必需的。这很可能是由于在迭代之外实例化了Zend_Mail 对象并在此迭代中调用了setFrom()

    【讨论】:

    • 显然我还不清楚你的答案,因为我还在学习,我的课程扩展了 Zend_Mail。第一个函数没问题,我做了 $this->setfrom(),$this->setBody..然后我 $this->send($transport) ..所以我在发送后清除所有这些。我的第二个功能几乎相似。我很感激
    • @jamon 你在问问题吗?
    • Rotteveel,我附上了我的简单代码,因为即使在 clearFrom 之后我也不断收到错误。你能帮忙吗
    • @jamon,我没有看到你在任何地方调用 clearFrom()。再次阅读我的答案:如果应用正确,它解决问题。
    • @jamon 只需添加 $this->clearFrom();在 sendRegistrationMail() 结束时
    猜你喜欢
    • 2016-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-27
    • 2011-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多