Question

Trying to configure SwiftMailer to send messages with gmail.

Here is my configuration:

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

And my controller action:

     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);
}

And when I execute it:

Cannot send message without a sender address 500 Internal Server Error - 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'
                );
        }
Was it helpful?

Solution

In your case you should pass the parameters directly to the Message constructor.

Swift/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);
}

Solution1

$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');       

Solution2

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

Hope this helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top