[英]PHP mail() is not accepting variables
For some reason the $name, $email and $message variables aren't being passed into the $msgcontents and $headers variables. 由于某种原因,没有将$ name,$ email和$ message变量传递到$ msgcontents和$ headers变量中。 After I fill out the form and click submit the message in my inbox looks like this:
在填写表单并单击提交后,收件箱中的消息如下所示:
Subject: Message from Contact Form 主题:联系表中的消息
From: Unknown sender 发件人:未知发件人
Date: Mon, November 17, 2014 7:44 pm 日期:2014年11月17日星期一7:44 pm
To: myemail@myemail.com 至:myemail@myemail.com
Name: 名称:
Email: 电子邮件:
Message: 信息:
I've used var_dump() to see if my variables are being populated after clicking submit and they are but for some reason they are not being picked up by $msgcontents and $headers. 我使用了var_dump()来查看单击提交后是否填充了我的变量,但是由于某些原因,它们没有被$ msgcontents和$ headers占用。 Where am I going wrong?
我要去哪里错了?
Here's my code: 这是我的代码:
<?php
$name = trim(htmlspecialchars($_POST['name'], ENT_QUOTES));
$email = trim($_POST['email']);
$message = trim(htmlspecialchars($_POST['message'], ENT_QUOTES));
$to = "myemail@myemail.com";
$subject = "Message From Contact Form";
$msgcontents = "Name: $name <br> Email: $email <br> Message: $message";
$headers = array("MIME-VERSION: 1.0",
"Content-type: text/html; charset=iso-8859-1",
"From: $name <$email>",
"Reply-To: contact@info84.com",
"X-Mailer: PHP/" . PHP_VERSION
);
$headers = implode("\r\n", $headers);
$mailsent = mail($to, $subject, $msgcontents, $headers);
?>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form id="contactform" method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" novalidate>
<input type="text" name="name" placeholder="Your Name">
<input type="email" name="email" placeholder="Your Email">
<textarea name="message" placeholder="Your Message"></textarea>
<input type="submit" name="submitform" value="send">
</form>
<?php
var_dump($to);
?>
<br>
<?php
var_dump($subject);
?>
<br>
<?php
var_dump($msgcontents);
?>
<br>
<?php
var_dump($headers);
?>
<br>
<?php
var_dump($name);
?>
<br>
<?php
var_dump($email);
?>
<br>
<?php
var_dump($message);
?>
</body>
</html>
I think you check the message sended from first page load (without the form) 我认为您检查了从首页加载发送的消息(没有表格)
Try to wrap your email sending code, like this: 尝试包装您的电子邮件发送代码,如下所示:
if (isset($_POST['submitform'])) {
//...
mail(...);
//...
}
Then, when page loads no message been sended, but when you click a submit button, mail been sended 然后,当页面加载时,没有发送邮件,但是当您单击“提交”按钮时,邮件已发送
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.