【问题标题】:Unable to send email using PHP SMTP. Your server might not be configured to send mail using this method无法使用 PHP SMTP 发送电子邮件。您的服务器可能未配置为使用此方法发送邮件
【发布时间】:2017-12-28 09:13:18
【问题描述】:

我正在使用 codeigniter

我在实时服务器上执行了代码。 使用 print_debugger() 得到以下错误

无法使用 PHP SMTP 发送电子邮件。您的服务器可能不是 配置为使用此方法发送邮件。

public function sendEnquiry() {
            $this->load->library('email');
            $name = $this->input->post("fname");
            $cemail = $this->input->post("email");
            $pno = $this->input->post("phone");
            $message = $this->input->post("message");
            $config['protocol']    = 'smtp';
            $config['smtp_host']    = 'ssl://mail.gatewaykhobar.com';
            $config['smtp_port']    = '465';
            $config['smtp_timeout'] = '7';
            $config['smtp_user']    = '***********';
            $config['smtp_pass']    = '***********';
            $config['charset']    = 'utf-8';
            $config['newline']    = "\r\n";
            $config['mailtype'] = 'text'; // or html
            $config['validation'] = FALSE;

            $this->email->initialize($config);
            $this->email->from('info@gatewaykhobar.com','Gateway Restaurent Contact');
            $this->email->to($cemail); 
            $this->email->subject('Gateway Restaurent Contact Enquiry');

           $this->email->message($message);  
            $send = $this->email->send();
            if($send) {
                echo json_encode("send");
            } else {
                $error = $this->email->print_debugger(array('headers'));
                echo json_encode($error);
            }

        }

