PHP电子邮件表单 - 在本地启用它

时间:2014-03-01 22:22:10

标签: php html forms email localhost

我的代码是:

<?php 
if(isset($_POST['submit'])){
$to = "email@example.com"; // this is your Email address
$from = $_POST['email']; // this is the sender's Email address
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$subject = "Form submission";
$subject2 = "Copy of your form submission";
$message = $first_name . " " . $last_name . " wrote the following:" . "\n\n" . $_POST['message'];
$message2 = "Here is a copy of your message " . $first_name . "\n\n" . $_POST['message'];

$headers = "From:" . $from;
$headers2 = "From:" . $to;
mail($to,$subject,$message,$headers);
mail($from,$subject2,$message2,$headers2); // sends a copy of the message to the sender
echo "Mail Sent. Thank you " . $first_name . ", we will contact you shortly.";
// You can also use header('Location: thank_you.php'); to redirect to another page.
}
?>

<!DOCTYPE html>
<head>
<title>Form submission</title>
</head>
<body>

<form action="" method="post">
First Name: <input type="text" name="first_name"><br>
Last Name: <input type="text" name="last_name"><br>
Email: <input type="text" name="email"><br>
Message:<br><textarea rows="5" name="message" cols="30"></textarea><br>
<input type="submit" name="submit" value="Submit">
</form>

</body>
</html> 

我从php电子邮件表单的另一个问题中得到了它,我想在本地测试它。我该怎么做?

我最终计划在我正在为朋友工作的网站上发布它。

1 个答案:

答案 0 :(得分:2)

您可以通过phpmailer或swiftmailer进行smtp邮件服务器身份验证。然后您的表单将传递到目标地址。有关详细信息:https://github.com/PHPMailer/PHPMailer