fana
fana

Reputation: 373

Send email with codeigniter and ajax: aborted request

I'm using ajax to send an email through a contact form in codeigniter. The ajax (jquery) part is:

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

And the controller is:

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'] = "\r\n";

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

This actually sends the email correctly but the ajax call gets aborted and I never get a message back.

But if I remove the $this->email->send() bit, I get the response correctly but, of course, the email isn't sent.

What am I missing here?

Note: I have CSRF enabled and it's working ok in other ajax calls that query the database.

Upvotes: 1

Views: 3774

Answers (3)

Dodiya Devang
Dodiya Devang

Reputation: 1

Use the below ajax call.

$.ajax({
        url: '<?php echo site_url();?>/contact/send',
        type: 'POST',
        data: dataString,
        dataType: "json",
        async: false,
        success: function(message){
                if(message.sent){
                    $('#response_div').html("<?php echo lang('successfully_send'); ?>").delay(5000).hide('slow');
                        }
                        else{
                            $('#response_div').html("<?php echo lang('error'); ?>").delay(5000).hide('slow');
                        }
                        botao.attr('disabled', false);
               }
         });

Upvotes: 0

Ben Mullikin
Ben Mullikin

Reputation: 263

Although the above answers work, some may encounter the additional the problem that a php warning is thrown:

Message: date() [function.date]: It is not safe to rely on the system's timezone
settings.

The way you get around this is either by setting the timezone in your php.ini or by adding it manually to your codeigniter index.php.

ini_set('date.timezone', 'America/Chicago');

See Date error sending mail using codeigniter

Upvotes: 1

Catfish
Catfish

Reputation: 19294

Try setting async to false like this.

$.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);
               }
         });

Another way to try would be to use the complete function rather than the success function and lease async to true (which is the default). I think the complete function waits without a browser lock, but i'm not 100% certain of this.

Upvotes: 1

Related Questions