Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Codeigniter $this->email->send() not working while mail() does

I can't figure out why if I try to use the CI Email Class it doesn't send emails, while if I use the native PHP mail() Class works.

Has to be noted that sometimes I get "email sent" while is not actually sent and sometimes I get the error "my server is not setup".

I tried to figure out how to set it up but... nothing...

Controller code follows:

 if($this->form_validation->run()){

                //Set Language
                $this->lang->load('site', $this->session->userdata('lang'));

                //Random key
                $user_valid_key = md5(uniqid());

                //Prepare email
                $this->load->library('email', array('mailtype' => 'html'));
                $this->email->from($this->config->item('email_signup_from'), 'Wondermark.net');
                $this->email->to($this->input->post('email'));
                $this->email->subject($this->lang->line('email_signup_subject'));

                //Format mail con %s per inserire i campi necessari
                $signup_msg = sprintf($this->lang->line('email_signup_message'), $this->input->post('fname'), base_url().'main/signup_confirm/'.$user_valid_key);

                $this->email->message((string)$signup_msg);

                if($this->email->send()){
                    //TODO: load view...
                    echo "email sent";
                }else{
                    $to = $this->input->post('email');
                    mail($to, 'test', 'Other sent option failed');
                    echo $this->input->post('email');
                    show_error($this->email->print_debugger());
                }

                //TODO: Add to db

            }else{

            // Form validation failed

}
like image 650
Mr.Web Avatar asked Jul 31 '13 23:07

Mr.Web


People also ask

How to send email automatically in CodeIgniter?

Simply create a new file called the email. php, add the $config array in that file. Then save the file at config/email. php and it will be used automatically.

How does PHP mail send email?

PHP makes use of mail() function to send an email. This function requires three mandatory arguments that specify the recipient's email address, the subject of the the message and the actual message additionally there are other two optional parameters. mail( to, subject, message, headers, parameters );


1 Answers

Use this setup email..

$this->load->library('email');

$config['protocol']    = 'smtp';
$config['smtp_host']    = 'ssl://smtp.gmail.com';
$config['smtp_port']    = '465';
$config['smtp_timeout'] = '7';
$config['smtp_user']    = '[email protected]';
$config['smtp_pass']    = 'password';
$config['charset']    = 'utf-8';
$config['newline']    = "\r\n";
$config['mailtype'] = 'text'; // or html
$config['validation'] = TRUE; // bool whether to validate email or not      

$this->email->initialize($config);

$this->email->from('[email protected]', 'sender_name');
$this->email->to('[email protected]'); 
$this->email->subject('Email Test');
$this->email->message('Testing the email class.');  

$this->email->send();

echo $this->email->print_debugger();
like image 96
Anand Avatar answered Sep 18 '22 21:09

Anand