PDA

View Full Version : PHP Email Form Sending Blank Emails


EricM3
12-02-2008, 05:22 AM
Hey guys.

I got the code for a PHP email form from tutvid.com.

Anyway, I have uploaded my files, but whenever I send the email, the information in the form does not show up. I.e. The email says:

Name:
Email:
Phone:
Comments:

Here is my PHP code for the form:

<?php
/*Subject and Email Variables*/
$emailSubject = 'Page Set Contact';
$webMaster = 'my email address' ('ericmorgan@pageset.net');

/*Gathering Data Variables*/
$emailField = $_POST['email'];
$nameField = $_POST['name'];
$phoneField = $_POST['phone'];
$commentsField = $_POST['comments'];

$body = <<<EOD
<br><hr><br>
Email: $email <br>
Name: $name <br>
Phone: $phone <br>
Comments: $comments <br>
EOD;
$headers = "From: $email\r\n";
$headers .= "Content-type: text/html\r\n";
$success = mail($webMaster, $emailSubject, $body, $headers);

/*Results Rendered as HTML*/
$theResults = <<<EOD
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Contact Submission</title>
</head>
<body>
<p>Thank you for your interest. We will get back to you as soon as possible.</p>
<p><a href="<A href="http://">Back</a></p">homepage">Back</a></p>
</body>
</html>
EOD;
echo "$theResults";

?>

And the code for my HTML form page:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>



<body>
<form id="form1" name="form1" method="post" action="contactformprocess.php">
<p>
<label></label>
<input type="text" name="email" id="email" />
</p>
<p>
<label>
<input type="text" name="name" id="name" />
</label>
</p>
<p>
<label>
<input type="text" name="phone" id="phone" />
</label>
</p>
<p>
<label>
<textarea name="comments" id="comments" cols="45" rows="5"></textarea>
</label>
</p>
<p>&nbsp;</p>
<p>
<label>
<input type="submit" name="submit" id="submit" value="Submit" />
</label>
</p>
</form>
</body>
</html>



Any ideas?

edbr
12-02-2008, 05:40 AM
try replacing
$body = <<<EOD
<br><hr><br>
Email: $email <br>
Name: $name <br>
Phone: $phone <br>
Comments: $comments <br>
EOD;
with
$body = "
Email: $email \n
Name: $name \n
Phone: $phone \n
Comments: $comments \n
";

EricM3
12-02-2008, 10:35 PM
I tried what you suggested, edbr, but I am still getting the same results. I wish I knew a little more about PHP.

edbr
12-03-2008, 02:05 AM
dont we all , check out davidj 's video tuts on this site.
on your contactformprocess.php page add this
<?php echo $emailField ;
echo $nameField;
echo$phoneField;
echo $commentsField;
?>
this will test if the values are being passed .
a point on thismail handler , is it validated? if not you will be vunerable to spammers . There is a mail handler on this site for use btw

shelleymathers
08-18-2009, 07:52 PM
I have created a contact form in DW and have also set up a PHP page according to the article on this site called "Using Dreamweaver and PHP to send form results in email". I have used the following code on my php page:

<?php
//--------------------------Set these paramaters--------------------------

// Subject of email sent to you.
$subject = 'Results from Contact form';

// Your email address. This is where the form information will be sent.
$emailadd = 'mail@rdsnetworks.net';

// Where to redirect after form is processed.
$url = 'http://www.rdsnetworks.net/confirmation.html';

// Makes all fields required. If set to '1' no field can not be empty. If set to '0' any or all fields can be empty.
$req = '0';

// --------------------------Do not edit below this line--------------------------
$text = "Results from form:\n\n";
$space = ' ';
$line = '
';
foreach ($_POST as $key => $value)
{
if ($req == '1')
{
if ($value == '')
{echo "$key is empty";die;}
}
$j = strlen($key);
if ($j >= 20)
{echo "Name of form element $key cannot be longer than 20 characters";die;}
$j = 20 - $j;
for ($i = 1; $i <= $j; $i++)
{$space .= ' ';}
$value = str_replace('\n', "$line", $value);
$conc = "{$key}:$space{$value}$line";
$text .= $conc;
$space = ' ';
}
mail($emailadd, $subject, $text, 'From: '.$emailadd.'');
echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">';
?>
It seems like this is only partially working - it IS redirecting to the correct page after I hit "submit", but it IS NOT sending me an email with the information. How can I fix this?

