首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在Codeigniter 3中通过Office365帐户发送电子邮件-连接超时

在Codeigniter 3中通过Office365帐户发送电子邮件-连接超时
EN

Stack Overflow用户
提问于 2015-06-29 20:47:21
回答 3查看 11.4K关注 0票数 2

我正在尝试连接到我的Office365帐户,并在Codeigniter3中发送一封电子邮件:

代码语言:javascript
复制
$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)上就不行了,这就是为什么我怀疑服务器上的一些设置需要修改的原因。我得到的只是这个:

代码语言:javascript
复制
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连接到邮件服务器,但我找不到解决方案,因为我对服务器配置了解不多。

任何建议都将不胜感激!

EN

回答 3

Stack Overflow用户

发布于 2015-10-07 06:44:31

以下是过去对我有效的方法:

代码语言:javascript
复制
<?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_user相同

票数 7
EN

Stack Overflow用户

发布于 2018-06-08 04:29:07

代码语言:javascript
复制
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";
}
?>
票数 0
EN

Stack Overflow用户

发布于 2019-05-13 03:57:06

对于那些面临同样问题的人,在2019年及之后连接Office365 smtp :我只是使用PHPMailer类解决了我的问题。我在这里和另一个问题中测试了所有东西,但什么都没有。上传PHPMailer文件夹,设置配置文件和boom。电子邮件起作用了。使用codeigniter 3.10和问题似乎仍然存在,因为这是一个老问题。在他们的电子邮件类(codeigniter)中有一个评论,似乎smtp连接不是很好……

代码语言:javascript
复制
/**
* 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/,并在那里创建一个文件:

代码语言:javascript
复制
MY_phpmailer.php

MY_phpmailer.php文件中:

代码语言:javascript
复制
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MY_PHPMailer {
        public function My_PHPMailer() {
        require_once('Phpmailer/class.phpmailer.php');
    }
}
?>

PHPMailer发送函数:

代码语言:javascript
复制
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');
    }

}

好了,最后,在你想要使用这个send函数的控制器上:

代码语言:javascript
复制
$this->load->library("MY_phpmailer");

在您看来,当您想要发送电子邮件时:

代码语言:javascript
复制
$this->system->sendmail2($to, $subject, $message); //$to, $subject and $message are vars you sent from your backend - prefer HTML to $message
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31116129

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档