php - Php: Email received without any messages
问题描述
I am trying to create the "contact us" form in php and link it to my html. I do receive the email, but there is no message.
<?php
$visitor_email = $_POST['email'];
$message = $_POST['message'];
$email_from = 'web@based.com';
$email_subject = "New Form Submission";
$email_body = "User Email: $visitor_email.\n".
"User Message: $message.\n";
$to = "ur.zic@based.com";
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";
mail($to, $email_subject, $email_body, $headers);
header('Location: '.$_SERVER['HTTP_REFERER']);
?>
<form id="ContactForm" method="post" action="message.php">
<h5 class="contactUsHeading">CONTACT US</h5>
<div class="form-group">
<input
type="email"
name="email"
placeholder="youremail@greatemail.com"
class="form-control w-75 w-100-mob"
required
/>
</div>
<div class="form-group">
<textarea
name="message"
class="form-control"
rows="7"
placeholder="Tell us about your engineering challenges and we will find a way to solve them."
required
></textarea>
</div>
<div class="form-group">
<input type="submit" class="btn btn-brand btn-lg w-50 w-100-mob" value="Send us your challenge" >
</div>
</form>
If there is a problem with the code I would be happy to take any suggestions.
Best regards,
Uroš
解决方案
根据您的 PHP 版本,正确连接可能很重要
$email_body = "User Email:". $visitor_email."\r\nUser Message:". $message."\r\n";
$headers = "From: ". $email_from. "\r\n";
$headers .= "Reply-To:". $visitor_email;
// you might not need \r\n at the end if you do not add more header
如果邮件已发送,我还会添加一个条件
if(mail($to, $email_subject, $email_body, $headers)){
header("Location: ".$_SERVER['HTTP_REFERER']);
// careful with the mix of single quote for different things (above)
}
else {
die("Error with your email");
}
推荐阅读
- spring-boot - Spring Boot 手动提交事务
- hibernate - 与 JPA 存储库一起使用时 SQL 语句不起作用
- python - 需要帮助来加速此代码 - Python 和 numpy
- ubuntu-16.04 - 在 cloud-config 中使用 Jinja 模板“ssh-rsa {{ v1.public_ssh_keys }}”会给出警告“找不到 text/jinja2 类型支持的子处理程序”
- swift - iPhone Xs 中的“主线程检查器:在后台线程上调用 UI API”错误,在模拟器中工作正常
- python - Boost Python自己的模块抛出分段错误`GlobalError::PushToStack()`
- windows - IIS 管理器丢失和 HTTP 错误 503
- javascript - Jest 遇到了一个意外的令牌:实现 try/catch
- javascript - 如何根据属性的值从对象中提取
- python - 如何使用python在csv中查找重复项,然后更改行