使用codeigniter和ajax发送电子邮件:请求终止



我使用ajax在codeigniter中通过联系人表单发送电子邮件。ajax (jquery)部分是:

var dataString = 'nome=' +  nome + '&msg=' + msg + '&email=' + email + '&secure=' + secure + '&mailto=' + mailto + '&ci_token=' + $.cookie("ci_csrfprotection_cookie");
$.ajax({
        url: '<?php echo site_url();?>/contact/send',
        type: 'POST',
        data: dataString,
        timeout: 1000,
        dataType: "json",
        success: function(msg){
                if(msg.sent){
                    $('#feedback').html("<?php echo lang('email_sucesso'); ?>").delay(6000).hide('slow');
                        }
                        else{
                            $('#feedback').html("<?php echo lang('email_erro'); ?>").delay(6000).hide('slow');
                        }
                        botao.attr('disabled', false);
                    }
                });

控制器为:

public function send()
{
    if ($this->input->post('secure') != 'siteform') {
        echo lang('erro_no_js');
    }else{
        $this->load->library('email');
        $nome = $this->input->post('nome');
        $email = $this->input->post('email');
        $msg = $this->input->post('msg');
        $mailto = $this->input->post('mailto');
        $secure = $this->input->post('secure');
        $config['protocol'] = "smtp";
        $config['smtp_host'] = "ssl://smtp.googlemail.com";
        $config['smtp_port'] = "465";
        $config['smtp_user'] = $this->settings['smtp_email'];
        $config['smtp_pass'] = $this->settings['smtp_password'];
        $config['charset'] = "utf-8";
        $config['mailtype'] = "html";
        $config['newline'] = "rn";
        $this->email->initialize($config); 
        $this->email->from($email, $nome);
        $this->email->to($mailto);
        $this->email->subject('Contacto do site');
        $this->email->message($msg);
        if ($this->email->send()){
            echo json_encode(array("sent"=>TRUE));
        }else{
            echo json_encode(array("sent"=>FALSE));
        }
    }
}

这实际上发送电子邮件正确,但ajax调用被中止,我从来没有得到一个消息返回。

但是如果我删除$this->email->send()位,我得到正确的响应,但是,当然,电子邮件没有发送。

我在这里错过了什么?

注意:我已经启用了CSRF,它在查询数据库的其他ajax调用中工作正常

试试这样设置async为false

$.ajax({
        url: '<?php echo site_url();?>/contact/send',
        type: 'POST',
        data: dataString,
        timeout: 1000,
        dataType: "json",
        async: false,
        success: function(msg){
                if(msg.sent){
                    $('#feedback').html("<?php echo lang('email_sucesso'); ?>").delay(6000).hide('slow');
                        }
                        else{
                            $('#feedback').html("<?php echo lang('email_erro'); ?>").delay(6000).hide('slow');
                        }
                        botao.attr('disabled', false);
               }
         });

另一种尝试方法是使用complete函数而不是成功函数,并将async租约为true(这是默认值)。我认为完整的功能等待没有浏览器锁定,但我不是100%确定这一点。

虽然上面的答案有效,但有些人可能会遇到抛出php警告的额外问题:

Message: date() [function.date]: It is not safe to rely on the system's timezone
settings.
解决这个问题的方法是在php.ini中设置时区,或者手动将其添加到codeigniter index.php.
ini_set('date.timezone', 'America/Chicago');

请参阅使用codeigniter发送邮件的日期错误

最新更新