我正在尝试连接到我的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';
$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连接到邮件服务器,但我找不到解决方案,因为我对服务器配置知之甚少。
任何建议都将不胜感激!
答案 0 :(得分:3)
你可能在Linux机器上运行php,如果是这种情况,你需要使用以下行来覆盖CodeIgniter的配置参数
'newline'=> “\ r \ n”,//必须拥有office365!
我在这里发表了一篇文章:
http://aus800.com.au/sending-out-emails-using-codeigniter-from-office365-account/
答案 1 :(得分:2)
这是过去对我有用的东西:
<?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"
?>
另请注意,FROM地址必须与smtp_user
相同答案 2 :(得分:0)
我无法准确指出错误。所以我发布了3个方法。抓住想法或制定出哪种方法。
方法01
重要信息是:
然后
$config['smtp_host'] = '';//change this
$config['smtp_timeout'] = '5';//fist try with 5 if no response increase to 90 and check
方法02
$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';
SSL/TLS vs plaintext/STARTTLS port numbers
方法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
答案 3 :(得分:0)
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";
}
?>
答案 4 :(得分:0)
对于那些面临相同问题的人,请在2019年及之后连接Office365 smtp: 我仅使用PHPMailer类解决了我的问题。 我在这里测试了所有内容,并在另一个问题中进行了测试,什么也没有。 上传了PHPMailer文件夹,设置了配置文件并开始运行。电子邮件有效。 使用codeigniter 3.10和问题似乎仍然存在,因为这是一个老问题。 在他们的Email类(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