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

Email CodeIgniter无法使用PHP mail()发送电子邮件,email,codeigniter,Email,Codeigniter,我试图用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"

我试图用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();
$config['protocol'] = 'sendmail';
$config['mailtype'] = 'html';
$config['charset']  = 'utf-8';
$config['newline']  = "\r\n";
我在电子邮件调试器中得到了这个消息:无法使用PHP mail()发送电子邮件。您的服务器可能未配置为使用此方法发送邮件

如果我使用相同的地址发送简单的PHP mail()函数,它会工作,但当我使用CodeIgniter时,它会给出错误。那么,为什么它可以使用simple mail()而不能使用CodeIgniter呢?有什么想法吗


谢谢。

您的配置文件夹中有email.php文件吗?您的配置可能有问题。

向配置数组添加一个协议变量,并将其赋值为“sendmail”。config文件夹中的email.php文件应如下所示。我的工作原理如下:

$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();
$config['protocol'] = 'sendmail';
$config['mailtype'] = 'html';
$config['charset']  = 'utf-8';
$config['newline']  = "\r\n";
他也有类似的问题

这是来自控制器的工作代码:

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

似乎没有人真正找到一个明确的答案,所以我做了一些调查,找出了原因

在system/libraries/Email.php中,首先查看第1552行:

if ( ! mail($this->_recipients, $this->_subject, $this->_finalbody, $this->_header_str, "-f ".$this->clean_email($this->_headers['From'])))
它似乎把一切都像桃子一样送去了。我也有完全相同的症状。为了看我是不是疯了,我就插了进去

mail($this->_recipients, $this->_subject, $this->_finalbody)
所以我基本上删除了所有的头文件,让PHP使用默认值。答对 了没有CI的头,它就可以工作。对于CI头,它不会。那是什么呢

再仔细研究一下,我找到了初始化和使用html的地方。事实证明,直到1046年左右,它才真正做任何事情,在那里它构建了消息体

第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;
}
在TRUE和FALSE之间翻转send\u multipart将使邮件类工作或不工作

透过这本书什么也看不出来。转到第52行:

var $send_multipart = TRUE; // TRUE/FALSE - Yahoo does not like multipart alternative, so this is an override.  Set to FALSE for Yahoo.
好了。CI处理多部分消息的方式可能存在错误?隐藏的配置首选项

$config['send_multipart'] = FALSE;
在email.php中似乎可以做到这一点。

确保域名在

$this->email->from("myemail@**email.com**");
匹配服务器域名

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

显然,似乎没有一个明确的“一刀切”的答案。对我有用的东西正在改变

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


希望这有帮助…

我在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”标志-此问题

我也遇到了同样的问题,尽管我现在觉得这很傻,但我还是将一些配置数组选项大写,而它们都需要小写:

是:

固定的:

$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);
这对我很有用

$config=Array(
    $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.
'协议'=>'smtp', 'smtp_主机'=>'ssl://smtp.googlemail.com', “smtp_端口”=>465, “smtp_用户”=>“电子邮件”, “smtp_pass”=>“pass”, “邮件类型”=>“html”, “字符集”=>“iso-8859-1” ); $this->load->library('email',$config);
**$this->email->set_newline(“\r\n”);** 值得一提的是,如果您使用的是WAMP(Windows),则需要安装sendmail,否则没有默认的SMTP发送方法。我想使用Gmail,但无法使用,因为根本没有默认的邮件机制。

确保Apache可以发送电子邮件

要检查您当前的sendmail状态,请执行以下操作:
sestatus-b | grep httpd_can_sendmail

如果它处于关闭状态,请将其更改为:
sudo setsebool-P httpd_可以发送邮件

是的,我有一个email.php配置,它是默认的,只保存以下内容:$config['mailtype']='html'$配置['charset']='utf-8'$配置['newline']='\r\n';也许你应该试试$config['mailtype']='text';邮件类型html已经给我带来了一些问题。或者尝试删除整个配置文件以使用默认设置。我会尝试,谢谢。但我不应该有这个问题,我希望能够发送HTML消息,如果需要的话。我可以用mail()来完成,但很遗憾,我不能用这么大的框架来完成。我仍然怀疑我是否做错了什么=>
$config['protocol']='mail'
您使用的是哪种协议?中的参考通常也适用于CodeIgniter包装。这是一个很好的观点,CodeIgniter电子邮件脚本中的评论“这是一个MIME格式的多部分邮件。您的电子邮件应用程序可能不支持此格式。”我发现邮件($this->\u recipients,$this->\u subject,$this->\u finalbody)结合$config['send_multipart']=FALSE$config['mailpath']=“/usr/sbin/sendmail”;解决了我的问题,这才是真正的解决办法。我也准备把它发出去。
sendmail
的位置解决了我的问题,当它实际在
sbin
中时,我使用了默认的
bin
,真有趣。对我来说,效果正好相反,即将协议从“邮件”更改为“smtp”(并添加“smtp_主机”和“smtp_端口”,如@Fedir的回答所示)。如上所述,一种大小并不适合所有人——主要是因为每个人都在不同的服务器环境中执行此操作。这个故事的寓意是:看看所有这些答案,尝试不同的答案。(我的线索是注意到PHP
mail()
函数的文档——它对我有用——顺便提到它使用SMTP。)如果您使用SMTP,请确保还设置了SMTP_主机。localhost可能应该是框架的默认smtp_主机。