PHP电子邮件不发送

时间:2014-09-17 08:55:43

标签: php email

我有以下功能:

    public function retrieve_password()
    {
// multiple recipients
        $to  = 'mr@businesspower.dk' . ', '; // note the comma
        $to .= 'mr@businesspower.dk';

// subject
        $subject = 'Birthday Reminders for August';

// message
        $message = '
<html>
<head>
  <title>Birthday Reminders for August</title>
</head>
<body>
  <p>Here are the birthdays upcoming in August!</p>
  <table>
    <tr>
      <th>Person</th><th>Day</th><th>Month</th><th>Year</th>
    </tr>
    <tr>
      <td>Joe</td><td>3rd</td><td>August</td><td>1970</td>
    </tr>
    <tr>
      <td>Sally</td><td>17th</td><td>August</td><td>1973</td>
    </tr>
  </table>
</body>
</html>
';

// To send HTML mail, the Content-type header must be set
        $headers  = 'MIME-Version: 1.0' . "\r\n";
        $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

// Additional headers
        $headers .= 'To: Mary <mary@example.com>, Kelly <kelly@example.com>' . "\r\n";
        $headers .= 'From: Birthday Reminder <birthday@example.com>' . "\r\n";
        $headers .= 'Cc: birthdayarchive@example.com' . "\r\n";
        $headers .= 'Bcc: birthdaycheck@example.com' . "\r\n";

// Mail it
        mail($to, $subject, $message, $headers);

    }

现在我知道从我的本地计算机上发送它有时会出现问题,所以现在它在我的服务器上是在线的。

但是,当我使用此功能发送电子邮件时,电子邮件永远不会到达。

我知道有时候你需要smtp并设置一个用户应该发送电子邮件,但我不太清楚该怎么做以及是否需要它?

有人能指出我正确的方向吗?

1 个答案:

答案 0 :(得分:3)

不幸的是,在PHP中使用mail函数时,它实际上并没有给出很多错误,也不是一个非常好的函数。

问题可能来自错误设置的sendmail服务器,或者可能是服务器是localhost并且尚未配置为在本地服务器之外发送邮件。

您最好的选择是使用php类/库,例如PHPMailer。通过这种方式,您可以连接到SMTP服务器,它也可以更容易地进行调试,而不是使用邮件功能。

有很多变量可能导致您的问题。下面列出了一些。

希望这有帮助。