0

I followed this tutorial at CSS-Tricks, but clearly screwed something up.

I'm wondering if my problems are being caused by having my form and my PHP in separate files? Regardless...the PHP being used currently won't send me an email at ALL.

Here is the site currently live, with the code that won't send even send the email

Here is the code that sends the email, but isn't formatted:

<?php
/* Set e-mail recipient */

$to = 'myemail@example';
$from='teamemail@example.com';
$subject='Demo Submission Form';
$headers .='From: ' . strip_tags($_POST['email']) . '/r/n';
$headers .='Reply-To: ' . strip_tags($_POST['email']) . '/r/n';
$headers .='MIME-Version: 1.0/r/n';
$headers .='Content-type: text/html; charset=ISO-8859-1/r/n';


/* Check all form inputs using check_input function */
$name = check_input($_POST['name'], 'Please, enter your name');
$email = check_input($_POST['email'], 'Please enter your email address');
$number = check_input($_POST['phoneNumber']);
$address = check_input($_POST['address']);
$city = check_input($_POST['city']);
$state = check_input($_POST['state']);
$zip = check_input($_POST['zip']);
$contact= $_POST['radio'];
$request= $_POST['checkbox'];

if (isset($_POST['checkbox'])) {
$checkbox = $_POST['checkbox'];
// $service is an array of selected values
$checkbox_string = '';
for($i=0;$i<count($checkbox);$i++)
{
    if($i!=0)
    {
        $checkbox_string = $checkbox_string . ', ';
    }
    $checkbox_string = $checkbox_string . $checkbox[$i];
}$checkbox = join(',  ', $_REQUEST['checkbox']);
}

/* If e-mail is not valid show error message */
if (!preg_match('/([\w\-]+\@[\w\-]+\.[\w\-]+)/', $email))
{
show_error('E-mail address not valid');
}
/* Let's prepare the message for the e-mail */
$message = "

Name: $name
E-mail: $email
Client Type: $contact

Phone Number: $number

Address: $address
City: $city
State: $state
Zip Code: $zip

Request: $checkbox

";

/* Send the message using mail() function */

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

/* Redirect visitor to the thank you page */
header('Location: thanks.html');
exit();

I'm learning this as I go, so feel free to explain like I'm a 5-year-old idiot!

ETA: Code that isn't working:

<?php
/* Set e-mail recipient */

$to = 'myemail@example.com';
$from='testemail@example.com';
$subject='Demo Submission Form';
$headers .='From: ' . strip_tags($_POST['email']) . '/r/n';
$headers .='Reply-To: ' . strip_tags($_POST['email']) . '/r/n';
$headers .='MIME-Version: 1.0/r/n';
$headers .='Content-type: text/html; charset=ISO-8859-1/r/n';


/* Check all form inputs using check_input function */
$name = check_input($_POST['name'], 'Please, enter your name');
$email = check_input($_POST['email'], 'Please enter your email address');
$number = check_input($_POST['phoneNumber']);
$address = check_input($_POST['address']);
$city = check_input($_POST['city']);
$state = check_input($_POST['state']);
$zip = check_input($_POST['zip']);
$contact= $_POST['radio'];
$request= $_POST['checkbox'];

if (isset($_POST['checkbox'])) {
$checkbox = $_POST['checkbox'];
// $service is an array of selected values
$checkbox_string = '';
for($i=0;$i<count($checkbox);$i++)
{
    if($i!=0)
    {
        $checkbox_string = $checkbox_string . ', ';
    }
    $checkbox_string = $checkbox_string . $checkbox[$i];
}$checkbox = join(',  ', $_REQUEST['checkbox']);
}

