CodeIgniter无法使用PHP邮件发送电子邮件()

时间:2022-10-08 18:14:37

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

我正在尝试发送一封带有Codeigniter的电子邮件,如下所示:

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

$this->email->from("myemail@email.com");
$this->email->reply_to("myemail@email.com");
$this->email->to("myemail@email.com");
$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.

我在电子邮件调试器上得到了这个:无法使用PHP mail()发送电子邮件。您的服务器可能未配置为使用此方法发送邮件。

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 ?

如果我使用相同的地址执行简单的PHP mail()函数,它可以工作,但是当我使用CodeIgniter时,它会给我错误。那么为什么它适用于简单的mail()而不是CodeIgniter呢?有任何想法吗 ?

Thanks.

谢谢。

11 个解决方案

#1


7  

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

你的配置文件夹中有email.php文件吗?也许你的配置存在问题。

#2


14  

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

#3


10  

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...

希望这可以帮助...

#4


8  

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:

在system / libraries / Email.php中,首先看一下第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?

所以我基本上删除了所有标题,让PHP放入默认值。答对了!没有CI的标题,它可以工作。使用CI标头,它没有。那是什么?

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.

挖掘更多,我查找了html初始化和使用的地方。事实证明,直到1046左右它才真正做任何事情,它构建了消息体。

from line 1048:

从第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.

将send_multipart翻转为TRUE和FALSE将使邮件类工作或不工作。

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

看看Code Ignitor的电子邮件类文档什么都没透露。转到第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

所以你有它。 CI如何处理多部分消息可能是错误的?隐藏的配置首选项

$config['send_multipart'] = FALSE;

in the email.php seems to do the trick.

在email.php中似乎可以解决问题。

#5


4  

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:

将协议变量添加到配置数组并为其分配值“sendmail”。 config文件夹中的email.php文件应如下所示。我的工作方式如下:

$config['protocol'] = 'sendmail';
$config['mailtype'] = 'html';
$config['charset']  = 'utf-8';
$config['newline']  = "\r\n";

#6


4  

Be sure domain name in

确保域名

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

match to server domain name

匹配服务器域名

#7


0  

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

遇到同样的问题,请确保您的“发件人”地址是有效的电子邮件地址。

#8


0  

I read a comment in the file email.php :

我在文件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!!!

“-f”标志 - 这个问题!!!

#9


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@example.com';
$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@example.com';
$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

这对我有用

#10


0  

    $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.

#11


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.

值得一提的是,如果您使用WAMP(Windows),则需要安装sendmail,否则没有默认的SMTP发送方法。我想使用Gmail,但不能,因为根本没有默认邮件机制。

#1


7  

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

你的配置文件夹中有email.php文件吗?也许你的配置存在问题。

#2


14  

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

#3


10  

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...

希望这可以帮助...

#4


8  

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:

在system / libraries / Email.php中,首先看一下第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?

所以我基本上删除了所有标题,让PHP放入默认值。答对了!没有CI的标题,它可以工作。使用CI标头,它没有。那是什么?

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.

挖掘更多,我查找了html初始化和使用的地方。事实证明,直到1046左右它才真正做任何事情,它构建了消息体。

from line 1048:

从第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.

将send_multipart翻转为TRUE和FALSE将使邮件类工作或不工作。

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

看看Code Ignitor的电子邮件类文档什么都没透露。转到第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

所以你有它。 CI如何处理多部分消息可能是错误的?隐藏的配置首选项

$config['send_multipart'] = FALSE;

in the email.php seems to do the trick.

在email.php中似乎可以解决问题。

#5


4  

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:

将协议变量添加到配置数组并为其分配值“sendmail”。 config文件夹中的email.php文件应如下所示。我的工作方式如下:

$config['protocol'] = 'sendmail';
$config['mailtype'] = 'html';
$config['charset']  = 'utf-8';
$config['newline']  = "\r\n";

#6


4  

Be sure domain name in

确保域名

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

match to server domain name

匹配服务器域名

#7


0  

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

遇到同样的问题,请确保您的“发件人”地址是有效的电子邮件地址。

#8


0  

I read a comment in the file email.php :

我在文件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!!!

“-f”标志 - 这个问题!!!

#9


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@example.com';
$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@example.com';
$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

这对我有用

#10


0  

    $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.

#11


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.

值得一提的是,如果您使用WAMP(Windows),则需要安装sendmail,否则没有默认的SMTP发送方法。我想使用Gmail,但不能,因为根本没有默认邮件机制。