Php 通过Codeigniter 3中的Office365帐户发送电子邮件-连接超时
我正在尝试连接到我的Office365帐户并在Codeigniter 3中发送电子邮件:Php 通过Codeigniter 3中的Office365帐户发送电子邮件-连接超时,php,codeigniter,email,debian,Php,Codeigniter,Email,Debian,我正在尝试连接到我的Office365帐户并在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'; $confi
$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();
这段代码在我的本地服务器(wamp)上运行良好,但在我的生产服务器(Debian)上运行不好,这就是为什么我怀疑服务器上的某些设置需要更改的原因。我得到的只是:
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
我还尝试用Phpmailer类发送邮件,得到了同样的结果;在本地计算机上工作,而不是在生产服务器上
Codeigniter类使用函数fsockopen连接邮件服务器,但我找不到解决方案,因为我对服务器配置不太了解
任何建议都将不胜感激 我不能准确指出错误。所以我发布了3种方法。抓住这个想法,或者找出哪种方法 方法01
- 服务器名称:pod51010.outlook.com(您的可能不同)
- 港口:587
- 加密方法:TLS
$config['protocol'] = 'smtp';
$config['smtp_host'] = 'smtp.office365.com';
$config['smtp_user'] = '****';
$config['smtp_pass'] = '****';
$config['smtp_port'] = '465';//change this
$config['smtp_timeout'] = '60';
$config['smtp_crypto'] = 'tls';
方法03
$config['protocol'] = 'smtp';
$config['smtp_host'] = 'tls://smtp.office365.com';//change this
$config['smtp_user'] = '****';
$config['smtp_pass'] = '****';
$config['smtp_port'] = '587';
$config['smtp_timeout'] = '60';
$config['smtp_crypto'] = 'tls';//not sure about this, if not work remove this and try once
您可能在Linux机器上运行php,如果是这种情况,则需要使用下面的行覆盖CodeIgniter的配置参数 'newline'=>“\r\n”,//必须为office365提供强> 我在这里发表了一篇文章:
以下是我过去的工作经历:
<?php
$config['smtp_crypto'] = 'tls';
$config['protocol'] = 'smtp';
$config['smtp_host'] = 'smtp.office365.com';
$config['smtp_user'] = 'USER@EMAIL.COM';
$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"
?>
还要注意的是,发件人地址必须与smtp_用户相同继续此测试代码
continue with this tested code
<?php
$mail= new phpmailer();
$mail->isSMTP();
$mail->Host = 'smtp.office365.com';
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->Username = 'alertas@xxx.com';
$mail->Password = 'Password';
$mail->SetFrom('alertas@xxx.com', 'FromEmail'`enter code here`);
$mail->addAddress('x@xxx.com', 'ToEmail');
$mail->IsHTML(true);
$mail->Subject = 'Correo desde PHP';
$mail->Body = 'Prueba de correo desde php</b>';
if(!$mail->send()) {
echo "Ok";
} else {
echo "Error";
}
?>
对于遇到相同问题的用户,请在2019年及其后连接Office365 smtp:
我只使用PHPMailer类解决了我的问题。
我在这里和其他问题中测试了所有内容,但什么都没有。
上传了PHPMailer文件夹,设置配置文件和boom。电子邮件有效。
使用codeigniter 3.10,问题似乎仍然存在,因为这是一个老问题。
在他们的电子邮件类(codeigniter)有一个评论,似乎smtp连接不是很好
/**
* 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 ...
*/
在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');
}
}
?>
好的,要完成,在您想要使用此发送功能的控制器上:
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
谢谢你的快速回复。我用在其中一个答案中找到的脚本测试了端口。我设法解除了服务器防火墙上587和465端口的阻塞。现在,如果我尝试使用Phpmailer库发送邮件,一切正常!然而,我仍然无法在Codeigniter内发送电子邮件。我现在收到这个错误:消息:stream\u socket\u enable\u crypto():SSL:crypto enabling timeout谢谢您的回复。这是我在在线服务门户中所能找到的全部内容:
POP和IMAP设置:SMTP设置服务器名称:SMTP.office365.com端口:587加密方法:TLS
我已尝试更改设置(主机、端口、加密)并仔细检查登录凭据,但问题仍然存在。无论我做什么,我总是得到相同的错误(在页面加载很长时间后):“stream\u socket\u enable\u crypto():SSL:crypto enabling timeout”记住,我可以使用Phpmailer脚本在同一服务器上以相同的设置轻松发送电子邮件!你可以用gmail而不是Outlooks不幸的是,我不能,我需要通过我们公司的邮件发送电子邮件,这是我最后的选择。如果Codeogniter中的库非常好的话,我看不出使用和外部库有什么意义。我知道这个问题在某种程度上与服务器设置有关,但我对服务器了解不多,也弄不清楚。
$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