我正在尝试为我的妻子的网站开发联系表格。
我发现此代码非常有效,可以完成所需的工作(将副本发送到邮件的发件人)
我唯一想广告的是Google Recaptcha v2。我不是真正的程序员,但我想将一些代码粘贴在一起....所以,请多多包涵。
我正在使用的代码,我也在Stackoverflow上找到了
Send email with PHP from html form on submit with the same script
我已经将Recaptcha添加到html部分。但是我无法让它具有php部分来处理它。
<html>
<head>
<title>Contact Formulier</title>
<script src="https://www.google.com/recaptcha/api.js"></script>
</head>
<body>
<form action="mail_handler.php" 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>
Phone: <input type="text" name="phone">
<br>
Message:<br><textarea rows="5" name="message" cols="30"></textarea><br>
<br>
<div class="g-recaptcha" data-sitekey="MY SITEKEY-CODE"></div>
<br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
<?php
if(isset($_POST['submit'])){
$to = "[email protected]"; // 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'];
$phone = $_POST['phone'];
$subject = "Mailform from www.website.com";
$subject2 = "Copy of your message you have sent to www.website.com";
$message = $first_name . " " . $last_name . "\n" . $from . "\n". $phone . "\n" ." wrote this:" . "\n\n" . $_POST['message'];
$message2 = $first_name . ", copy of your message : " ."\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.
// You cannot use header and echo together. It's one or the other.
}
?>
我想这个网站可能会帮助您复制/粘贴您看到的所有内容https://writeup.xyz/how-to/google-recaptcha-v2-tutorial-3125/