CodeIgniter unable to send email using PHP mail()
Asked Answered
P

12

19

I'm trying to send an e-mail with Codeigniter like this:

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

$this->email->from("[email protected]");
$this->email->reply_to("[email protected]");
$this->email->to("[email protected]");
$this->email->subject("Test mail");
$this->email->message("Email body");
$this->email->set_alt_message("Email body txt");
$this->email->send();

and I got this on the email debugger: Unable to send email using PHP mail(). Your server might not be configured to send mail using this method.

If I do e simple PHP mail() function with the same addresses, it works but when I use CodeIgniter it gives me the error. So why would it work with simple mail() but not with CodeIgniter ? Any ideas ?

Thanks.

Panettone answered 4/10, 2010 at 23:41 Comment(2)
which protocol are you using?Atlanta
The reference in PHP mail() form doesn't complete sending e-mail generally applies to the CodeIgniter wrapper as well.Carlock
L
9

Do you have an email.php file in your config folder? Maybe there's a problem with your configuration in there.

Lyford answered 5/10, 2010 at 1:0 Comment(4)
Yes, I do have an email.php config, it's default and holds only these: $config['mailtype'] = 'html'; $config['charset'] = 'utf-8'; $config['newline'] = '\r\n';Panettone
Maybe you should try $config['mailtype'] = 'text'; The mailtype html already caused some problems for me. Or try deleting the whole config file to use default settings.Lyford
I will try that, thanks. But I shouldn't have this problem, I want to be able to send HTML message if needed. And I can do it with mail(), and it's bad that I can't do it with such large framework. I still wonder if I am doing something wrong.Panettone
$config['protocol'] = 'sendmail'; => $config['protocol'] = 'mail';Chelton
V
16

Had similar issue.

That's working code from the controller :

        $config = array();
        $config['useragent']           = "CodeIgniter";
        $config['mailpath']            = "/usr/bin/sendmail"; // or "/usr/sbin/sendmail"
        $config['protocol']            = "smtp";
        $config['smtp_host']           = "localhost";
        $config['smtp_port']           = "25";
        $config['mailtype'] = 'html';
        $config['charset']  = 'utf-8';
        $config['newline']  = "\r\n";
        $config['wordwrap'] = TRUE;

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

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

        $this->email->from($fromEmail, $fromName);
        $this->email->to($email);

        $this->email->subject('Тест Email');
        $this->email->message($this->load->view('email/'.$type.'-html', $data, TRUE));

        $this->email->send();
Vidrine answered 21/5, 2012 at 1:42 Comment(1)
The location of sendmail solved my issue, I was using the default bin when it was actually in sbinEthiopia
K
12

Clearly, there does not seem to be a definitive 'one size fits all' answer. What worked for me was changing

$config['protocol'] = 'smtp';

TO:

$config['protocol'] = 'mail';

Hope this helps...

