【问题标题】:CodeIgniter unable to send email using PHP mail()CodeIgniter 无法使用 PHP mail() 发送电子邮件
【发布时间】:2011-04-21 01:43:14
【问题描述】:

我正在尝试使用 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();

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

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

谢谢。

【问题讨论】:

标签: email codeigniter


【解决方案1】:

有类似的问题。

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

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

【讨论】:

  • sendmail 的位置解决了我的问题,我使用默认的bin,而它实际上在sbin
【解决方案2】:

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

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

到:

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

希望这会有所帮助...

【讨论】:

  • 呵呵,好笑。对我有用的恰恰相反,即将协议 from 'mail' to 'smtp' (并添加 'smtp_host' 和 'smtp_port' 如@费迪尔的回答)。如前所述,一种尺寸并不适合所有人——主要是因为每个尺寸都在不同的服务器环境中执行此操作。故事的寓意:查看所有这些答案,并尝试变化。 (我的线索是注意到 PHP mail() 函数的文档——它对我有用——顺便提到它使用 SMTP。)
  • 如果您使用的是 smtp,请务必同时设置 smtp_host。 localhost 应该是框架的默认 smtp_host。
【解决方案3】:

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

【讨论】:

  • 是的,我确实有一个 email.php 配置,它是默认的并且只包含这些: $config['mailtype'] = 'html'; $config['charset'] = 'utf-8'; $config['newline'] = '\r\n';
  • 也许你应该试试 $config['mailtype'] = 'text'; mailtype html 已经给我带来了一些问题。或者尝试删除整个配置文件以使用默认设置。
  • 我会试试的,谢谢。但我不应该有这个问题,如果需要,我希望能够发送 HTML 消息。而且我可以用mail()来做,可惜我不能用这么大的框架来做。我仍然怀疑我是否做错了什么。
  • $config['protocol'] = 'sendmail'; => $config['protocol'] = 'mail';
【解决方案4】:

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

在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_multipart 将使邮件类工作或不工作。

查看Code Ignitor's email class docs 没有发现任何问题。转到第 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 中似乎可以解决问题。

【讨论】:

  • 好点,这个来自 codeigniter 电子邮件脚本的评论“这是一个 MIME 格式的多部分消息。您的电子邮件应用程序可能不支持这种格式。”
  • 我发现mail($this->_recipients, $this->_subject, $this->_finalbody) 结合了$config['send_multipart'] = FALSE; $config['mailpath'] = "/usr/sbin/sendmail";解决了我的问题。
【解决方案5】:

确保域名在

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

匹配服务器域名

【讨论】:

  • 这是真正的解决方案。我也准备发了。
【解决方案6】:

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

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

【讨论】:

    【解决方案7】:

    确保 Apache 可以发送电子邮件。

    要检查您当前的 sendmail 状态: sestatus -b | grep httpd_can_sendmail

    如果它关闭,则将其更改为: sudo setsebool -P httpd_can_sendmail on

    【讨论】:

      【解决方案8】:

      我在文件 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”标志——这个问题!!!

      【讨论】:

        【解决方案9】:

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

        【讨论】:

          【解决方案10】:

          我遇到了同样的问题,虽然现在对我来说似乎很愚蠢,但我有一些配置数组选项在它们都需要小写时大写:

          曾经:

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

          固定:

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

          这对我有用

          【讨论】:

            【解决方案11】:

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

            【讨论】:

              【解决方案12】:
                  $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.
              

              【讨论】:

                猜你喜欢
                • 2013-06-02
                • 1970-01-01
                • 1970-01-01
                • 2012-07-24
                • 2011-05-11
                • 2013-06-15
                • 2012-10-31
                相关资源
                最近更新 更多