5

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']    = 'MyEmail@btconnect.com'; 
    $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.

jonboy
  • 2,729
  • 6
  • 37
  • 77

1 Answers1

12

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';
winnie damayo
  • 426
  • 9
  • 17
  • 1
    Amazing @winnie damayo - that worked! Thank you very much. Why does that work and my original method didn't? According to the Codeigniter docs it says to use `$config['protocol'] = 'smtp'` I've been trying to figure this out for days, thanks again :) If you could elaborate on the answer that would be great! – jonboy Jun 19 '17 at 19:50
  • @winnie damayo - That worked! Thanks! What is the reason for not using SMTP as the protocol though using the same actually? – heyanshukla May 21 '19 at 15:51
  • 1
    by writing mail instead of smtp the codeigniter will skip smtp setting and it'll use default email method to send email. – Tashen Jazbi Jul 30 '19 at 10:54