I'm learning PHP, and I found a piece of code that can send an email using PHP. But the email never appears in my inbox. I've tested with many emails and checked every spam box.
Here is my code:
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$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"]);
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;
}
$recipient = "contact@email.com";
$email_content = "Name: $name\n";
$email_content .= "Email: $email\n\n";
$email_content .= "Message:\n$message\n";
$email_headers = "From: $name <$email>";
if (mail($recipient, $assunto, $email_content, $email_headers)) {
http_response_code(200);
include("contact.php");
?>
<script type="text/javascript">
$(document).ready(function () {
$("#resultado").html("SUCCESS!");
setTimeout(function(){ $("#resultado").fadeOut() }, 5000);
})
</script>
It executes the 200
code as the email was sent.