【问题标题】:Why is this php script not mailing me?为什么这个 php 脚本不给我发邮件?
【发布时间】:2011-03-25 02:25:22
【问题描述】:

我正在运行 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>

【问题讨论】:

  • 旁注:mail() 返回TRUEFALSE。在 PHP 中,当您将布尔值打印为字符串时,您会得到 '1' 表示 TRUE 和 ''(空字符串)表示 FALSE。

标签: php


【解决方案1】:

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

<?php echo get_cfg_var('cfg_file_path');

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

display_startup_errors = On
display_errors = On
error_reporting = E_ALL

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

【讨论】:

  • 看不懂,它的目的是什么?我使用 ubuntu 的搜索找到了 php.ini 文件。
  • 当我尝试更改文件时,我发现它是只读的(我正在尝试更改在 /etc/php5/apache2 上找到的文件)
  • 如果这是您的开发/测试服务器,那么 php.ini 中的这些值会非常方便。 display_errors=On 可能值得商榷。您还可以密切关注 error.log,而不是将消息发送到浏览器。但是error_reporting=E_ALLerror_reporting=E_ALL|E_STRICT 对于立即发现错误非常有用。
  • “我发现它是只读的” - 可能适用于您的“普通用户”帐户。试试sudo nano -w /etc/php5/apache2/php.ini(如果那是 php.ini 的正确路径)
  • 感谢您对 php 函数的编辑,我用 echo 运行它并打印:/etc/php5/apache2/php.ini,与我在 ubuntu 的搜索中找到的路径相同。
【解决方案2】:

首先, 通过检查 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";
?>

【讨论】:

  • 如果我没有网站怎么办?我正在使用 localhost,玩弄学习 php,这不是真正的服务器。
  • 我的意思是,它仍然可以在 gmail 中使用吗?我猜他们有某种方式来避免这被用作邪恶的垃圾邮件制造机器。
  • @dmindreader - 默认情况下,本地主机的“发送邮件”功能被禁用。您需要启用它才能将邮件发送到 Gmail 或任何其他服务器。
  • @dmindreader - 首先,最好发送一封测试电子邮件以检查“sendmail”是否正常工作。然后尝试查看 Gmail 是否接受您的邮件。
【解决方案3】:

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

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

【讨论】:

  • 我认为不需要安装专用或辅助的额外邮件服务器,文档中没有提及。
【解决方案4】:

来自mail() 的 PHP 文档:

重要的是要注意 因为邮件被接受 交付,这并不意味着邮件 实际上会达到预期的 目的地。

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

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

【讨论】:

  • 虽然这是真的,但问题表明 mail() 返回 false 或者脚本的执行甚至没有到达 mail() 或 echo... 语句。我们会看到;-)
  • mail() 在此处返回 false。您将如何避免被归类为垃圾邮件的邮件?
  • 我明白了,我在扫描问题时将WAS IT MAILED? TRUE 误读为脚本的输出。
  • 哦,根据这篇文章接受的答案,gmail需要特殊配置:stackoverflow.com/questions/36079/php-mail-using-gmail
【解决方案5】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-31
    • 1970-01-01
    • 2011-09-12
    • 2020-02-09
    • 1970-01-01
    • 2011-10-30
    • 2015-01-30
    • 1970-01-01
    相关资源
    最近更新 更多