为什么这个PHP脚本无法给我发送邮件?

4
我正在运行《Head First PHP和MySQL》第1章的示例。我将文件放在了Head Fist网站上apache的/var/www文件夹中,程序也可以运行。但是,为什么php的mailto没有起作用呢?
编辑:顺便提一下,我正在使用Ubuntu 10.04。
我在脚本中添加了两行调试代码:
$testmail = mail($to, $subject, $msg);
echo 'WAS IT MAILED? <br />'.$testmail;

为什么无法打印“WAS IT MAILED? TRUE”?为什么无法发送邮件?

编辑:我将邮件发送至我的Gmail地址是否相关?

以下是脚本:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>Aliens Abducted Me - Report an Abduction</title>
</head>
<body>
  <h2>Aliens Abducted Me - Report an Abduction</h2>

<?php
  $name = $_POST['firstname'] . ' ' . $_POST['lastname'];
  $when_it_happened = $_POST['whenithappened'];
  $how_long = $_POST['howlong'];
  $how_many = $_POST['howmany'];
  $alien_description = $_POST['aliendescription'];
  $what_they_did = $_POST['whattheydid'];
  $fang_spotted = $_POST['fangspotted'];
  $email = $_POST['email'];
  $other = $_POST['other'];

  $to = 'antoniorueda18@gmail.com';
  $subject = 'Aliens Abducted Me - Abduction Report';
  $msg = "$name was abducted $when_it_happened and was gone for $how_long.\n" .
    "Number of aliens: $how_many\n" .
    "Alien description: $alien_description\n" .
    "What they did: $what_they_did\n" .
    "Fang spotted: $fang_spotted\n" .
    "Other comments: $other";

  $testmail = mail($to, $subject, $msg);
  echo 'WAS IT MAILED? <br />'.$testmail;

  echo 'Thanks for submitting the form.<br />';
  echo 'You were abducted ' . $when_it_happened;
  echo ' and were gone for ' . $how_long . '<br />';
  echo 'Number of aliens: ' . $how_many . '<br />';
  echo 'Describe them: ' . $alien_description . '<br />';
  echo 'The aliens did this: ' . $what_they_did . '<br />';
  echo 'Was Fang there? ' . $fang_spotted . '<br />';
  echo 'Other comments: ' . $other . '<br />';
  echo 'Your email address is ' . $email;
?>

</body>
</html>

以下是HTML表单:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>Aliens Abducted Me - Report an Abduction</title>
  <link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
  <h2>Aliens Abducted Me - Report an Abduction</h2>

  <p>Share your story of alien abduction:</p>
  <form method="post" action="report.php">
    <label for="firstname">First name:</label>
    <input type="text" id="firstname" name="firstname" /><br />
    <label for="lastname">Last name:</label>
    <input type="text" id="lastname" name="lastname" /><br />
    <label for="email">What is your email address?</label>
    <input type="text" id="email" name="email" /><br />
    <label for="whenithappened">When did it happen?</label>
    <input type="text" id="whenithappened" name="whenithappened" /><br />
    <label for="howlong">How long were you gone?</label>
    <input type="text" id="howlong" name="howlong" /><br />
    <label for="howmany">How many did you see?</label>
    <input type="text" id="howmany" name="howmany" /><br />
    <label for="aliendescription">Describe them:</label>
    <input type="text" id="aliendescription" name="aliendescription" size="32" /><br />
    <label for="whattheydid">What did they do to you?</label>
    <input type="text" id="whattheydid" name="whattheydid" size="32" /><br />
    <label for="fangspotted">Have you seen my dog Fang?</label>
    Yes <input id="fangspotted" name="fangspotted" type="radio" value="yes" />
    No <input id="fangspotted" name="fangspotted" type="radio" value="no" /><br />
    <img src="fang.jpg" width="100" height="175"
      alt="My abducted dog Fang." /><br />
    <label for="other">Anything else you want to add?</label>
    <textarea id="other" name="other"></textarea><br />
    <input type="submit" value="Report Abduction" name="submit" />
  </form>
</body>
</html>

2
附注:mail() 函数返回 TRUEFALSE。在 PHP 中,当您将布尔值作为字符串打印时,TRUE 会显示为 '1',而 FALSE 则会显示为空字符串 ''。 - Álvaro González
5个回答

3

mail() 的 PHP 文档中指出:

需要注意的是,仅仅因为邮件被接收并投递成功,并不意味着该邮件一定能够到达预期的目的地。

