0

尝试将 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'
                );
        }
4

1 回答 1

4

在您的情况下,您应该将参数直接传递给 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');

希望这可以帮助。

于 2013-05-25T17:47:42.533 回答