表格在网站登陆/刷新时发送电子邮件

时间:2013-10-08 10:39:12

标签: php html forms email sendmail

我的表单每次访问我的网站或刷新时都会向我发送一封空白的电子邮件(现场数据为空)。

我看了一些提到“发布/重定向/获取”的帖子,但这让我感到困惑,而且我不确定它是否属于我的确切问题。

这是我在网上找到的基本响应表单,必须找到实际发送表单数据的PHP。 (改变它以满足我的需要)

我希望有一个错误我错过了某个地方,但我似乎无法找到它。

非常感谢任何帮助。提前谢谢。

这是基本形式(减去css):

PHP:

<?php
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$formcontent=" From: $name \n Phone: $phone \n Email: $email \n Message: $message";
$recipient = "MY EMAIL HERE";
$subject = "Portfolio Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You!";
?>

HTML:

<form id="contact-form" action="/" method="post">
            <h3>Say Hello!</h3>
            <h4>Fill in the form below, and I'll get back to you as soon as i can. Thanks!</h4>
            <div>
                <label>
                    <span>Name: (required)</span>
                    <input placeholder="Please enter your name" type="text" name="name" tabindex="1" required autofocus>
                </label>
            </div>
            <div>
                <label>
                    <span>Email: (required)</span>
                    <input placeholder="Please enter your email address" type="text" name="email" tabindex="2" required>
                </label>
            </div>
            <div>
                <label>
                    <span>Telephone: (required)</span>
                    <input placeholder="Please enter your number" type="text" name="phone" tabindex="3" required>
                </label>
            </div>
            <div>
                <label>
                    <span>Message: (required)</span>
                    <textarea placeholder="Include all the details you can" type="text" name="message" tabindex="5" required></textarea>
                </label>
            </div>
            <div>
                <button name="submit" type="submit" id="contact-submit">Send Email</button>
            </div>
        </form>

2 个答案:

答案 0 :(得分:2)

使用isset

<?php
if(isset($_POST['submit']))
{
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$formcontent=" From: $name \n Phone: $phone \n Email: $email \n Message: $message";
$recipient = "MY EMAIL HERE";
$subject = "Portfolio Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You!";
}
?>

只有当它由任何人提交时,它才会post您的电子邮件。

答案 1 :(得分:0)

我已经在stackoverflow链接code link中为其他人发布了这个 这是我的code.let尝试,我希望它能帮到你

<?PHP
$email = $_POST["emailaddress"];
$to = "you@youremail.com";
$subject = "New Email Address for Mailing List";
$headers = "From: $email\n";
$message = "A visitor to your site has sent the following email address to be added to your mailing list.\n

Email Address: $email";
$user = "$email";
$usersubject = "Thank You";
$userheaders = "From: you@youremailaddress.com\n";
$usermessage = "Thank you for subscribing to our mailing list.";
mail($to,$subject,$message,$headers);
mail($user,$usersubject,$usermessage,$userheaders);
?>