【问题标题】:Sending email over Office365 account in Codeigniter 3 - connection timeout在 Codeigniter 3 中通过 Office365 帐户发送电子邮件 - 连接超时
【发布时间】:2015-09-15 22:37:46
【问题描述】:

我正在尝试连接到我的 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 连接邮件服务器,但由于我对服务器配置不太了解,所以我找不到解决方案。

任何建议将不胜感激!

【问题讨论】:

标签: php codeigniter email debian


【解决方案1】:

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

<?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】:

    你可能在 Linux 机器上运行了 php,如果是这种情况,你需要使用下面的代码来覆盖 CodeIgniter 的配置参数

    'newline' => "\r\n", //office365 必备!

    我在这里发表了一篇文章:

    http://aus800.com.au/sending-out-emails-using-codeigniter-from-office365-account/

    【讨论】:

      【解决方案3】:

      我无法准确指出错误。所以我发布了3种方法。抓住想法或制定出哪种方法。

      方法01

      1. 登录Microsoft Online Services Portal
      2. 点击 Outlook
      3. 点击选项(右上角)
      4. 点击关于
      5. 会有一个标题为外部 SMTP 设置的部分,如下所示:

      重要信息是:

      • 服务器名称:pod51010.outlook.com(您的可能不同)
      • 端口:587
      • 加密方式:TLS

      然后

      $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
      

      【讨论】:

      • 感谢您的回复。这就是我在在线服务门户中可以找到的全部内容:POP and IMAP settings: SMTP setting Server name: smtp.office365.com Port: 587 Encryption method: TLS 我尝试更改设置(主机、端口、加密)并仔细检查了登录凭据,但问题仍然存在。无论我做什么,我总是得到同样的错误(在页面加载很长时间之后):“stream_socket_enable_crypto():SSL:加密启用超时”请记住,我可以使用 Phpmailer 脚本轻松发送一封电子邮件同一台服务器上的相同设置!
      • 你可以用gmail代替outlook
      • 很遗憾,我不能,我需要通过我们公司的邮件发送电子邮件,该邮件由 Microsoft 托管。
      • 这将是我最后的选择。如果 Codeogniter 中的库非常好,我看不出使用和外部库的意义。我知道这个问题在某种程度上与服务器设置有关,但我对服务器了解不多,无法弄清楚。
      【解决方案4】:
      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";
      }
      ?>
      

      【讨论】:

        【解决方案5】:

        对于那些在 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
        

        【讨论】:

          猜你喜欢
          • 2020-02-06
          • 2023-03-03
          • 2018-08-19
          • 1970-01-01
          • 2011-05-21
          • 2020-09-07
          • 1970-01-01
          • 2021-05-06
          • 2014-03-08
          相关资源
          最近更新 更多