For those facing same problem to connect Office365 smtp on 2019 and after:
I resolved my problem just using PHPMailer Class.
I tested everything here and in another questions and nothing.
Uploaded PHPMailer Folder, set up config files and boom. Emails works.
Using codeigniter 3.10 and problem seems to remain since this is a old question.
In their Email class (codeigniter) have a comment that seems smtp connect is not very good...
/**
* STREAM_CRYPTO_METHOD_TLS_CLIENT is quite the mess ...
*
* - On PHP <5.6 it doesn't even mean TLS, but SSL 2.0, and there's no option to use actual TLS
* - On PHP 5.6.0-5.6.6, >=7.2 it means negotiation with any of TLS 1.0, 1.1, 1.2
* - On PHP 5.6.7-7.1.* it means only TLS 1.0
*
* We want the negotiation, so we'll force it below ...
*/
I don't speak english very well but this seems little confusing.
Ok, so, if you don't want to lose more time, upload PHPMailer Folder you can download at:
https://github.com/PHPMailer/PHPMailer
Upload your PHPMailer folder to application/libraries/
and create a file there too:
MY_phpmailer.php
In MY_phpmailer.php
file:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_PHPMailer {
public function My_PHPMailer() {
require_once('Phpmailer/class.phpmailer.php');
}
}
?>
PHPMailer send function:
public function sendmail2($to,$subject,$message,$reply=null,$nameReply=null){
$this->CI->load->library('MY_phpmailer');
//$this->load->library('MY_phpmailer'); (If you use this function inside CI, use this instead above) I use CI->load above because my function is in a function_helper, not on controller
$mail = new PHPMailer();
$mail->IsSMTP(); //Definimos que usaremos o protocolo SMTP para envio.
$mail->SMTPDebug = 0;
$mail->CharSet = 'UTF-8';
$mail->SMTPAuth = true; //Habilitamos a autenticaçăo do SMTP. (true ou false)
$mail->SMTPSecure = "tls"; //Estabelecemos qual protocolo de segurança será usado.
$mail->Host = "smtp.office365.com"; //Podemos usar o servidor do gMail para enviar.
$mail->Port = 587; //Estabelecemos a porta utilizada pelo servidor do gMail.
$mail->Username = "your_email@yourdomain.com.br"; //Usuário do gMail
$mail->Password = "Strong_Password"; //Senha do gMail
if($reply != null and $nameReply != null){
//add replyto if you send those values, so you can set other reply address than senders address
$mail->AddReplyTo($reply, $nameReply);
}
$mail->SetFrom('your_email@yourdomain.com.br', 'Senders Name'); //Quem está enviando o e-mail.
$mail->Subject = $subject;
$mail->IsHTML(true);
$mail->Body = $message;
//$mail->AltBody = "Plain text.";
$mail->AddAddress($to);
/*If you want to put attachments that comes from a input FILE type name='file'.*/
if (isset($_FILES['file']) &&
$_FILES['file']['error'] == UPLOAD_ERR_OK) {
$mail->AddAttachment($_FILES['file']['tmp_name'],
$_FILES['file']['name']);
}
if(!$mail->Send()) {
// error occur - user your show method to show error
set_msg('msgerro', $mail->ErrorInfo, 'erro');
} else {
// success occur - user your show method to show success
set_msg('msgok', 'E-mail enviado.', 'sucesso');
}
}
Ok, to finish, on controller that you'd want to use this send function:
$this->load->library("MY_phpmailer");
And in your view, when you want to send e-mails:
$this->system->sendmail2($to, $subject, $message); //$to, $subject and $message are vars you sent from your backend - prefer HTML to $message