symfony 2 swiftmailer无法发送没有发件人地址的邮件

时间:2013-05-25 17:32:13

标签: symfony swiftmailer

尝试配置SwiftMailer以使用gmail发送邮件。

这是我的配置:

# Swiftmailer Configuration
swiftmailer:
    transport: gmail
    host:      ~
    username:  username@gmail.com
    password:  password

我的控制器动作:

     public function registerAction(Request $request) {
                    $message = new \Swift_Message();                    
                            $message::newInstance()
                              ->setFrom('username@gmail.com')
                            ->setSubject('ssssssss')                            
                            ->setTo(array('username@gmail.com'))
                            ->setBody(
                                    'text is going here');       
                            $res = $this->get('mailer')->send($message);
}

当我执行它时:

无法发送没有发件人地址的邮件 500内部服务器错误 - Swift_TransportException

Stack Trace
in /var/www/local/symfony/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Transport/AbstractSmtpTransport.php at line 164   
        }
        if (!$reversePath = $this->_getReversePath($message)) {
            throw new Swift_TransportException(
                'Cannot send message without a sender address'
                );
        }

1 个答案:

答案 0 :(得分:3)

在您的情况下,您应该将参数直接传递给Message构造函数。

<强>夫特/消息

public function __construct($subject = null, $body = null, $contentType = null, $charset = null) {
   // ...
}

public static function newInstance($subject = null, $body = null, $contentType = null, $charset = null)
{
    return new self($subject, $body, $contentType, $charset);
}

解决方案1 ​​

$message = new Message();    
$message            
    ->setFrom('username@gmail.com')  // no static $message::newInstance() call here
    ->setSubject('ssssssss')                            
    ->setTo(array('username@gmail.com'))
    ->setBody('text is going here');       

<强>溶液2

$message = new Message('ssssss','text is going here');
$message        
    ->setTo(array('username@gmail.com'))
    ->setFrom('username@gmail.com');

希望这有帮助。