【问题标题】:trying to send mails using code igniter and email not working尝试使用代码点火器发送邮件并且电子邮件不起作用
【发布时间】:2017-09-12 14:03:41
【问题描述】:

代码未将电子邮件发送给收件人......................................................

class Welcome extends Frontend_Controller {
public function __construct() {
    parent::__construct();
public function Subscribe_Mail() {
    $this->_Creating_subscription_mail();
    if ($this->form_validation->run() === TRUE) {
    $email = $this->input->post('email');
    $to_email = "zzz@gmail.com";
    $from_email = trim($email);
    $config['useragent'] = "CodeIgniter";
    $config['protocol'] = "Send mail";
    $config['SMTPSecure'] = 'ssl';
    $config['smtp_host'] = 'mail.XXX.com';
    $config['smtp_user'] = 'rrrrr';
    $config['smtp_pass'] = 'xxxxxxxxxxxxxxxxxxx';
    $config['mailpath'] = '/usr/bin/sendemail';
    $config['smtp_port'] = '587';
    $config['smtp_timeout'] = '5';  
    $config['charset'] = 'iso-8859-1';
    $config['wordwrap'] = TRUE;
    $config['crlf'] = "\r\n";
    $config['newline'] = "\r\n";
    $config['mailtype'] = 'html';
    $config['validation'] = TRUE;
    $config['web_admin_email_id'] = 'support@handwritingiih.com';
    $this->load->library('email');
    $this->email->initialize($config);    
    $data = array();
    $data['email'] = $email;  
      $this->email->from('xxx@gmail.com', 'Servhigh');
      $this->email->to('$to_email');
      $this->email->subject('Request Email On Servhigh.com');
      $this->email->message('This is my message');
      $this->email->send();    

我想知道控制器中的这段代码有什么问题。

【问题讨论】:

    标签: php codeigniter email


    【解决方案1】:
    $this->email->to('$to_email');
    

    把这个改成

    $this->email->to($to_email);
    

    例子:

    $data ="hai";
    echo "$data"; // output:hai
    echo '$data'; // output:$data
    echo $data;   //output:hai
    
    $config['mailpath'] = '/usr/bin/sendemail';
    

    替换

    $config['mailpath']  = "/usr/bin/sendmail";
    

    使用它对我有用

    function send_mail()
    {
        $this->load->library('email');
        $config = array();
        $config['useragent'] = "CodeIgniter";
        $config['mailpath']  = "/usr/bin/sendmail";
        $config['protocol']  = "smtp";
        $config['smtp_host'] = "smtp.sendgrid.net";
        $config['smtp_user'] = "your_user_name";
        $config['smtp_pass'] = "your_password";        
        $config['smtp_port'] = "25";
        $config['mailtype']  = 'html';
        $config['charset']   = 'utf-8';
        $config['newline']   = "\r\n";
        $config['wordwrap']  = TRUE;
        $this->load->library('email');
        $this->email->initialize($config);        
        $this->email->subject('TEST SUBJECT');
        $this->email->message("THIS IS A TEST MESSAGE");
        $this->email->from( "yourmailid"  );        
        $this->email->to("yourmailid");
        if($this->email->send())
        {
        echo "success";
        }
        else
        {
        echo $this->email->print_debugger();
        }   
    }
    

    【讨论】:

    • 请使用 print_debugger 检查邮件是否发送 echo $this->email->print_debugger();
    • 检查您的 smtp 用户名和密码。
    • 我使用了代码,它显示“SMTP connect() failed”。
    • 您的smtp用户名和密码是否错误,请确认。
    • 我将用户更改为我自己的电子邮件地址并显示此错误:0 php_network_getaddresses: getaddrinfo failed: No such host is known
    【解决方案2】:

    首先检查它是否正在尝试发送,方法是在末尾添加:

    if(!$this->email->send()){
       echo "Failed to send mail":
    } else {
       echo "Working... Mail Send";
    }
    

    然后替换这个:

    $this->email->to('$to_email');
    

    到这里:

    $this->email->to($to_email);
    

    【讨论】:

      【解决方案3】:

      请尝试以下代码(请根据您的要求更改凭据):

      class Welcome extends CI_Controller {
      
          public function __construct() {
              parent::__construct();
          }
      
          public function subscribe_mail() {
              $config['smtp_host'] = 'mail.xxx.com';
              $config['smtp_user'] = 'rrrrr';
              $config['smtp_pass'] = 'xxxxxxxxxxxxxxxxxxx';
              $config['charset'] = 'iso-8859-1';
              $config['crlf'] = "\r\n";
              $config['newline'] = "\r\n";
              $config['mailtype'] = 'html';
              $config['validate'] = TRUE;
              $this->load->library('email');
              $this->email->initialize($config);    
              $this->email->from('your@example.com', 'Your Name');
              $this->email->to('someone@example.com');
              $this->email->subject('Request Email On Servhigh.com');
              $this->email->message($this->input->post('email'));
              if($this->email->send()) {
                  echo "success";
              }
              else {
                  echo $this->email->print_debugger();
              }   
          }
      }
      

      首先,我把方法名从Subscribe_Mail()改成了subscribe_mail()

      其次,我删除了if ($this->form_validation->run() === TRUE) {,因为我没有找到任何定义的验证规则。

      来自manual

      由于您没有告诉表单验证类验证任何内容 然而,它默认返回 FALSE (boolean false)。 run() 方法 仅在成功应用您的规则时才返回 TRUE 他们中的任何一个都失败了。

      这基本上意味着如果没有定义验证规则并且验证不成功,则run() 方法将始终返回FALSE,从而阻止@987654329 内部的代码@ 语句被执行。

      第三,我删除了一些不必要的配置(默认情况下它们已经具有指定的值)。

      第四,我将Frontend_Controller改为CI_Controller并删除了$this->_Creating_subscription_mail();

      第五,我修复了一些{} 的问题。

      附注:虽然为了方便我删除了验证部分,但在实际实现中,您应该在处理之前验证数据。

      【讨论】:

      • 抱歉,我无法跟进,因为我度过了艰难的一周。我对代码做了一些修改。如果您还没有找到解决方案,请尝试代码并告诉我。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-06
      • 1970-01-01
      • 2013-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多