Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to send email using PHP SMTP. Your server might not be configured to send mail using this method

Iam using codeigniter

I exicuted the code on live server. got the following error using print_debugger()

Unable to send email using PHP SMTP. Your server might not be configured to send mail using this method.

public function sendEnquiry() {
            $this->load->library('email');
            $name = $this->input->post("fname");
            $cemail = $this->input->post("email");
            $pno = $this->input->post("phone");
            $message = $this->input->post("message");
            $config['protocol']    = 'smtp';
            $config['smtp_host']    = 'ssl://mail.gatewaykhobar.com';
            $config['smtp_port']    = '465';
            $config['smtp_timeout'] = '7';
            $config['smtp_user']    = '***********';
            $config['smtp_pass']    = '***********';
            $config['charset']    = 'utf-8';
            $config['newline']    = "\r\n";
            $config['mailtype'] = 'text'; // or html
            $config['validation'] = FALSE;

            $this->email->initialize($config);
            $this->email->from('[email protected]','Gateway Restaurent Contact');
            $this->email->to($cemail); 
            $this->email->subject('Gateway Restaurent Contact Enquiry');

           $this->email->message($message);  
            $send = $this->email->send();
            if($send) {
                echo json_encode("send");
            } else {
                $error = $this->email->print_debugger(array('headers'));
                echo json_encode($error);
            }

        }
like image 319
Muhammed Anas U Avatar asked Jul 22 '17 08:07

Muhammed Anas U


4 Answers

Change smtp_port from 465 to 587.

Make sure $config['newline'] = "\r\n"; is in double quotes not single quotes.

like image 68
katwekibs Avatar answered Oct 24 '22 23:10

katwekibs


$mail_config['smtp_host'] = 'smtp.gmail.com';
$mail_config['smtp_port'] = '587';
$mail_config['smtp_user'] = '[email protected]';
$mail_config['_smtp_auth'] = TRUE;
$mail_config['smtp_pass'] = 'password';
$mail_config['smtp_crypto'] = 'tls';
$mail_config['protocol'] = 'smtp';
$mail_config['mailtype'] = 'html';
$mail_config['send_multipart'] = FALSE;
$mail_config['charset'] = 'utf-8';
$mail_config['wordwrap'] = TRUE;
$this->email->initialize($mail_config);

$this->email->set_newline("\r\n");

I just added the last line

like image 10
Tadeu RT Avatar answered Oct 24 '22 23:10

Tadeu RT


A common cause of this is the way that CodeIgniter interacts with the SMTP server with regards to line breaks. Your SMTP server might require \r\n and CodeIgniter is using \n.

There is an easy fix: after your $this->email->initialize(), add the following:

$this->email->set_newline("\r\n");  

That should get it working for you.

like image 5
pbarney Avatar answered Oct 25 '22 01:10

pbarney


Just use "mail" for the 'protocol' array item, and that's all...

$config = array();
        $config['useragent']    = $system_name;
        $config['mailpath']     = "/usr/bin/sendmail"; // or "/usr/sbin/sendmail"
        $config['protocol']     = "mail"; //use 'mail' instead of 'sendmail or smtp'
        $config['smtp_host']    = "your domain name";
        $config['smtp_user']    =  $from;
        $config['smtp_pass']    = "*************";
        $config['smtp_port']    =  465;
        $config['smtp_crypto']  = 'ssl';
        $config['smtp_timeout'] = "";
        $config['mailtype']     = "html";
        $config['charset']      = "utf-8";
        $config['newline']      = "\r\n";
        $config['wordwrap']     = TRUE;
        $config['validate']     = FALSE;
like image 4
Christian Avatar answered Oct 25 '22 00:10

Christian