Sending email over Office365 account in Codeigniter 3 - connection timeout

11,879

Solution 1

Here is what has worked for me in the past:

<?php 
$config['smtp_crypto'] = 'tls'; 
$config['protocol'] = 'smtp'; 
$config['smtp_host'] = 'smtp.office365.com';
$config['smtp_user'] = '[email protected]'; 
$config['smtp_pass'] = 'password'; 
$config['smtp_port'] = '587'; 
$config['charset']='utf-8'; // Default should be utf-8 (this should be a text field) 
$config['newline']="\r\n"; //"\r\n" or "\n" or "\r". DEFAULT should be "\r\n" 
$config['crlf'] = "\r\n"; //"\r\n" or "\n" or "\r" DEFAULT should be "\r\n" 
?>

Also to note that the FROM address must be the same as smtp_user

Solution 2

You probably ran php on a Linux machine, if this is the case, you'd need to use following line to override CodeIgniter's configuration parameter

'newline' => "\r\n", //must have for office365!

I published an article here:

http://aus800.com.au/sending-out-emails-using-codeigniter-from-office365-account/

Share:
11,879
rannt
Author by

rannt

Updated on June 05, 2022

Comments

  • rannt
    rannt almost 2 years

    I'm trying to connect to my Office365 account and send out an email in Codeigniter 3:

    $config['protocol'] = 'smtp';
    $config['smtp_host'] = 'smtp.office365.com';
    $config['smtp_user'] = '****';
    $config['smtp_pass'] = '****';
    $config['smtp_port'] = '587';
    $config['smtp_timeout'] = '60';
    $config['smtp_crypto'] = 'tls';
    $config['mailtype'] = 'html';
    $this->email->initialize($config);
    
    $this->email->from('****', '****');
    $this->email->to('****');
    
    $this->email->subject('Email Test');
    $this->email->message('Testing the email class.');
    
    $this->email->send(FALSE);
    
    $this->email->print_debugger();
    

    This code works fine on my local server (wamp), but not on my production server (Debian), that's why I suspect some settings on the server need to be altered. All I get is this:

    A PHP Error was encountered
    
    Severity: Warning
    
    Message: fsockopen(): unable to connect to smtp.office365.com:587 (Connection timed out)
    
    Filename: libraries/Email.php
    
    Line Number: 1949
    

    I also tried sending mail with Phpmailer class and I got the same result; works on my local machine, not on production server.

    Codeigniter class uses function fsockopen to connect to the mail server, but I can't find out the solution, since I don't know much about server configuration.

    Any suggestion would be appreciated!