此外:尽管 mail() 消息已经被加入邮件队列,但由于各种原因可能会被退回。最好的办法是检查您的邮件日志 - 这将取决于您的操作系统和 sendmail 程序。

从 Web 服务器发送邮件时,很可能会被归类为垃圾邮件 - 这取决于接收邮件服务器的配置,这可能导致该邮件被发送到垃圾邮件文件夹或自动删除。


1
虽然这是真的,但问题表明要么mail()返回false,要么脚本的执行甚至没有达到mail()或echo...语句。我们拭目以待;-) - VolkerK
邮件()在这里返回了false。你如何避免邮件被归类为垃圾邮件? - andandandand
我明白了,我在扫描问题时把“WAS IT MAILED? TRUE”误读成脚本的输出了。 - leepowers
哦,根据这篇帖子的被接受的答案,Gmail需要特殊配置:https://dev59.com/i3VD5IYBdhLWcg3wQZUg - andandandand

2

由于这似乎是一个测试/开发 Web 服务器运行

<?php echo get_cfg_var('cfg_file_path');

它将打印此PHP实例使用的php.ini。在文本编辑器中打开此php.ini并设置以下值(该指令应已存在,您只需更改其值)

display_startup_errors = On
display_errors = On
error_reporting = E_ALL

然后重新启动Web服务器并重试。您可能会收到一些错误/警告消息。编辑您的原始问题并添加这些消息。


我不理解 <?php get_cfg_var('cfg_file_path'); ?> 的功能,它在这里的目的是什么?我使用 Ubuntu 的搜索找到了 php.ini 文件。 - andandandand
当我试图更改文件时,我发现它是只读的(我正在尝试更改位于/ etc / php5 / apache2上的文件) - andandandand
如果这是您的开发/测试服务器,php.ini中的这些值会变得非常方便。display_errors=On可能有争议。您也可以关注error.log而不是将消息发送到浏览器。但是,error_reporting=E_ALLerror_reporting=E_ALL|E_STRICT非常有用,可以立即发现错误。 - VolkerK
我发现它是只读的 - 对于您的“普通用户”帐户可能是如此。尝试使用 sudo nano -w /etc/php5/apache2/php.ini(如果这是 php.ini 的正确路径)。 - VolkerK
感谢您对您的PHP函数进行编辑,我使用echo运行它并打印出了:/etc/php5/apache2/php.ini,这与我在Ubuntu搜索中找到的路径相同。 - andandandand
是的,这只是一个测试/游乐场服务器。 - andandandand

1
首先,尝试在网页顶部编写PHP代码,通过检查POST中的提交输入类型来实现。
此外,您是否设置或检查了服务器的发送邮件功能?有时默认情况下未设置/启用。请先尝试发送简单的邮件进行检查,如果不起作用,请立即联系服务器管理员或向服务器提交支持工单。
编辑:从您的代码中,通常变量"$testmail"将始终提供"TRUE"或"1"值,如果"mail()"函数的语法正确,则只会返回false,如果邮件未排队等待发送。
另一个要点是尝试使用以下代码:
mail($to, $subject, $message, $headers, "-femail.address@example.com"); 

其中"$headers"将包含正确的标头,第5个参数可以是您的电子邮件地址前缀为“-f”。

<?php
    $headers = 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
    $headers .= "From: My site<noreply@my_site.com>" . "\r\n";
    $headers .= "Reply-To: info@my_site.com" . "\r\n";
    $headers .= "Return-Path: info@my_site.com" . "\r\n";
    $headers .= "X-Mailer: PHP/" . phpversion() . "\r\n";
?>

如果我没有网站怎么办?我正在使用本地主机,玩弄学习php,这不是一个真正的服务器。 - andandandand
我的意思是,它在 Gmail 上仍然有效吗?我猜他们有一种阻止这被用作恶意垃圾邮件制造机的方式。 - andandandand
@dmindreader - 默认情况下,本地主机的“sendmail”功能是禁用的。您需要启用它才能将邮件发送到Gmail或任何其他服务器。 - Knowledge Craving
@dmindreader - 首先,最好发送一封测试邮件来检查"sendmail"是否正常工作。然后尝试查看Gmail是否接受您的邮件。 - Knowledge Craving

1

邮件功能可能已被禁用。如果您使用的是UNIX系统,请检查/var/log/mail.log。


0

我认为不需要安装专用或辅助的额外邮件服务器,因为文档中没有提到。 - andandandand

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接