0

我在 php 文件中有一个简单的电子邮件表单,我尝试发送电子邮件,但似乎我的变量为空。在我的代码之后我有一个回显,只是为了测试我的变量内部是否有任何值并且它们没有被打印出来。唯一打印的是“done”和“email $to”。我做错什么了吗?我从 youtube 上的帖子中遵循了这种方法,他做了完全相同的事情,并且对他有用。我还尝试了更多电子邮件 php 文件,但仍然没有。这是我的html和php代码。

php代码:

 <?php 

$name = $_REQUEST['author'];
$from = $_REQUEST['email'];
$subj = $_REQUEST['sub'];
$message = $_REQUEST['msg'];

$headers .= "From: ".$from;

$to = "mygmail@gmail.com";

$subject = $subj;
$body = $message;

if(mail($to,$subject,$body,$headers)) {
echo "An e-mail was sent to ".$to." with the subject: ".$subject;
} else {
echo "There was a problem sending the mail. Check your code and make sure that the e-mail address ".$to." is valid";
}

?>

html代码:

<form action="formtoemail.php" method="post" enctype="text/plain">
                            <p>
                                <label for="author">Name:</label> 
                                <input type="text" id="author" name="author" class="required input_field" />
                            </p>
                            <p>
                                <label for="email">Email:</label> 
                                <input type="text" id="email" name="email" class="validate-email required input_field" />
                            </p>
                            <p class="no_margin_right">
                                <label for="subject">Subject:</label> 
                                <input type="text" name="sub" id="sub" class="input_field" />
                            </p>
                            <div class="cleaner h20"></div>

                            <label for="text">Message:</label> 
                            <textarea id="msg" name="msg" rows="0" cols="0" class="required"></textarea>
                            <div class="cleaner h20"></div>

                            <input type="submit" value="Send" id="submit" name="submit" class="submit_btn float_l" />
                            <input type="reset" value="Reset" id="reset" name="reset" class="submit_btn float_r" />
                      </form>
4

4 回答 4

2

POST您通过GETtry$_POST而不是$_GET在 PHP中发送和检索

或在 HTML 中 -

承格从行动到GET而不是POST

于 2012-12-26T10:28:40.320 回答
0

在您的 HTML 代码中,将“POST”方法更改为“GET”

<form action="formtoemail.php" method="get" enctype="text/plain">
                        <p>
                            <label for="author">Name:</label> 
                            <input type="text" id="author" name="author" class="required input_field" />
                        </p>
                        <p>
                            <label for="email">Email:</label> 
                            <input type="text" id="email" name="email" class="validate-email required input_field" />
                        </p>
                        <p class="no_margin_right">
                            <label for="subject">Subject:</label> 
                            <input type="text" name="sub" id="sub" class="input_field" />
                        </p>
                        <div class="cleaner h20"></div>

                        <label for="text">Message:</label> 
                        <textarea id="msg" name="msg" rows="0" cols="0" class="required"></textarea>
                        <div class="cleaner h20"></div>

                        <input type="submit" value="Send" id="submit" name="submit" class="submit_btn float_l" />
                        <input type="reset" value="Reset" id="reset" name="reset" class="submit_btn float_r" />
                  </form>
于 2012-12-26T10:30:53.643 回答
0

使用“get”方法:

<form action="formtoemail.php" method="get" enctype="text/plain">
于 2012-12-26T10:31:20.003 回答
0

你需要改变这个:

$headers .= "From: $email";

对此:

$headers = "From: ".$from;

并改变这一点:

$subject = "$subj";
$body = "$message";

mail($to, $subject, $body, $headers);

if(mail($to, $subject, $body, $headers)) {
    echo "An e-mail was sent to $to with the subject: $subject";
} else {
    echo "There was a problem sending the mail. Check your code and make sure that the e-mail address $to is valid";
}

对此:

$subject = $subj;
$body = $message;

if(mail($to, $subject, $body, $headers)) {
    echo "An e-mail was sent to ".$to." with the subject: ".$subject;
} else {
    echo "There was a problem sending the mail. Check your code and make sure that the e-mail address ".$to." is valid";
}
于 2012-12-26T10:38:43.083 回答