0

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.

  • Are you running this from a web-server or your own computer? – jeroen Aug 16 '14 at 01:53
  • Own computer, but using MAMP. –  Aug 16 '14 at 01:56
  • 1
    That's probably the problem, your mail will have been deleted before it even gets to the spam filter on the receiving server as it is sent from a highly unreliable address. It's probably been blocked by your ISP already. – jeroen Aug 16 '14 at 01:58
  • 1
    Your ISP or your ISP smtp server should sent mail from your email address or your computer, but can block them. If your are able to send mail with a mail client, look at the smtp server you sue and parameters and use them in php. – Shire Aug 16 '14 at 02:04

1 Answers1

0

You need the headers? if not you can try this code:

mail("yourmail@mail.com","subject","The message's body");
lacrirra
  • 98
  • 1
  • 2