通过HTML发送邮件时为空

时间:2014-05-30 19:11:45

标签: php html email

以下一些帮助将不胜感激!我一直在搜索网络和stackoverflow几个小时。我的php邮件功能存在这个问题。

<form method="POST" action="contact.php" onsubmit="validateForm()" enctype="application/x-www-form-urlencoded">
<p>Name*</p> <input type="text" name="name" required>
<p>Mail*</p> <input type="email" name="mails" required>
<p>Phone*</p> <input type="tel" name="phones" required>
<p>Subject*</p> <input type="text" name="subjects" required>
<p>Message*</p> <textarea name="messages" required></textarea>
<input type="submit"> 
<input type="reset" value="Clear">
</form> 

这是我的 contact.php

<?php
$field_name = $_POST['name'];
$field_email = $_POST['mails'];
$field_phones = $_POST['phones'];
$field_subject = $_POST['subjects'];
$field_message = $_POST['messages'];

$mail_to = 'example@gmail.com';
$subject = 'Message from a site visitor '.$field_subject;

$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Phone : '.$field_phones."\n";
$body_message .= 'Message: '.$field_message;

$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";

$mail_status = mail($mail_to, $subject, $body_message, $headers);

if ($mail_status) {?>
    <script language="javascript" type="text/javascript">
        alert('Thank you for the message. We will contact you shortly.');
            window.location = 'http://www.example.com';
    </script>
<?php
}
else { ?>
    <script language="javascript" type="text/javascript">
        alert('Message failed. Please, send an email to ');
            window.location = 'http://www.example.com';
                </script>
<?php
}
?>

当我尝试echo时,它显示但无法通过mail()发送电子邮件

2 个答案:

答案 0 :(得分:0)

你应该考虑使用php来验证表单。您可能还希望对每个字段使用strip_tags()trim(),但您可以尝试以下操作。 如果您收到错误消息,则您的帖子为空,否则其他错误

<form action="contact.php" method="POST">
    <ul>
        <li>Name*</li> 
        <li><input type="text" name="name" required></li>

        <li>Mail*</li> 
        <li><input type="email" name="mails" required></li>

        <li>Phone*</li> 
        <li><input type="tel" name="phones" required></li>

        <li>Subject*</li> 
        <li><input type="text" name="subjects" required></li>

        <li>Message*</li> 
        <li><textarea name="messages" required></textarea></li>

        <li><input type="submit" value="Submit"></li>
        <li><input type="reset" value="Clear"></li>
    </ul>
</form>

contact.php:

<?php
if($_POST) {
    $name = mysql_real_escape_string($_POST['name']);
    $email = mysql_real_escape_string($_POST['mails']);
    $phones = mysql_real_escape_string($_POST['phones']);
    $subject = mysql_real_escape_string($_POST['subjects']);
    $message = mysql_real_escape_string($_POST['messages']);

    if(!empty($name) && !empty($email) && !empty($phones) && !empty($subject) && !empty($message)) {
        $mail_to = 'example@gmail.com';
        $subject = 'Message from a site visitor '.$field_subject;

        $body_message = 'From: '.$field_name."\n";
        $body_message .= 'E-mail: '.$field_email."\n";
        $body_message .= 'Phone : '.$field_phones."\n";
        $body_message .= 'Message: '.$field_message;

        $headers = 'From: '.$field_email."\r\n";
        $headers .= 'Reply-To: '.$field_email."\r\n";

        $mail_status = mail($mail_to, $subject, $body_message, $headers);

        if ($mail_status) {?>
            <script language="javascript" type="text/javascript">
                alert('Thank you for the message. We will contact you shortly.');
                    window.location = 'http://www.example.com';
            </script>
        <?php
        }
        else { ?>
            <script language="javascript" type="text/javascript">
                alert('Message failed. Please, send an email to ');
                    window.location = 'http://www.example.com';
                        </script>
        <?php
        }
    } else {
        echo 'There are empty fields';
    }
}
?>

而不是使用javascript来弹出窗口,你可能想要做这样的事情:

if ($mail_status) {
            header('Location: somefile.php?success');
            exit();
        }else { 
            echo 'Something went wrong';
        }

在该文件中,您可以通过以下方式取得成功:

if(isset($_GET['success'])) {
    echo '<p>Thank you for the message. We will contact you shortly.</p>';
}

答案 1 :(得分:0)

我不知道我是否会找到答案,但也许我会偶然发现一些事情...... 记住,您需要和您的网络服务器可以通话的SMTP服务器。 mail()函数 除非你设置一个沙箱,否则它不能在沙盒中(在家里,在你的计算机上)工作!另外,在php.ini中 搜索smtp。您可以设置SMTP服务器的域名或IP。 但是,再次,您的网络服务器 必须被授权将邮件发送到该SMTP服务器。

注意最终,您想要进行服务器端输入验证。此外,警惕注入电子邮件。

//1. Use lowercase post in your <form> tag. You might try removing the onsubmit="validateFrom()" attribute
//of your <form> tag so that you can get a basic submission and e-mail to work.

<form method="post" action="contact.php" onsubmit="validateForm()" enctype="application/x-www-form-urlencoded">

//2. Use the <label> tag for your control labels.
//3. Self-close the <input> tags

<p>Name*</p> <input type="text" name="name" required />
<p>Mail*</p> <input type="email" name="mails" required />
<p>Phone*</p> <input type="tel" name="phones" required />
<p>Subject*</p> <input type="text" name="subjects" required />
<p>Message*</p> <textarea name="messages" required></textarea>

//4. You should be doing this kind of validation before attempting to copy the values from $_POST
//Better look up empty() in the PHP Manual to make sure that you really want to exclude all that
//empty() returns `false` for.  isset() is probably more appropriate in this situation.

PHP Manual: empty()

PHP Manual: isset()

if(!empty($name) && !empty($email) && !empty($phones) && !empty($subject) && !empty($message)) {