14

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('info@gatewaykhobar.com','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);
            }

        }
Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
Muhammed Anas U
  • 305
  • 1
  • 4
  • 15
  • 2
    [link 1](https://stackoverflow.com/questions/29972179/unable-to-send-email-using-php-mail-your-server-might-not-be-configured-to-se) OR [link 2](https://stackoverflow.com/questions/3859958/codeigniter-unable-to-send-email-using-php-mail) OR [link 3](https://stackoverflow.com/questions/23234021/codeigniter-smtp-email-error) – Alive to die - Anant Jul 22 '17 at 08:48
  • As far as I know a host shouldn't include the protocol (e.g. `ssl://`) in it. – apokryfos Jul 22 '17 at 08:48
  • i removed the ssl:// but the smae error happen. @apokryfos – Muhammed Anas U Jul 22 '17 at 08:57
  • i read the all links but not happen the error @AlivetoDie – Muhammed Anas U Jul 22 '17 at 08:59
  • Is this live server a Linux server? If so, did you install mailutils on your live server? – Daniel Jul 24 '17 at 21:20
  • @Daniel -- Yes it is a Linux Server? mailutlls installed, – Muhammed Anas U Jul 25 '17 at 12:49
  • @AnasUmmalil, if you are using Ubuntu 16, check your php.ini file in /etc/php/7.0/apache2. If you are using gmail for example, you will have to scroll down to the [mail function] and uncomment SMTP = smtp.gmail.com, it's probably showing SMTP = localhost right now. Hope that helps. – Daniel Jul 25 '17 at 23:49
  • @Daniel Iam using live server – Muhammed Anas U Jul 26 '17 at 12:54
  • @AnasUmmalil, great, so in your live server, assuming its an Ubuntu 16, go to /etc/php/7.0/apache2, again assuming you are using Apache web server and sudo into the php.ini file. Search for [mail function] and underneath there look for SMTP = localhost. Assuming you are using gmail, replace localhost with smtp.gmail.com. – Daniel Jul 26 '17 at 15:35

8 Answers8

27

Change smtp_port from 465 to 587.

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

Lucius
  • 1,246
  • 1
  • 8
  • 21
katwekibs
  • 1,342
  • 14
  • 17
  • Thank you! that is so strange... why port 465 doenst work on codeigniter? o.O, Ive always used Swift-4.* php to send emails without any problem using 465 port... – Diogo Garcia Apr 13 '18 at 01:42
  • For me, changing $config['newline'] = "\r\n" to $config['newline'] = "\r" works though. Before this is "\r\n", but this cause the email subject to show some weird encoding content. – Eddy Goh Mar 20 '20 at 06:38
  • changing the port to 587 worked – Vicky Jan 04 '22 at 14:56
  • This was it. The $config['newline'] = "\r\n"; part solved it for me. – gvillavizar Dec 12 '22 at 16:02
10
$mail_config['smtp_host'] = 'smtp.gmail.com';
$mail_config['smtp_port'] = '587';
$mail_config['smtp_user'] = 'user@example.com';
$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

Tadeu RT
  • 101
  • 1
  • 5
5

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.

pbarney
  • 2,529
  • 4
  • 35
  • 49
4

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;
Christian
  • 57
  • 1
  • 1
  • 1
    Actually what I found is by changing it to mail, the emails are sent from the server mail itself. So this doesn't work. You can check the sender details of the email you receive. – Devender Gupta Jan 21 '20 at 16:14
  • Thanks @DevenderGupta, it solve my problem – silverknightone Aug 11 '21 at 07:19
  • This is not right. What you are doing, you are telling the server to sent mail through mail service only not by SMTP. The question asked is regarding the SMTP. – rahim.nagori Aug 17 '22 at 08:21
0

It looks like the mail server is hosted by yourself as well, try sending email from any e-mail client. If it fails - there's a problem with your mailserver config, not the code you pasted - check the server logs.

Greg
  • 5,862
  • 1
  • 25
  • 52
0

I am using much time Run my configure code in localhost but it always gives me an error (Unable to send email using PHP SMTP. Your server might not be configured to send mail using this method.)

But when run this Below code in my server it works for me.

application>controller>Sendingemail_Controller.php

public function send_mail() {
    $this->load->library('email');   
    $config = array();
    $config['protocol']     = "smtp"; // you can use 'mail' instead of 'sendmail or smtp'
    $config['smtp_host']    = "ssl://smtp.googlemail.com";// you can use 'smtp.googlemail.com' or 'smtp.gmail.com' instead of 'ssl://smtp.googlemail.com'
    $config['smtp_user']    = "my@gmail.com"; // client email gmail id
    $config['smtp_pass']    = "******"; // client password
    $config['smtp_port']    =  465;
    $config['smtp_crypto']  = 'ssl';
    $config['smtp_timeout'] = "";
    $config['mailtype']     = "html";
    $config['charset']      = "iso-8859-1";
    $config['newline']      = "\r\n";
    $config['wordwrap']     = TRUE;
    $config['validate']     = FALSE;
    $this->load->library('email', $config); // intializing email library, whitch is defiend in system

    $this->email->set_newline("\r\n"); // comuplsory line attechment because codeIgniter interacts with the SMTP server with regards to line break

    $from_email = $this->input->post('f_email'); // sender email, coming from my view page 
    $to_email = $this->input->post('email'); // reciever email, coming from my view page
    //Load email library

    $this->email->from($from_email);
    $this->email->to($to_email);
    $this->email->subject('Send Email Codeigniter'); 
    $this->email->message('The email send using codeigniter library');  // we can use html tag also beacause use $config['mailtype'] = 'HTML'
    //Send mail
    if($this->email->send()){
        $this->session->set_flashdata("email_sent","Congragulation Email Send Successfully.");
        echo "email_sent";
    }
    else{
        echo "email_not_sent";
        echo $this->email->print_debugger();  // If any error come, its run
    }
}

and my view page where I defined f_email and email comes through post method. application>view>emailtesting.php

<html>
<head>    
    <title> Send Email Codeigniter </title>
</head>
<body>
<?php
echo $this->session->flashdata('email_sent');
echo form_open('/Sendingemail_Controller/send_mail');
?>
<input type = "email" name = "f_email" placeholder="sender email id (from)"  required />
<input type = "email" name = "email"  placeholder="reciever email id (to)" required />
<input type = "submit" value = "SEND MAIL">
<?php
echo form_close();
?>
</body>

if some error comes again please visit official documentation below:

https://codeigniter.com/user_guide/libraries/email.html

Sonu P.
  • 85
  • 1
  • 5
0

For anyone else who finds this error, has set the settings mentioned elsewhere (even Codeigniter 4) but still getting tht error, one way to test what is going on is from the server console and using telnet. For example:

telnet smtp.yourprovider.com 587

Then test against a different provider and see if that works. If your provider doesn't but another does then the problem is with your provider and you should contact them. If both can't connect then you should speak with your webhost.

Antony
  • 3,875
  • 30
  • 32
0

I've found another solution. I had the same problem: (Codeigniter 4 and the issue with php smtp), and the reason was 2-step verification of my gmail account, from which I wanted to send emails in my app. I fixed it by getting from my gmail account, password for my app. It works, at last.

msgarski
  • 31
  • 3