0
votes

I am trying to send email in Laravel using Swift Mailer, i have been trying from last 3 days but No Luck.

I have Godaddy Shared hosting, i already discussed with Customer Support , they said there is no issue from there side

I am not able to find what is wrong in this code.

Already Referred this post but didn't get any solutions.

  1. Laravel - Connection could not be established with host smtp.gmail.com [ #0]
  2. Swift_TransportException Connection could not be established with host smtp.gmail.com
  3. Connection could not be established with host smtp.gmail.com [Connection timed out #110] in laravel 5.2
  4. How to resolve the error: Message: fsockopen(): unable to connect to ssl://smtp.gmail.com:465 (Connection refused)

Code

   $transport = \Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, "ssl")
      ->setUsername('[email protected]')
      ->setPassword('*****');

      $nam = 'abc';
      $from = '[email protected]';

      $message = \Swift_Message::newInstance($subject)

      ->setFrom(array($from => $name))
      ->setTo(array($to))
      ->setBody($body);
      $message->setContentType("text/html");
      $mailer = \Swift_Mailer::newInstance($transport);
      $result = $mailer->send($message);
        return $result;



Please ignore Variable declaration, like $subject and ect....

Port Tried.

1. 465
2. 587
3. 80
4. 25
5. 110 ( Gmail Commercial Email )

It's showing error

Message: fsockopen(): unable to connect to ssl://smtp.gmail.com:465 (Connection refused)

enter image description here

4
do you have insecure apps allowed on your gmail account? That's usually what blocks the connection.Florian Humblot
yes it's allowedSantosh Khuva
Godaddy Shared hosting there's your problemIan
@lan. does it mean it will not work in Godaddy shared hosting ?Santosh Khuva

4 Answers

2
votes

I have used an old server of godaddy and I can to send emails. I had to change sendmail's path. You can to see it on info.php ->

// Show all information, defaults to INFO_ALL
phpinfo();

And search: "sendmail_path"...

In my case sendmail_path was "/usr/sbin/sendmail -t -i"

You should put it on config/mail.php:

   /*
    |-----------------------------------------------------------------
    | Sendmail System Path
    |-----------------------------------------------------------------
    |
    | When using the "sendmail" driver to send e-mails, we will need to know
    | the path to where Sendmail lives on this server. A default path has
    | been provided here, which will work well on most of your systems.
    |
    */

    'sendmail' => '/usr/sbin/sendmail -t -i',
0
votes

check your smtp connection email, trying with google email for testing

smtp.gmail.com port : 587 secureconnection: true

make disable in google email secure connectino

0
votes

I've got a solution. Use this configuration:

        'mail' => [
        'class' => 'yii\swiftmailer\Mailer',
        'htmlLayout' => '@frontend/views/user/mail/layouts/html',
        'transport' => [
            'class' => 'Swift_SmtpTransport',
            'host' => 'smtp-relay.gmail.com',  // e.g. smtp.mandrillapp.com or smtp.gmail.com
            'username' => '',
            'password' => '',
            'port' => '465',
            'encryption' => 'ssl',
            'streamOptions' => [
                'ssl' => [
                    'allow_self_signed' => true,
                    'verify_peer' => false,
                    'verify_peer_name' => false,
                ],
            ]
        ],
    ],
0
votes

This configuration worked for me !

MAIL_DRIVER=sendmail
MAIL_HOST=smtp.gmail.com
MAIL_PORT=465
[email protected]
MAIL_PASSWORD=******
MAIL_ENCRYPTION=tls

Make sure u clear cache after you have changed configuration.

php artisan cache:clear