【问题讨论】:

  • link 1link 2link 3
  • 据我所知,主机不应该在其中包含协议(例如ssl://)。
  • 我删除了 ssl:// 但发生了 smae 错误。 @apokryfos
  • 我阅读了所有链接但没有发生错误@AlivetoDie
  • 这个 live 服务器是 Linux 服务器吗?如果是这样,您是否在实时服务器上安装了 mailutils?

标签: php codeigniter email


【解决方案1】:

smtp_port 从 465 更改为 587。

确保$config['newline'] = "\r\n"; 用双引号而不是单引号。

【讨论】:

  • 谢谢!太奇怪了……为什么端口 465 不能在 codeigniter 上工作? o.O,我一直使用 Swift-4.* php 使用 465 端口发送电子邮件没有任何问题...
  • 对我来说,将 $config['newline'] = "\r\n" 更改为 $config['newline'] = "\r" 是可行的。在此之前是“\r\n”,但这会导致电子邮件主题显示一些奇怪的编码内容。
  • 将端口更改为 587 工作
【解决方案2】:

看起来邮件服务器也是由您自己托管的,请尝试从任何电子邮件客户端发送电子邮件。如果失败 - 您的邮件服务器配置有问题,而不是您粘贴的代码 - 请检查服务器日志。

【讨论】:

    【解决方案3】:

    造成这种情况的一个常见原因是 CodeIgniter 与 SMTP 服务器在换行方面的交互方式。您的 SMTP 服务器可能需要 \r\n 而 CodeIgniter 正在使用 \n

    有一个简单的解决方法:在您的 $this->email->initialize() 之后,添加以下内容:

    $this->email->set_newline("\r\n");  
    

    这应该让它为你工作。

    【讨论】:

      【解决方案4】:
      $mail_config['smtp_host'] = 'smtp.gmail.com';
      $mail_config['smtp_port'] = '587';
      $mail_config['smtp_user'] = 'user@example.com';
      $mail_config['_smtp_auth'] = TRUE;
      $mail_config['smtp_pass'] = 'password';
      $mail_config['smtp_crypto'] = 'tls';
      $mail_config['protocol'] = 'smtp';
      $mail_config['mailtype'] = 'html';
      $mail_config['send_multipart'] = FALSE;
      $mail_config['charset'] = 'utf-8';
      $mail_config['wordwrap'] = TRUE;
      $this->email->initialize($mail_config);
      
      $this->email->set_newline("\r\n");
      

      我刚刚添加了最后一行

      【讨论】:

        【解决方案5】:

        只需将“邮件”用于“协议”数组项,仅此而已...

        $config = array();
                $config['useragent']    = $system_name;
                $config['mailpath']     = "/usr/bin/sendmail"; // or "/usr/sbin/sendmail"
                $config['protocol']     = "mail"; //use 'mail' instead of 'sendmail or smtp'
                $config['smtp_host']    = "your domain name";
                $config['smtp_user']    =  $from;
                $config['smtp_pass']    = "*************";
                $config['smtp_port']    =  465;
                $config['smtp_crypto']  = 'ssl';
                $config['smtp_timeout'] = "";
                $config['mailtype']     = "html";
                $config['charset']      = "utf-8";
                $config['newline']      = "\r\n";
                $config['wordwrap']     = TRUE;
                $config['validate']     = FALSE;
        

        【讨论】:

        • 其实我发现是把它改成邮件,邮件是从服务器邮件本身发送的。所以这行不通。您可以查看收到的电子邮件的发件人详细信息。
        • 感谢@DevenderGupta,它解决了我的问题
        【解决方案6】:

        我使用了很多时间在 localhost 中运行我的配置代码,但它总是给我一个错误(无法使用 PHP SMTP 发送电子邮件。您的服务器可能未配置为使用此方法发送邮件。 )

        但是当在我的服务器中运行下面的代码时,它对我有用。

        application>controller>Sendingemail_Controller.php

        public function send_mail() {
            $this->load->library('email');   
            $config = array();
            $config['protocol']     = "smtp"; // you can use 'mail' instead of 'sendmail or smtp'
            $config['smtp_host']    = "ssl://smtp.googlemail.com";// you can use 'smtp.googlemail.com' or 'smtp.gmail.com' instead of 'ssl://smtp.googlemail.com'
            $config['smtp_user']    = "my@gmail.com"; // client email gmail id
            $config['smtp_pass']    = "******"; // client password
            $config['smtp_port']    =  465;
            $config['smtp_crypto']  = 'ssl';
            $config['smtp_timeout'] = "";
            $config['mailtype']     = "html";
            $config['charset']      = "iso-8859-1";
            $config['newline']      = "\r\n";
            $config['wordwrap']     = TRUE;
            $config['validate']     = FALSE;
            $this->load->library('email', $config); // intializing email library, whitch is defiend in system
        
            $this->email->set_newline("\r\n"); // comuplsory line attechment because codeIgniter interacts with the SMTP server with regards to line break
        
            $from_email = $this->input->post('f_email'); // sender email, coming from my view page 
            $to_email = $this->input->post('email'); // reciever email, coming from my view page
            //Load email library
        
            $this->email->from($from_email);
            $this->email->to($to_email);
            $this->email->subject('Send Email Codeigniter'); 
            $this->email->message('The email send using codeigniter library');  // we can use html tag also beacause use $config['mailtype'] = 'HTML'
            //Send mail
            if($this->email->send()){
                $this->session->set_flashdata("email_sent","Congragulation Email Send Successfully.");
                echo "email_sent";
            }
            else{
                echo "email_not_sent";
                echo $this->email->print_debugger();  // If any error come, its run
            }
        }
        

        我定义 f_emailemail 的视图页面来自 post 方法。 应用程序>查看>emailtesting.php

        <html>
        <head>    
            <title> Send Email Codeigniter </title>
        </head>
        <body>
        <?php
        echo $this->session->flashdata('email_sent');
        echo form_open('/Sendingemail_Controller/send_mail');
        ?>
        <input type = "email" name = "f_email" placeholder="sender email id (from)"  required />
        <input type = "email" name = "email"  placeholder="reciever email id (to)" required />
        <input type = "submit" value = "SEND MAIL">
        <?php
        echo form_close();
        ?>
        </body>
        

        如果再次出现错误,请访问以下官方文档:

        https://codeigniter.com/user_guide/libraries/email.html

        【讨论】:

          【解决方案7】:

          对于发现此错误的其他人,已经设置了其他地方提到的设置(甚至是 Codeigniter 4)但仍然出现错误,测试正在发生的事情的一种方法是从服务器控制台并使用 telnet。例如:

          telnet smtp.yourprovider.com 587

          然后针对不同的提供商进行测试,看看是否可行。如果您的提供者没有,但另一个提供者有,那么问题出在您的提供者身上,您应该联系他们。如果两者都无法连接,那么您应该与您的虚拟主机交谈。

          【讨论】:

            猜你喜欢
            • 2019-02-23
            • 2022-01-05
            • 2018-11-29
            • 2018-10-15
            • 2018-06-07
            • 2021-06-29
            • 2019-06-27
            • 2016-11-18
            相关资源
            最近更新 更多