/* If e-mail is not valid show error message */
if (!preg_match('/([\w\-]+\@[\w\-]+\.[\w\-]+)/', $email))
{
show_error('E-mail address not valid');
}
/* Let's prepare the message for the e-mail */
$message = '<html><body>';
$message .= '<img src="IMAGE"     alt="Ferris Demo Submission Request" />';
$message .= '<table rules="all" style="border-color: #666;" cellpadding="10">';
$message .= "<tr style='background: #eee;'><td><strong>Name:</strong> </td><td>" . ($_POST['name']) . "</td></tr>";
$message .= "<tr><td><strong>Email:</strong> </td><td>" . ($_POST['email']) . "</td></tr>";
$message .= "<tr><td><strong>Client Type:</strong> </td><td>" . ($_POST['contact']) . "</td></tr>";
$message .= "<tr><td><strong>Phone Number:</strong> </td><td>" . ($_POST['phoneNumber']) . "</td></tr>";
$message .= "<tr><td><strong>Address:</strong> </td><td>" . $_POST['address'] . "</td></tr>";
$message .= "<tr><td><strong>City:</strong> </td><td>" . $_POST['city'] . "</td></tr>";
$message .= "<tr><td><strong>State:</strong> </td><td>" . $_POST['state'] . "</td></tr>";
$message .= "<tr><td><strong>Zip Code:</strong> </td><td>" . $_POST['zip'] . "</td></tr>";
$message .= "<tr><td><strong>Request:</strong> </td><td>" . $_POST['checkbox'] . "</td>         </tr>";
$message .= "</table>";
$message .= "</body></html>"
;

/* Send the message using mail() function */

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

/* Redirect visitor to the thank you page */
header('Location: thanks.html');
exit();

/* Functions we used */
function check_input($data, $problem='')
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0)
{
show_error($problem);
}
return $data;
}

function show_error($myError)
{
?>
<html>
<body>

<p>Please correct the following error:</p>
<strong><?php echo $myError; ?></strong>
<p>Hit the back button and try again</p>

</body>
</html>
<?php
exit();
}
?>
crossbeats
  • 65
  • 1
  • 14
  • So.. you do or you don't get an email from this code? Put `'/r/n'` between double quotes like `"/r/n"` – putvande Jul 29 '14 at 14:32
  • The code I posted I do get an email; but the code with the HTML formatting (currently on the live site I liked to) doesn't send the email at all. – crossbeats Jul 29 '14 at 14:34
  • Sorry, I realize how confusing that whole thing is in hindsight! – crossbeats Jul 29 '14 at 14:34
  • So if the code you posted sends the email and the code you have on your website doesn't, why are you not posting the code that doesn't work? – putvande Jul 29 '14 at 14:37
  • Could you post the code that's not working instead? – jjjjjjjjjjjjjjjjjjjj Jul 29 '14 at 14:37
  • Firstly, remove the first dot for `$headers .='From: ' . strip_tags($_POST['email']) . '/r/n';` then change all `/r/n` to `\r\n` - I suggest you [**read the manual on `mail()`**](http://php.net/manual/en/function.mail.php) – Funk Forty Niner Jul 29 '14 at 14:42
  • Added code that isn't working – crossbeats Jul 29 '14 at 14:42
  • @Fred-ii- I have been reading it; can't find anything that matches the issue I'm having. – crossbeats Jul 29 '14 at 14:44
  • Did you not change all `/r/n` to `\r\n`? – Funk Forty Niner Jul 29 '14 at 14:45
  • The tutorial I followed, as linked in my original post, had it as "/r/n" I've tried it both ways, still not working. – crossbeats Jul 29 '14 at 14:46
  • If that tutorial tells you to use `/r/n` then this tells me that the rest of the code contains errors. I suggest you find something else. There's no sense in trying to salvage this. You will probably end up spending more time trying to debug this, then to find code that does work, and with similar injection-related functions. – Funk Forty Niner Jul 29 '14 at 14:49
  • OK, I just checked that link you included in your question and it does **not** contain `/r/n`, you're obviously changing the code there, and probably in other areas. Copy those codes to a **"T"** and change the ones to suit your needs. Good luck. – Funk Forty Niner Jul 29 '14 at 14:53
  • @Fred-ii- Fair enough. Kind of a bummer that the FIRST result for a Google search of formatting PHP emails has such a big error! – crossbeats Jul 29 '14 at 14:53
  • Keep searching and try out a few more. I always try out 5-10 examples as a minimum. If I feel that I can modify one in order to get it to work for me, then I'll spend time on that one, while working with a copy instead of originally working code. That way, you'll have something to revert to, should something go wrong. – Funk Forty Niner Jul 29 '14 at 14:56
  • As an added sidenote: I've spend up to 3 "days" on a few occasions, in trying to find a solution for code I am wanting to run the way I want it to (wink). Cheers – Funk Forty Niner Jul 29 '14 at 14:59

0 Answers0