我正在学习PHP,我发现了一段可以使用PHP发送电子邮件的代码。但电子邮件从未出现在我的收件箱中。我已经测试了许多电子邮件并检查了每个垃圾邮件箱。
这是我的代码:
<?php
// Only process POST reqeusts.
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Get the form fields and remove whitespace.
$name = strip_tags(trim($_POST["nome"]));
$name = str_replace(array("\r","\n"),array(" "," "),$name);
$email = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL);
$assunto = trim($_POST["assunto"]);
$message = trim($_POST["mensagem"]);
// Check that data was sent to the mailer.
if ( empty($name) OR empty($message) OR empty($assunto) OR !filter_var($email, FILTER_VALIDATE_EMAIL)) {
// Set a 400 (bad request) response code and exit.
http_response_code(400);
include("contact.php");
?>
<script type="text/javascript">
$(document).ready(function () {
alert("Oops! There was a problem with your submission. Please complete the form and try again.");
})
</script>
<?
exit;
}
// Set the recipient email address.
$recipient = "contato@gjmolter.com";
// Build the email content.
$email_content = "Name: $name\n";
$email_content .= "Email: $email\n\n";
$email_content .= "Message:\n$message\n";
// Build the email headers.
$email_headers = "From: $name <$email>";
// Send the email.
if (mail($recipient, $assunto, $email_content, $email_headers)) {
// Set a 200 (okay) response code.
http_response_code(200);
include("contact.php");
?>
<script type="text/javascript">
$(document).ready(function () {
$("#resultado").html("SUCCESS!");
setTimeout(function(){ $("#resultado").fadeOut() }, 5000);
})
</script>
<?
} else {
// Set a 500 (internal server error) response code.
http_response_code(500);
include("contact.php");
?>
<script type="text/javascript">
$(document).ready(function () {
alert("Oops! Something went wrong and we couldn't send your message.");
})
</script>
<?
}
} else {
// Not a POST request, set a 403 (forbidden) response code.
http_response_code(403);
include("contact.php");
?>
<script type="text/javascript">
$(document).ready(function () {
alert("There was a problem with your submission, please try again.");
})
</script>
<?
}
?>
它会在发送电子邮件时执行200
代码。
答案 0 :(得分:1)
你需要标题吗?如果没有,你可以尝试这个代码:
mail("yourmail@mail.com","subject","The message's body");