Search code examples
smtpcpanelslimswiftmailer

How to I enable establish SMTP connection on Cpanel?


I want to be able o send an email from my application through SMTP and I don't want to use sendmail as a transport.

$transport = (new \Swift_SmtpTransport('smtp.gmail.com', 465, 'ssl'))
            ->setUsername('afashaisakiye@gmail.com')
            ->setSourceIp('0.0.0.0')
            ->setPassword('**********');
        //$transport = (new \Swift_SendmailTransport('/usr/sbin/sendmail -bs'));




        // Create the Mailer using your created Transport
        $mailer = new \Swift_Mailer($transport);
        // Create a message
        $message = (new \Swift_Message('This is the message subject'))
            ->setFrom(['afashaisakiye@gmail.com' => 'Smartup Digital team'])
            ->setTo([afasha@linearc.top => 'Isakiye Afasha'])
            ->setBody("thisis an html body", 'text/html', 'iso-8859-2');
        // Send the message
        $result = $mailer->send($message);

This works on my computer but when I deploy on the Cpannel I get this error.

Type: Swift_TransportException
Message: Connection could not be established with host smtp.gmail.com [Connection refused #111]
File: /home/linearct/checkin/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Transport/StreamBuffer.php
Line: 269

Solution

  • Sometimes by default external SMTP access is disabled for your CPanel account, you have to contact your service providers to re-enable it for you.