Search code examples
phphtmlformsemailhost

Simple php form to email errors


Below is my php code for a form I made. It is on my website and seems to work except it isn't forwarding the email to the one I set up in hostgator.

<?php

if (isset($_POST['submit'])) {
$name = $_POST['name'];
$subject = $_POST['subject'];
$mailfrom = $_POST['mail'];
$message = $_POST['message'];

$mailTo = "xavier@xavierbullock.com";
$headers = "From: ".$mailFrom;
$txt = "New form submission from ".$name.".\n\n".$message;

mail($mailTo, $subject, $txt, $headers);
header("Location: contact.html?mailsend");
}

?>

This is part of the error message I received in my hostgator webmail: Messages missing a valid address in From: 550 5.7.1 header, or having no From: header, are not accepted. I am not very familiar with php but I hope this is an easy fix. Thanks in Advance.


Solution

  • Are you positive that $_POST['mail'] is set? it could be that it is null or an empty string. I typically send the headers in as an associative array but the docs say a string is fine. Maybe toss a carriage return at the end of the line:

    $headers = "From: " . $mailFrom . "\r\n";