0

Everything seems to working properly, and I'm not getting any errors on front end or from the network.

However, when I test the code I am not receiving an email to the $to account.

Question

How can I alter the code below so an email is sent and received.

Here is my code.

<?php

$to = 'test@test.com'; // Change your email address

$name = $_POST['name'];
$subject = $_POST['subject'];
$email = $_POST['email'];
$message = $_POST['message'];

// Email Submit
// Note: filter_var() requires PHP >= 5.2.0
if ( isset($email) && isset($name) && isset($subject) && isset($message) && 
  filter_var($email, FILTER_VALIDATE_EMAIL) ) {

  // detect & prevent header injections
  $test = "/(content-type|bcc:|cc:|to:)/i";
  foreach ( $_POST as $key => $val ) {
    if ( preg_match( $test, $val ) ) {
      exit;
    }
  }

$body = <<<EMAIL
subject : $subject

My name is, $name.

$message

From : $name
Email : $email

EMAIL;

$header = 'From: ' . $_POST["name"] . '<' . $_POST["email"] . '>' . "\r\n" .
  'Reply-To: ' . $_POST["email"] . "\r\n" .
  'X-Mailer: PHP/' . phpversion();

// mail( $to , $_POST['subject'], $_POST['message'], $headers );
mail($to, $subject, $body, $header);
}
?>

I think it's something small as i'm not getting any real errors and nothing is blowing up. This is some of my first times working in PHP, so perhaps I'm missing something bigger.

jwpfox
  • 5,124
  • 11
  • 45
  • 42
Chris Johnson
  • 187
  • 1
  • 13
  • What exactly is the issue? – Nathan Robb Sep 09 '16 at 19:33
  • I'm just not receiving an email to the $to account when I test it. – Chris Johnson Sep 09 '16 at 19:37
  • Have you installed 'sendmail' ? PHP mail() requires 'sendmail' to be installed (as per http://php.net/manual/en/mail.requirements.php) The install process should be similar to: https://www.abeautifulsite.net/configuring-sendmail-on-ubuntu-1404 – kdvy Sep 09 '16 at 21:03
  • What is the email that you send the message to it? Remember that some email services such as Yahoo regret emails from specific IPs due to it is being listed in spam lists. You could check your server IP from [**this link**](https://www.spamhaus.org/lookup/) – SaidbakR Sep 10 '16 at 12:09

1 Answers1

0

Your problem could be that you are testing your script in a local enviroment. You should try to test it on a live server.

Anyway if I can give you some advice I would filter the post when storing variables and I would drop the heredoc syntax, changing the script in something like this:

<?php

//this is tested, it should work (on a live server) 

$to = 'mail@mail.com'; // Change it to your email address

$name = filter_var(trim($_POST['name']), FILTER_SANITIZE_STRING);
$subject = filter_var(trim($_POST['subject']), FILTER_SANITIZE_STRING);
$email = filter_var(trim($_POST['email']), FILTER_VALIDATE_EMAIL);
$message = filter_var(trim($_POST['message']), FILTER_SANITIZE_STRING);

if ($email && isset($name) && isset($subject) && isset($message))
{

    $body  = 'Subject : ' . $subject . "\r\n";
    $body .= 'My name is, ' . $name . ".\r\n";
    $body .= $message . "\r\n";
    $body .= 'From : ' . $name . "\r\n";
    $body .= 'Email : ' . $email . "\r\n";

    // If you want to send html mail add this, if you delete this pay attention to string concatenation
    $headers  = 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

    // Additional headers
    $headers .= 'To: Me <'.$to.'>' . "\r\n";
    $headers .= 'From: '.$name.' <'.$email.'>' . "\r\n";
    // Since replies are sent to the same address in the "from" field is useless to set the "reply-to" header

    mail($to, $subject, $body, $headers);

}
 else
{
    echo ('Error: unable to send mail.');
}

?>
soulpaul
  • 174
  • 9