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

Man*_*era 17 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");
$this->email->message("Email body");
$this->email->set_alt_message("Email body txt");
$this->email->send();
Run Code Online (Sandbox Code Playgroud)

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

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

谢谢.

Fed*_*TIK 15

有类似的问题.

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

        $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();
Run Code Online (Sandbox Code Playgroud)


mc_*_*ser 12

显然,似乎没有明确的"一刀切"的答案.对我有用的是改变

$config['protocol'] = 'smtp';
Run Code Online (Sandbox Code Playgroud)

至:

$config['protocol'] = 'mail';
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助...


mse*_*seo 8

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


pxl*_*pxl 8

似乎没有人真正找到明确的答案,所以我做了一些挖掘并发现了原因.

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

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

它似乎发送所有桃子般的东西.我也有完全相同的症状.为了看我是不是疯了,我立刻插入......

mail($this->_recipients, $this->_subject, $this->_finalbody)
Run Code Online (Sandbox Code Playgroud)

所以我基本上删除了所有标题,让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;
}
Run Code Online (Sandbox Code Playgroud)

在TRUE和FALSE之间翻转send_multipart将使邮件类工作或不工作.

看看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.
Run Code Online (Sandbox Code Playgroud)

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

$config['send_multipart'] = FALSE;
Run Code Online (Sandbox Code Playgroud)

在email.php似乎可以做到这一点.


小智 5

确保域名在

$this->email->from("myemail@**email.com**");
Run Code Online (Sandbox Code Playgroud)

与服务器域名匹配