如何从html表单发送php中的电子邮件

时间:2014-02-20 01:01:04

标签: php html forms email variables

以下是导致问题的PHP和HTML代码;当我按下提交 <input>时,不会发送电子邮件:

<head>
<title>`Fraud Detector`</title>
<link rel="stylesheet" type="text/css" href="CSS.css"/>
<LINK REL="SHORTCUT ICON" HREF="favicon.ico">
<body>

<form method="post" name="form1" action="submit.php">
Name: 
<br/><input type="text" name="name" /><br/>
Email:
<br/><input type="text" name="email"/><br/><br/>
<input type="submit" value="Continue"/>
</form>
<?php
$email = $_POST["email"];
$subject = "Your Account May Have Been Hacked!";
$body = "Your account may have been hacked! 
            Please click the following link to fix the problem: 
            http://testsites.webatu.com/process.php";
$from = "fraud_dept@security_inc.com";
mail($email, $subject, $body, $from);
?>
</body>
</head>

有谁可以告诉我为什么电子邮件不发送以及我做错了什么?

2 个答案:

答案 0 :(得分:1)

确保脚本名为“submit.php”,并且位于您的网络目录的根目录中。

此外,在尝试访问POST请求之前,我会添加一些检查以确保POST请求确实存在:

<?php
  if ($_POST && isset($_POST['name']) && isset($_POST['email']))
  {
    $name = $_POST['name'];
    $email = $_POST["email"];
    $subject = "Your Account May Have Been Hacked!";
    $body = "Hello {$name}: Your account may have been hacked! Please click the following 
      link to fix the problem: http://testsites.webatu.com/process.php";

    $headers = "To: {$name} <{$email}>\r\n";
    $headers .= "From: Fraud Dept <fraud_dept@security_inc.com>\r\n";

    mail($email, $subject, $body, $headers);
  }
?>

(我还添加了$name,因为您似乎忽略了现有代码中的该字段。)

答案 1 :(得分:0)

也许您可以尝试以下操作,在发送电子邮件时,您需要两个电子邮件地址接收者和来自。

<?php
if ( isset($_POST['submit']) && isset($_POST['email']) ) {
   /* To email */
   $to = $_POST["email"];

   /* Sender email*/
   $from = 'myemail@live.com';

   /* Subject */
   $subject = "Your Account May Have Been Hacked!";

   /* Body*/
   $message = "Your account may have been hacked! Please click the following link to fix the problem: http://testsites.webatu.com/process.php";

   /* Send the email */
   mail($to, $subject, $message, "From: {$from}");
}
?>

<form method="post">
<input type="text" name="email" placeholder="your email address" />
<input type="submit" name="submit" value="submit" />
</form>

我还注意到电子邮件正文中的链接托管在免费主机上,只是确保您不会同时发送许多电子邮件。一些免费主机可能会禁止您在短时间内发送多封电子邮件。