Problem with mail function.

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
Crux
Forum Newbie
Posts: 7
Joined: Sat Jan 08, 2011 7:01 am

Problem with mail function.

Post by Crux »

This is the first time I am trying to code a website in PHP and I am having some issues. As a development platform I use XAMPP. I wrote a script that is included in the contact page of the website. It is not completely finished yet. But the parts that are I can not seem to get to work. It will not send the mail and the text it should echo is not being echoed when the mail is send.

Do not know i it is important but the form calls itself.

Code: Select all

<?php
if(isset($_POST['Submit']))
{
// Set my email
$mail_to = '*******@*****.***';

// Collect the information and store it in variables
$name = $_POST["fname"];
$email = $_POST["femail"];
$mail = $_POST["fmail"];

// Build a variable that contains the sender
$sender = $name . ' ' . $email;

// Wordwrap in case the mail is larger then 70 characters
$mail = wordwrap($mail, 70);

//Send the mail
mail($mail_to, $sender, $mail);

echo("send");

}
else
{
echo("
<div id=\"contact_form\">
	<form action=\"contact.php\" method=\"post\">
		Name: <input type=\"text\" name=\"fname\"  />
		Email: <input type=\"text\" name=\"femail\" />
		What you have to say:<textarea name=\"fmail\"></textarea>
		<input type=\"submit\" value=\"submit\" />
	</form>
</div>
");
}
?>
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Problem with mail function.

Post by califdon »

Don't use parentheses () in an echo command. Also, using escaped double quotes like that makes it quite difficult to read your code. Just use single quotes, like this:

Code: Select all

echo "<div id='contact_form'>
        <form action='contact.php' method='post'>
                Name: <input type='text' name='fname'  />
                Email: <input type='text' name='femail' />
                What you have to say:<textarea name='fmail'></textarea>
                <input type='submit' value='submit' />
        </form>
</div>";
or, since you aren't using any variables in that block, you could simply do it like this:

Code: Select all

?>
<div id='contact_form'>
    <form action='contact.php' method='post'>
        Name: <input type='text' name='fname'  />
        Email: <input type='text' name='femail' />
        What you have to say:<textarea name='fmail'></textarea>
        <input type='submit' value='submit' />
     </form>
</div>
<?php
Crux
Forum Newbie
Posts: 7
Joined: Sat Jan 08, 2011 7:01 am

Re: Problem with mail function.

Post by Crux »

That did not solve it.

It still does not echo "send" after I hit the submit button. And I am not receiving no email in my inbox. But I guess that has more to do with that I got XAMPP installed on my PC and no mail server.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Problem with mail function.

Post by califdon »

Yes, and you also aren't giving the mail() function the appropriate arguments. See http://php.net/manual/en/function.mail.php.

First of all, do you know that PHP is working? Will it run other PHP scripts? Did you correct the parentheses in your echo "send"; line? I don't see anything else wrong, although I haven't tried to run it.
Crux
Forum Newbie
Posts: 7
Joined: Sat Jan 08, 2011 7:01 am

Re: Problem with mail function.

Post by Crux »

From what I see in example one on the link you gave me I am giving the right info into the mail function. And I am sure PHP is working. Other PHP scripts that I wrote work perfectly.

I changed the code how you told me to. Maybe I misunderstood you so I will post the edited code.

Code: Select all

<!-- Here code will be inserted that will see if the user is logged in. If so it will auto fill in the name of the user. 
Otherwise it will be empty. -->
<?php
if(isset($_POST['Submit']))
{
// Set my email
$mail_to = '******@****.com';

// Collect the information and store it in variables
$name = $_POST["fname"];
$email = $_POST["femail"];
$mail = $_POST["fmail"];

// Build a variable that contains the sender
$sender = $name . ' ' . $email;

// Wordwrap in case the mail is larger then 70 characters
$mail = wordwrap($mail, 70);

//Send the mail
mail($mail_to, $sender, $mail);

echo "send";

}
else
{
echo
"<div id='contact_form'>
	<form action='contact.php' method='post'>
		Name: <input type='text' name='fname'  />
		Email: <input type='text' name='femail' />
		What you have to say:<textarea name='fmail'></textarea>
		<input type='submit' value='submit' />
	</form>
</div>"
;
}
?>
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Problem with mail function.

Post by califdon »

OK, I didn't understand that you wanted the name and email as the subject. So I spent a few minutes to actually run your code on my server to determine if the form values are being received correctly, which they are. So what's happening is this: your mail() function isn't working, as you suspected, because you don't have a mail server configured. Since the mail() function error is a fatal error, it never reaches the line to echo "send". You could do something like this:

Code: Select all

<html>
<head><title>test</title></head>
<body>
<?php
if(isset($_POST['fname'])) {
// Set my email
   $mail_to = '******@****.com';

// Collect the information and store it in variables
   $name = $_POST["fname"];
   $email = $_POST["femail"];
   $mail = $_POST["fmail"];

// Build a variable that contains the sender
   $sender = $name . ' ' . $email;

// Wordwrap in case the mail is larger then 70 characters
   $mail = wordwrap($mail, 70);
//Send the mail
//   echo "<br>$mail_to, $sender, $mail";
   if(mail($mail_to, $sender, $mail)) {
      echo "Sent";
   } else {
      echo "Mail failed";
   }
} else {
   echo "<div id='contact_form'>
         <form action='contact.php' method='post'>
            Name: <input type='text' name='fname'  />
            Email: <input type='text' name='femail' />
            What you have to say:<textarea name='fmail'></textarea>
            <input type='submit' value='submit' />
         </form>
         </div>";
}
?>
</body>
</html>>
I have made several changes, including adding HTML, which is generally a good thing to do, although it will send output to the browser even if you don't include it (but you have little control of the page without it). The main thing I did was change the simple mail() function to test whether it succeeded or failed (such functions invariably return a False if they fail) and either report "sent" or "Mail failed".
Post Reply