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

7

13

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

        }
Chrysostom answered 22/7, 2017 at 8:45 Comment(9)
link 1 OR link 2 OR link 3Walking
As far as I know a host shouldn't include the protocol (e.g. ssl://) in it.Sympathy
i removed the ssl:// but the smae error happen. @SympathyChrysostom
i read the all links but not happen the error @AlivetoDieChrysostom
Is this live server a Linux server? If so, did you install mailutils on your live server?Pahoehoe
@Pahoehoe -- Yes it is a Linux Server? mailutlls installed,Chrysostom
@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.Pahoehoe
@Pahoehoe Iam using live serverChrysostom
@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.Pahoehoe
B
33

Change smtp_port from 465 to 587.

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

Bendite answered 14/8, 2017 at 15:13 Comment(4)
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...Odoric
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.Nonce
changing the port to 587 workedChatoyant
This was it. The $config['newline'] = "\r\n"; part solved it for me.Danny
P
11
$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

Purl answered 23/1, 2019 at 21:43 Comment(0)
H
7

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.

Halfcock answered 28/6, 2018 at 20:34 Comment(1)
After ages of debugging, this was the fix for me. Seems like the plex that was hosting my email SMTP site upgraded and required the "$this->email->set_newline("\r\n"); "Stroke
T
5

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;
Tasse answered 3/7, 2019 at 5:50 Comment(3)
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.Pail
Thanks @DevenderGupta, it solve my problemExtraditable
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.Diplomatic
P
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']    = "[email protected]"; // 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

Phenica answered 13/2, 2020 at 12:35 Comment(0)
L
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.

Lair answered 1/2, 2022 at 12:43 Comment(0)
T
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.

Thapsus answered 14/1, 2023 at 15:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.