Jump to content

Problem sending email with php form


hatefulcrawdad

Recommended Posts

You wrote it so if $emailMessage is blank, it won't send. $emailMessage will always be "\n----MESSAGE-----\n" even if they entered a message or not. You'd just need to check the POST not the variable. Try this:

<?php
if (isset($_POST['Name'])){
$emailTo = '[email protected]';
$emailName = $_POST['Name'];
$emailFrom = $_POST['Email'];
$emailSubject = $_POST['Subject'];
$emailMessage = "\n-----MESSAGE-----\n" . $_POST['Message'];
$emailMessage .= "\n-----NAME-----\n" . $_POST['Name'];
$emailMessage .= "\n-----EMAIL-----\n" . $_POST['Email'];


if (!preg_match('/^([A-Z0-9\.\-_]+)@([A-Z0-9\.\-_]+)?([\.]{1})([A-Z]{2,6})$/i', $emailFrom) || empty($emailFrom)) {
echo 'Please enter a valid email address';
} elseif (empty($emailName)) {
echo 'Please enter your name';
} elseif (empty($emailSubject)) {
echo 'Please enter a subject';
} elseif (empty($_POST['Message'])) {
echo 'Please enter a message';
} else {
if (!empty($emailFrom)) {
$emailHeaders = 'FROM: <[email protected]>';
}
    
/* Send Email */
if (mail($emailTo, $emailSubject, $emailMessage, $emailHeaders)) {
echo 'You message has been sent!';
} else {
echo 'There was an internal error whilst sending your email.<br>';
echo 'Please try again later.';    
}
}
}
?>

 

A simple mistake to make.

You wrote it so if $emailMessage is blank, it won't send. $emailMessage will always be "\n----MESSAGE-----\n" even if they entered a message or not. You'd just need to check the POST not the variable. Try this

 

...code deleted for length...

 

A simple mistake to make.

 

OMG, thank you so much. What is with that guy trying to get headers defined when it already is? wierd. But anyway, you are a complete life saver. I am set with this contact form, not just gotta style the bitch.

No problem! I spoke from experience when I first made a contact form..

 

Since your problem is solved, hit the 'Topic Solved' button on the bottom left of this thread.. We try to get as many users as we can to use it .

 

EDIT: That guy doesn't know much about programming, Not sure why he babbles on like that. He's pretty good with some things though..

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.