我目前有一个表单,在即将到来的web服务器页面上,我需要捕获输入的电子邮件地址并将其发送到我的电子邮件地址,例如example@hotmail.com。
使用PHP实现这一点的最佳方式是什么?
<form method="post">
<input class="emailsubscribe mobile clearfix" type="text" name="mail"
placeholder="Enter your email" onFocus="if(!this._haschanged)
{this.value=''};this._haschanged=true;"/>
<input class="emailsubscribe desktop clearfix" type="text" name="mail"
placeholder="Enter your email to stay up to date" onFocus="if(!this._haschanged)
{this.value=''};this._haschanged=true;"/>
<input class="emailsubmit clearfix" type="submit" name="submit"/>
</form>
提前感谢!
HTML:
<form method="post" action="send.php">
Enter your e-mail: <input type="text" name="email" />
<input type="submit" name="submit" value="Submit" />
</form>
PHP(send.PHP):
if(!isset($_POST['submit']) || !isset($_POST['email'])) die();
$email = $_POST['email'];
mail('youremail@gmail.com', 'Subject', 'Entered e-mail: '.$email);
你应该添加一些像captcha这样的保护来防止垃圾邮件。
使用PHPMailer类发送电子邮件或者只是一个php邮件函数
表单中有两个名称相同的输入。
if (isset($_POST['submit'])) {
$email = $_POST['email']; // Check first if is a valid email address with either filter_var or a regex.
$recipient = "therecipient@email.com";
$mail_body = "Any message you wan to include here";
$subject = "From online Form";
$header = "From: Online Form: <" . $email . ">'r'n";
mail($recipient, $subject, $mail_body, $header);
}