bee80
08-18-2009, 09:45 PM
shouldnt it be :


<?php
/*Subject and Email Variables*/
$emailSubject = 'Page Set Contact';
$webMaster = 'my email address';

/*Gathering Data Variables*/
$emailField = $_POST['email'];
$nameField = $_POST['name'];
$phoneField = $_POST['phone'];
$commentsField = $_POST['comments'];

$body = <<<EOD
<br><hr><br>
Email: $emailField <br>
Name: $nameField <br>
Phone: $phoneField <br>
Comments: $commentsField <br>
EOD;
$headers = "From: $emailField\r\n";
$headers .= "Content-type: text/html\r\n";
$success = mail($webMaster, $emailSubject, $body, $headers);

/*Results Rendered as HTML*/
$theResults = <<<EOD
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Contact Submission</title>
</head>
<body>
<p>Thank you for your interest. We will get back to you as soon as possible.</p>
<p><a href="<A href="http://">Back</a></p">homepage">Back</a></p>
</body>
</html>
EOD;
echo "$theResults";

?>


i think you missed the Field part at the end of all the variables? or am i missing something?

bee80
08-18-2009, 10:21 PM
sorry forgot to add edbr's bit


$body = <<<EOD
Email: $emailField \n
Name: $nameField \n
Phone: $phoneField \n
Comments: $commentsField \n
EOD;


<br> tags are for browsers /n lines work in text documents :)

gigiloumill
08-20-2009, 06:55 PM
If that doesn't work. try it in this format:

Body = "Name : " .$name . "\n" . "Email : " .$email . "\n" .

Do the same for everything else.

gigiloumill
08-20-2009, 07:05 PM
If your server requires SMTP auth, the script you're using above might not work, unless, you change stuff around. Here's one that works.

<?php
require("class.phpmailer.php");
// Where to redirect after form is processed.
$url = 'http://www.yoursite.com/yourpage.php';

$to = "your_email.com";

$name = $_REQUEST["name"];
$lname = $_REQUEST["lname"];
$email = $_REQUEST["email"];
$message = $_REQUEST["message"];

function is_valid_email($email) {
return preg_match('#^[a-z0-9.!\#$%&\'*+-/=?^_`{|}~]+@([0-9.]+|([^\s]+\.+[a-z]{2,6}))$#si', $email);
}
i

if (!is_valid_email($email)) {
echo 'Sorry, invalid email. Please go back and imput correct e-mail';
exit;
}

$mail = new PHPMailer();$mail = new PHPMailer();
$mail->CharSet ="utf-8"; // You can adjust the Charset according to your language
$mail->IsSMTP();
$mail->Host = "mail.yourdomain.com";
$mail->From="your_email.com"; //REMEMBER, this MUST be same as your authorization email address above.
$mail->FromName="My site's mailer";
$mail->SMTPAuth = true;
$mail->Username = "your_email.com";
$mail->Password = "your_password";

$mail->AddAddress($to);
$mail->Subject = "your_Contact Form";
$mail->Body = "Name : " .$name . "\n" . "Last Name : " . $lname . "\n" . "email : " . $email ."\n" . "Message : " . $message;

if(!$mail->Send())
{
echo "Error sending: " . $mail->ErrorInfo;
}
else
{
echo "Thanks for submitting.";
echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">';
}


?>


Also, in order for complete script to work, you need to download phpmailer.php from here:

http://www.webhost4life.com/HostingKB/KnowledgebaseArticle50140.aspx

Good luck

shelleymathers
08-24-2009, 10:08 PM
Thanks for all of your suggestions! I'm more than a little lost in all of that code! I have no knowledge of this whatsoever, so I'm trying to figure this out the best that I can. :confused: I'm still going through these ideas and trying to get them to work. So far I haven't gotten it to work and now I'm not being taking to the correct confirmation page, it's taking me to the "blank" php page. I've tried both bee80 and gigiloumill's codes and they've neither one worked for me. I'm sure that I must be doing something wrong. I'll let you know how it goes............

