为什么这个php脚本没有邮寄给我?

时间:2010-08-04 06:14:22

标签: php

我在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>

5 个答案:

答案 0 :(得分:3)

来自mail()的PHP文档:

  

重要的是要注意这一点   因为邮件被接受了   送货,这并不意味着邮件   实际上会达到目的   目的地。

另外:mail()消息可能已被接受到邮件队列中,但由于各种原因而被退回。最好的办法是检查邮件日志 - 这取决于您的操作系统和sendmail程序。

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

答案 1 :(得分:2)

因为这似乎是测试/开发网络服务器运行

<?php echo get_cfg_var('cfg_file_path');

它将打印这个php实例使用的php.ini。在文本编辑器中打开这个php.ini并设置以下值(该指令应该已经在那里,你只需要更改它们的值)

display_startup_errors = On
display_errors = On
error_reporting = E_ALL

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

答案 2 :(得分:1)

可以禁用邮件功能。如果您在UNIX下,请检查/var/log/mail.log。

答案 3 :(得分:1)

首先, 尝试通过检查POST中的提交输入类型,在网页顶部编写PHP代码。

您是否还设置或检查了服务器的发送邮件功能?有时默认情况下不会设置/启用。请先尝试发送简单邮件进行检查,如果不起作用,请立即联系服务器管理员或向服务器提交支持服务单。

修改: - 从您的代码中,如果“mail()”函数的语法正确,通常变量“$ testmail”将始终提供“TRUE”或“1”值。如果邮件尚未排队等待发送,则只返回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";
?>

答案 4 :(得分:0)

也许你没有安装邮件服务器。

这是一个很好的指南。 http://library.linode.com/email/exim/send-only-mta-ubuntu-9.10-karmic