对于那些在 2019 年及之后连接 Office365 smtp 遇到同样问题的人:
我只使用 PHPMailer 类解决了我的问题。
我在这里和其他问题中测试了所有内容,但什么也没有。
上传 PHPMailer 文件夹,设置配置文件并繁荣。电子邮件有效。
使用 codeigniter 3.10 并且问题似乎仍然存在,因为这是一个老问题。
在他们的电子邮件类(codeigniter)中有一条评论似乎 smtp connect 不是很好...
/**
* 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 ...
*/
我的英语说得不太好,但这似乎有点令人困惑。
好的,所以,如果您不想浪费更多时间,请上传 PHPMailer 文件夹,您可以在以下位置下载:
https://github.com/PHPMailer/PHPMailer
将您的 PHPMailer 文件夹上传到 application/libraries/ 并在那里创建一个文件:
MY_phpmailer.php
在MY_phpmailer.php 文件中:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_PHPMailer {
public function My_PHPMailer() {
require_once('Phpmailer/class.phpmailer.php');
}
}
?>
PHPMailer 发送函数:
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');
}
}
好的,结束,在您想要使用此发送功能的控制器上:
$this->load->library("MY_phpmailer");
在你看来,当你想发送电子邮件时:
$this->system->sendmail2($to, $subject, $message); //$to, $subject and $message are vars you sent from your backend - prefer HTML to $message