Question

I am receiving a the following error:

Cannot send message without a recipient

This is while trying to send a email using swiftmailer. My code works in localhost and has all the parameters needed from sender to receiver but it throws an error saying it cannot send without recipient.

Here is my code:

public function email()
{


    Mail::send('emails.auth.mail', array('token'=>'SAMPLE'), function($message){
        $message = Swift_Message::newInstance();
        $email = $_POST['email']; $name = $_POST['name']; $subject = $_POST['subject']; $msg = $_POST['msg']; 

        $message = Swift_Message::newInstance()
            ->setFrom(array($email => $name))
            ->setTo(array('name@gmail.com' => 'Name'))           
            ->setSubject($subject)
            ->setBody($msg);




        $transport = Swift_MailTransport::newInstance('smtp.gmail.com', 465, 'ssl');
        //$transport->setLocalDomain('[127.0.0.1]');

        $mailer = Swift_Mailer::newInstance($transport);
        //Send the message
        $result = $mailer->send($message);
        if($result){

             var_dump('worked');
        }else{
            var_dump('Did not send mail');
        }
  }

}

Was it helpful?

Solution

You can do this without adding your the SMTP information in your Mail::send() implementation.

Assuming you have not already, head over to app/config/mail.php and edit the following to suit your needs:

'host' => 'smtp.gmail.com',
'port' => 465,
'encryption' => 'ssl',
'username' => 'your_username',
'password' => 'your_password',

Then your code should be as simple as:

public function email()
{
    Mail::send('emails.auth.mail', array('token'=>'SAMPLE'), function($message)
    {
        $message->from( Input::get('email'), Input::get('name') );

        $message->to('name@gmail.com', 'Name')->subject( Input::get('subject') );
    });
}

So, hopefully the issue is one of configuration. Do you have other environments setup that might be over-riding the app/config/mail.php configuration settings in your server where it's not working?

OTHER TIPS

Please make sure you have configured all the necessary configuration correctly on /app/config/mail.php. Ensure all the configuration is correct for the environment where the email is not working correctly.

If you want to use your Gmail account as an SMTP server, set the following in app/config/mail.php:

'driver' => 'smtp',
'host' => 'smtp.gmail.com',
'port' => 465,
'encryption' => 'ssl',
'username' => 'your-email@gmail.com',
'password' => 'your-password',

When switching to online server, you want to protect this file or switch provider so as not to expose your gmail credentials. Port 587 is for mailgun not gmail.

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