gigiloumill
08-24-2009, 11:42 PM
Post your code, please..

shelleymathers
08-25-2009, 07:19 PM
This is the code I started out with. It is taken directly from the article on this site called "Using Dreamweaver and PHP to send form results in email". This came closer to working than anything else I've tried (It did take the user to the correct confirmation page after they hit "submit", I just never got an email with the info.)


<?php
//--------------------------Set these paramaters--------------------------

// Subject of email sent to you.
$subject = 'Results from Contact form';

// Your email address. This is where the form information will be sent.
$emailadd = 'mail@rdsnetworks.net';

// Where to redirect after form is processed.
$url = 'http://www.rdsnetworks.net/confirmation.html';

// Makes all fields required. If set to '1' no field can not be empty. If set to '0' any or all fields can be empty.
$req = '0';

// --------------------------Do not edit below this line--------------------------
$text = "Results from form:\n\n";
$space = ' ';
$line = '
';
foreach ($_POST as $key => $value)
{
if ($req == '1')
{
if ($value == '')
{echo "$key is empty";die;}
}
$j = strlen($key);
if ($j >= 20)
{echo "Name of form element $key cannot be longer than 20 characters";die;}
$j = 20 - $j;
for ($i = 1; $i <= $j; $i++)
{$space .= ' ';}
$value = str_replace('\n', "$line", $value);
$conc = "{$key}:$space{$value}$line";
$text .= $conc;
$space = ' ';
}
mail($emailadd, $subject, $text, 'From: '.$emailadd.'');
echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">';
?>

gigiloumill
08-28-2009, 01:23 AM
O.K. before we get into this, please find out if your server requires SMTP auth. If not, here's a working finish copy..

<?php
//--------------------------Set these paramaters--------------------------

// Subject of email sent to you.
$subject = 'Results from Contact form';

// Your email address. This is where the form information will be sent.
$emailadd = 'you@your.com';

// Where to redirect after form is processed.
$url = 'your web address.com';

// Makes all fields required. If set to '1' no field can not be empty. If set to '0' any or all fields can be empty.
$req = '0';

// --------------------------Do not edit below this line--------------------------
$text = "Results from form:\n\n";
$space = ' ';
$line = '
';
foreach ($_POST as $key => $value)
{
if ($req == '1')
{
if ($value == '')
{echo "$key is empty";die;}
}
$j = strlen($key);
if ($j >= 20)
{echo "Name of form element $key cannot be longer than 20 characters";die;}
$j = 20 - $j;
for ($i = 1; $i <= $j; $i++)
{$space .= ' ';}
$value = str_replace('\n', "$line", $value);
$conc = "{$key}:$space{$value}$line";
$text .= $conc;
$space = ' ';
}
mail($emailadd, $subject, $text, 'From: '.$emailadd.'');
echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">';
?>

shelleymathers
08-28-2009, 01:48 AM
I'm sorry, but this is a dumb question.............how do I check to see if it does require SMTP authorization? I checked in the email set up in my Outlook and it appears to me that it does require it. Is that what I'm looking for?

gigiloumill
08-28-2009, 01:05 PM
Outlook has noting to do with it. Contact your server provider. Some would have it in a Q & A page. If you have cpanel you might not need it.

gigiloumill
08-28-2009, 01:06 PM
Can you post your form code? (not the sending script but the form used to collect info.) Thanks.

shelleymathers
08-28-2009, 04:23 PM
Hi gigiloumill,
I appreciate your help so much! Maybe I'm beyond help - I'm just learning this stuff as I go........

What do you mean by my server provider? We use godaddy to host our website. Is that who I should contact?

I wasn't aware that I would need a form to collect the info. Where will this form go? I have the sending script uploaded to my website as a php page. Will I need to do the same with this new form? Can you give me a sample of what this should look like?

gigiloumill
08-28-2009, 04:39 PM
1. contact godaddy.com and ask them if a SMTP authorization in required for the sending script. (if not, then the script posted here will work.
2. Create a contact form. see here. http://car4students.com/contact.php
3. On "Contact" page include the sending script page on 'Action"
4. upload to server and check.