在网站上捕获用户电子邮件地址的最佳方式

时间:2012-07-08 12:13:40

标签: php

我目前有一个表单,在我的网络服务器的即将到来的页面上,我需要捕获输入的电子邮件地址并将其发送到我的电子邮件地址,例如 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>

提前致谢!

3 个答案:

答案 0 :(得分:2)

<强> 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这样的保护来防止垃圾邮件。

答案 1 :(得分:0)

使用PHPMailer课程发送电子邮件 或只是一个php mail function

答案 2 :(得分:0)

您的表单中有2个具有相同名称的输入。

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);
}