Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Codeigniter SMTP Unable to Connect

I'm using Codeigniter 3 and have a simple contact form on my website. This contact form works perfectly on my localhost XAMPP environment, but not on my shared web hosting (BT).

I can't fugire out what the issue is, I've been in contact with their support and apparently if the email account is able to send and receive emails via email clients (which it can) they don't offer any additional support :/

I am able to login to Office365 to send and receive emails using this account. The smpt settings in Office365 are;

Server name: smtp.office365.com
Port: 587
Encryption method: STARTTLS

My current code is as follows;

    $config['protocol']     = 'smtp';
    $config['smtp_host']    = 'smtp.office365.com'; // also tried tls://smtp.office365.com 
    $config['smtp_port']    = '587';
    $config['smtp_user']    = '[email protected]'; 
    $config['smtp_pass']    = 'MyPass'; 
    $config['smtp_crypto'] = 'tls';
    $config['mailtype']     = 'html';
    $config['wordwrap']     = TRUE;
    $config['charset']  = 'iso-8859-1';
    $config['newline']  = "\r\n"; 

In localhost the email sends and there are no issues. On the live web host I receive the following error;

Message: fsockopen(): unable to connect to smtp.office365.com:587 (Connection timed out)

I've read that the issue could be related to OpenSSL not being enabled, however the web host has confirmed it is enabled. I've also checked the loaded extensions using the following code;

    echo "<pre>";
    print_r(get_loaded_extensions());
    echo "</pre>";

This returns;

Array
(
    [0] => Core
    [1] => date
    [2] => ereg
    [3] => libxml
    [4] => openssl
    [5] => pcre
    etc
    etc
)

I've checked that I'm able to connect to the smtp server using the following code;

$fp = fsockopen('tcp://smtp.office365.com', 587, $errno, $errstr, 10);
echo fgets($fp, 128);
var_dump($fp, $errno, $errstr);
fclose($fp);

On my localhost I receive the following message;

220 VI1PR0602CA0001.outlook.office365.com Microsoft ESMTP MAIL Service ready at Mon, 19 Jun 2017 10:19:10 +0000 resource(55) of type (stream) int(0) string(0) ""

On the live server I receive this;

Message: fsockopen(): unable to connect to tcp://smtp.office365.com:587 (Connection timed out)

I have tried Gmail smtp, again this works locally but not on the remote web host.

Is there anything else I should try, or ask my web host to check? Currently I'm out of ideas.

Any help is appreciated.

like image 414
jonboy Avatar asked Jun 19 '17 10:06

jonboy


1 Answers

i dont know if this would work in your case, but i have the same issue. it work on my local development using homestead but in hostgator its error. what i did to fix it, is i change the value of protocol to mail.

try to change your code:

$config['protocol']     = 'smtp';

to this:

$config['protocol']     = 'mail';
like image 139
winnie damayo Avatar answered Oct 30 '22 14:10

winnie damayo