试图使用代码点火器和电子邮件无法发送邮件

时间:2017-09-12 14:03:41

标签: php codeigniter email

代码未向接收方发送电子邮件....................................

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();    

我想知道控制器中此代码的问题。

3 个答案:

答案 0 :(得分: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();
    }   
}

答案 1 :(得分:0)

首先通过最后添加此内容来检查是否尝试发送:

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

并替换它:

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

到此:

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

答案 2 :(得分:0)

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

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(布尔值false)。 run()方法   只有在没有成功应用规则的情况下才返回TRUE   他们中的任何一个都失败了。

基本上暗示如果没有定义验证规则且验证不成功,run()方法将始终返回FALSE ,从而阻止内部代码正在执行的if语句。

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

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

第五,我解决了一些{}问题。

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