Kosaka answered 22/9, 2015 at 22:23 Comment(2)
Huh, funny. What worked for me was exactly the opposite, i.e., changing the protocol from 'mail' to 'smtp' (and adding 'smtp_host' and 'smtp_port' as shown in @Fedir's answer). As stated, one size does not fit all -- mainly because each of is is doing this in a different server environment. Moral of the story: look at all of these answers, and try variations. (The clue for me was noting that the documentation of the PHP mail() function -- which works for me -- mentions in passing that it uses SMTP.)Botnick
If you are using smtp, make sure to also set smtp_host. localhost should probably be the default smtp_host for the framework.Jellicoe
L
9

Do you have an email.php file in your config folder? Maybe there's a problem with your configuration in there.

Lyford answered 5/10, 2010 at 1:0 Comment(4)
Yes, I do have an email.php config, it's default and holds only these: $config['mailtype'] = 'html'; $config['charset'] = 'utf-8'; $config['newline'] = '\r\n';Panettone
Maybe you should try $config['mailtype'] = 'text'; The mailtype html already caused some problems for me. Or try deleting the whole config file to use default settings.Lyford
I will try that, thanks. But I shouldn't have this problem, I want to be able to send HTML message if needed. And I can do it with mail(), and it's bad that I can't do it with such large framework. I still wonder if I am doing something wrong.Panettone
$config['protocol'] = 'sendmail'; => $config['protocol'] = 'mail';Chelton
H
9

Nobody seemed to really find a definitive answer, so I did some digging around and found out why.

in system/libraries/Email.php, first look at line 1552:

if ( ! mail($this->_recipients, $this->_subject, $this->_finalbody, $this->_header_str, "-f ".$this->clean_email($this->_headers['From'])))

it seems to send everything all peachy like. I had the exact same symptoms too. To see if I was crazy, i inserted immediately before...

mail($this->_recipients, $this->_subject, $this->_finalbody)

so I basically removed all the headers and let PHP put in the defaults. Bingo! Without CI's headers, it works. With the CI headers, it doesn't. So what is it?

Digging around some more, I looked up to where html is initialized and used. Turns out it doesn't really do anything until around 1046, where it builds the message body.

from line 1048:

if ($this->send_multipart === FALSE)
{
    $hdr .= "Content-Type: text/html; charset=" . $this->charset . $this->newline;
    $hdr .= "Content-Transfer-Encoding: quoted-printable";
}
else
{
    $hdr .= "Content-Type: multipart/alternative; boundary=\"" . $this->_alt_boundary . "\"" . $this->newline . $this->newline;

    $body .= $this->_get_mime_message() . $this->newline . $this->newline;
    $body .= "--" . $this->_alt_boundary . $this->newline;

    $body .= "Content-Type: text/plain; charset=" . $this->charset . $this->newline;
    $body .= "Content-Transfer-Encoding: " . $this->_get_encoding() . $this->newline . $this->newline;
    $body .= $this->_get_alt_message() . $this->newline . $this->newline . "--" . $this->_alt_boundary . $this->newline;

    $body .= "Content-Type: text/html; charset=" . $this->charset . $this->newline;
    $body .= "Content-Transfer-Encoding: quoted-printable" . $this->newline . $this->newline;
}

Flipping send_multipart between TRUE and FALSE will make the mail class work or not work.

Looked through the Code Ignitor's email class docs reveals nothing. Going to line 52:

var $send_multipart = TRUE; // TRUE/FALSE - Yahoo does not like multipart alternative, so this is an override.  Set to FALSE for Yahoo.

So there you have it. Possibly an error in how CI does multipart messages? The hidden config preference

$config['send_multipart'] = FALSE;

in the email.php seems to do the trick.

Hall answered 19/4, 2013 at 5:16 Comment(2)
Good point, and this comment from codeigniter email script "This is a multi-part message in MIME format. Your email application may not support this format."Poisson
I found that the mail($this->_recipients, $this->_subject, $this->_finalbody) combined with the $config['send_multipart'] = FALSE; $config['mailpath'] = "/usr/sbin/sendmail"; Solved my issue.Woolworth
U
5

Be sure domain name in

$this->email->from("myemail@**email.com**");

match to server domain name

Unfailing answered 24/5, 2013 at 20:25 Comment(1)
This is the real solution. I was also about to post it too.Grenadine
C
3

Add a protocol variable to the config array and assign it the value "sendmail". The email.php file in the config folder should read as shown below. Mine works like this:

$config['protocol'] = 'sendmail';
$config['mailtype'] = 'html';
$config['charset']  = 'utf-8';
$config['newline']  = "\r\n";
Cringe answered 3/11, 2011 at 22:14 Comment(0)
I
2

Ensure that Apache can send emails.

To check your current sendmail status: sestatus -b | grep httpd_can_sendmail

Change it to this if it is off: sudo setsebool -P httpd_can_sendmail on

Ikkela answered 28/4, 2020 at 4:47 Comment(0)
R
1

I read a comment in the file email.php :

// most documentation of sendmail using the "-f" flag lacks a space after it, however
        // we've encountered servers that seem to require it to be in place.
        return mail($this->_recipients, $this->_subject, $this->_finalbody, $this->_header_str, '-f '.$this->clean_email($this->_headers['Return-Path']));

"-f" flag - this problem!!!

Retinue answered 23/10, 2015 at 12:25 Comment(0)
I
0

Had the same problem, Make sure your 'from' address is a valid email address.

Insubordinate answered 19/1, 2014 at 18:24 Comment(0)
R
0

I had the same problem and though it seems silly to me now, I had some of the config array options capitalized when they all need to be lowercase:

was:

$mail_config['smtp_host'] = 'mail.example.com';
$mail_config['smtp_port'] = '587';
$mail_config['smtp_user'] = '[email protected]';
$mail_config['smtp_pass'] = 'password';
$mail_config['smtp_crypto'] = 'TLS'; //ERROR
$mail_config['protocol'] = 'SMTP'; //ERROR
$mail_config['mailtype'] = 'HTML'; //ERROR
$mail_config['send_multipart'] = FALSE;
$this->email->initialize($mail_config);

Fixed:

$mail_config['smtp_host'] = 'mail.example.com';
$mail_config['smtp_port'] = '587';
$mail_config['smtp_user'] = '[email protected]';
$mail_config['smtp_pass'] = 'password';
$mail_config['smtp_crypto'] = 'tls'; //FIXED
$mail_config['protocol'] = 'smtp'; //FIXED
$mail_config['mailtype'] = 'html'; //FIXED
$mail_config['send_multipart'] = FALSE;
$this->email->initialize($mail_config);

That worked for me

Rabies answered 5/6, 2016 at 5:36 Comment(0)
H
0

Its worth saying that if you're on WAMP (Windows) you will need to have sendmail installed otherwise there is no default SMTP method of sending. I wanted to use Gmail but couldn't because there is simply no default mail mechanism.

Hardfeatured answered 30/8, 2017 at 19:40 Comment(0)
C
-1
    $config = Array(
        'protocol' => 'smtp',
        'smtp_host' => 'ssl://smtp.googlemail.com',
        'smtp_port' => 465,
        'smtp_user' => 'email',
        'smtp_pass' => 'pass',
        'mailtype'  => 'html',
        'charset'   => 'iso-8859-1'
    );

    $this->load->library('email',$config);
**$this->email->set_newline("\r\n");**  <- add this line
   this code worked for me.
Codi answered 9/2, 2017 at 5:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.