Email Form Help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mike2fast
Forum Newbie
Posts: 5
Joined: Thu Mar 31, 2011 12:03 pm

Email Form Help

Post by mike2fast »

Hi all,

I have been trying to create an email form that allows me to send an email to a hotmail, gmail address but everytime i do this it fails and i dont recieve the email. I am relativly new to PHP and dont really have much experience and was wondering if one of you good people could possibly create a simple form or point me in the direction of a good tutorial.

I would be extreamly greatful if someone could help me out with this as i am really struggling with this and need it to complete my website.

Thanks for your time and any help

Mike
mike2fast
Forum Newbie
Posts: 5
Joined: Thu Mar 31, 2011 12:03 pm

Re: Email Form Help

Post by mike2fast »

the code i currently have is below. Please could someone help me out with this.

Code: Select all

<html>
<body>

<?php
if (isset($_REQUEST['email']))
//if "email" is filled out, send email
  {
  //send email
  $email = $_REQUEST['email'] ;
  $subject = $_REQUEST['subject'] ;
  $message = $_REQUEST['message'] ;
  mail("mike2fast135@hotmail.com", "$subject",
  $message, "From:" . $email);
  echo "Thank you for using our mail form";
  }
else
//if "email" is not filled out, display the form
  {
  echo "<form method='post' action='mailform.php'>
  Email: <input name='email' type='text' /><br />
  Subject: <input name='subject' type='text' /><br />
  Message:<br />
  <textarea name='message' rows='15' cols='40'>
  </textarea><br />
  <input type='submit' />
  </form>";
  }
?>

</body>
</html> 
Thanks

Mike
danwguy
Forum Contributor
Posts: 256
Joined: Wed Nov 17, 2010 1:09 pm
Location: San Diego, CA

Re: Email Form Help

Post by danwguy »

start by taking the quotes off of your $subject variable, any reason you are using $_REQUEST instead of $_POST since you are posting data to the page? And I believe with hotmail and gmail, and it's good practice anyway, to use headers i.e.

Code: Select all

$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
then use mail($to, $subject, $message, $headers);
I would do this...

Code: Select all

<html>
<body>

<?php
if (isset($_POST['email']))
//if "email" is filled out, send email
  {
  //send email
  $email = $_POST['email'];
  $subject = $_POST['subject'];
  $message = $_POST['message'];
  $to = "mike2fast135@hotmail.com";
  $headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: ' .$email. '<'.$email.'>' . "\r\n";
$headers .= 'Reply-To: ' .$email. ' "\r\n";
$headers .= 'Return-Path: '.$email.'  "\r\n";  
mail($to, $subject, $message, $headers);
  echo "Thank you for using our mail form";
  }
else
//if "email" is not filled out, display the form
  {
  echo "<form method='post' action='mailform.php'>
  Email: <input name='email' type='text' /><br />
  Subject: <input name='subject' type='text' /><br />
  Message:<br />
  <textarea name='message' rows='15' cols='40'>
  </textarea><br />
  <input type='submit' />
  </form>";
  }
?>

</body>
</html> 
mike2fast
Forum Newbie
Posts: 5
Joined: Thu Mar 31, 2011 12:03 pm

Re: Email Form Help

Post by mike2fast »

Hi,

Thanks for your reply i really appreciate it. Unfortunatly when i upload this i get 500 internal server error :(

Any ideas why?
Post Reply