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

6gpjuf90  于 2023-01-15  发布在  PHP
关注(0)|答案(8)|浏览(220)

我正在使用代码点火器
我在实时服务器上执行了代码。使用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);
            }

        }
kd3sttzy

kd3sttzy1#

smtp_port从465更改为587。
确保$config['newline'] = "\r\n";用双引号而不是单引号括起来。

eimct9ow

eimct9ow2#

$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");

我刚加了最后一行

o0lyfsai

o0lyfsai3#

出现此问题的常见原因是CodeIgniter与SMTP服务器在换行符方面的交互方式。您的SMTP服务器可能需要\r\n,而CodeIgniter正在使用\n
有一个简单的解决办法:在$this->email->initialize()之后,添加以下内容:

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

这应该能让它为你工作。

xxb16uws

xxb16uws4#

只需将“mail”用于“protocol”数组项,就可以了...

$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;
oalqel3c

oalqel3c5#

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

kuhbmx9i

kuhbmx9i6#

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

应用程序〉控制器〉发送电子邮件_控制器.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

gzszwxb4

gzszwxb47#

对于发现此错误的其他任何人,即使已设置了其他地方提到的设置(甚至Codeigniter 4),但仍然得到此错误,一种测试发生了什么的方法是从服务器控制台使用telnet。
telnet smtp.yourprovider.com 587
然后测试不同的供应商,看看是否工作。如果你的供应商没有,但另一个可以,那么问题是与您的供应商,你应该联系他们。如果两者都不能连接,那么你应该与您的网站主机。

5fjcxozz

5fjcxozz8#

我找到了另一个解决办法。我也有同样的问题:(Codeigniter 4和php smtp的问题),原因是我的gmail帐户的两步验证,我想从我的应用程序发送电子邮件。我修复了它从我的gmail帐户,我的应用程序的密码。它的工作,在最后。

相关问题