# Solved: Form to email using PHPMailer



## DrDing (Dec 20, 2010)

Hello all!

I seem to be having the most daunting problem. What I need to do is to be able to create a form, sent it through SMTP using Gmail's mail server, and have it arrive in my mailbox with the information provided in the form.

Here's my form code:


```
<html>
<head>
<title>Contact Form</title>
</head>

<body>

<form method="post" action="http://www.danielle.byethost7.com/phpmailer/sendmail.php">

<p><label>Name:</label> <input type="text" name="name"/></p>
<p><label>Email:</label>  <input type="text" name="email"/></p>
<p><label>Comments:</label>
<br/>
<textarea name="comments" rows="8" cols="60">Add your comments here!</textarea></p>
<p><input type="submit" value="Submit Form"/>
</form>

</body>
</html>
```
As you can see, pretty standard simple stuff.

Here's the .php file it's suppose to run:


```
<?php

require_once("http://www.danielle.byethost7.com/phpmailer/class.phpmailer.php");

$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->Host = "smtp.gmail.com";

$mail->SMTPSecure = "ssl";
$mail->Port = 465;

$mail->Username = "[email protected]";
$mail->Password = "password";

$mail->AddAddress("[email protected]");
$mail->From = $_POST['email'];
$mail->FromName = $_POST['name'];
$mail->Subject = "User Comment";
$mail->IsHTML(true);
$mail->Body = $_POST['comments'].' name: '.$_POST['name'].'email'.$_POST['email'];

if(!$mail->Send()) {
  echo "Mailer Error: " . $mail->ErrorInfo;
} else {
  echo "Message sent! Thanks for your comments!";
}

?>
```
I can also paste what's going on in my class.phpmailer.php if need be, just let me know.

So here's the problem...when I fill in my little test form and hit the send button I get sent to a blank page, the address of which is listed as the "sendmail.php" file. Nothing happens after that. And the form information never arrives in my inbox at Gmail (I even checked my spam folder), so I have to assume nothing is actually being sent. 
Am I completely off on my php coding? Any and all help would be so greatly appreciated.

I'm running one Windows XP 
And my files are hosted on a free server at byethost.com
Again, let me know if you need any more info.


----------



## DrDing (Dec 20, 2010)

Don't know how but I somehow got it to magically work. I ended up putting my two files (my form.html and my processing.php) in the very top of my website directory (on my wamp server) and kept the class.phpmailer.php file in the phpmailer directory and just linked to it.

I'm guessing it was a directory problem? By that I mean I was directing to the file incorrectly. What's funny is I even tried this same set up at the free host I'm with and it worked as well, even with a longer directory string.

I also took the ! out of the 

```
if(!$mail->Send())
```
Here's my final codes for reference: 

my form code:

```
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Contact Form</title>
<meta name="Generator" content="Alleycode HTML Editor">
<meta name="Description" content="Your description here...">
<meta name="Keywords" content="Your keywords here...">
</head>

<body>

<form method="post" action="sendmail.php">

<p><label>Name:</label> <input type="text" name="name"/></p>
<p><label>Email:</label>  <input type="text" name="email"/></p>
<p><label>Comments:</label>
<br/>
<textarea name="comments" rows="8" cols="60">Add your comments here!</textarea></p>
<p><input type="submit" value="Submit Form"/></p>
</form>

</body>
</html>
```
my processing php code:

```
<?php

include_once("phpmailer/class.phpmailer.php");

$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->Host = "smtp.gmail.com";

$mail->SMTPSecure = "ssl";
$mail->Port = 465;

$mail->Username = "[email protected]";
$mail->Password = "xxxxxxx";

$mail->AddAddress("[email protected]");
$mail->From = "[email protected]";
$mail->FromName = $_POST['name'];
$mail->Subject = "User Comment";
$mail->IsHTML(true);
$mail->Body = $_POST['comments']. ' name: '. $_POST['name']. 'email: ' .$_POST['email']; 

if($mail->Send()) {
   echo "Message sent! Thanks for your comments!";
} 
?>
```


----------

