PDA

View Full Version : Make an e-mail form using PHP


filburt1
October 30 '02, 10:33 PM
If you're like me then you never want to give out your real e-mail address. As a result you'll never (literally) get spam.

Here's a form similar to the one I use (click Contact at the top of the page to see it in action):


<?
require('header.php');

if (isset($_POST['from']))
{
$ip = $_SERVER['REMOTE_ADDR'];
$resolved = gethostbyaddr($ip);
if ($resolved != $ip)
{
$footer = "This e-mail was sent by a user with the IP address $ip, which resolved to '$resolved'.";
}
else
{
$footer = "This e-mail was sent by a user with the IP address $ip, which could not be resolved to a hostname.";
}
$message = $_POST['message'];
$message = "$message\n\n------------\n$footer";

if (mail('YOUR E-MAIL ADDRESS', $subject, $message, "From: $from\r\nReply-To: $from\r\n"))
{
echo 'Your mail was successfully sent.';
}
else
{
echo 'There was an error while sending the mail.';
}
}
else
{
?><form method="post" action="<? echo $_SERVER['PHP_SELF']; ?>">
<p>Please be aware that your IP address (<? echo $_SERVER['REMOTE_ADDR'] ?>) will be sent along with the message
to prevent against abuse of this mail form.</p>
<table border="0" cellspacing="0" cellpadding="3">
<tr>
<td><b><font face="Verdana, Arial, Helvetica, sans-serif" size="2">Your
e-mail address:</font></b><i><font face="Trebuchet MS, Verdana, Tahoma, Arial, sans-serif" size="1"><br>
<input type="text" name="from" size="60">
</font></i></td>
</tr>
<tr>
<td><b><font face="Verdana, Arial, Helvetica, sans-serif" size="2">Subject:</font></b>
<i><font face="Trebuchet MS, Verdana, Tahoma, Arial, sans-serif" size="1"><br>
<input type="text" name="subject" size="60">
</font></i></td>
</tr>
<tr>
<td><b><font face="Verdana, Arial, Helvetica, sans-serif" size="2"><a href="/forums/">Forums</a>
Username:</font></b><br>
<i><font face="Trebuchet MS, Verdana, Tahoma, Arial, sans-serif" size="1">Leave
this field blank if you are not registered.<br>
<input type="text" name="forumsusername" size="60">
</font></i></td>
</tr>
<tr>
<td><font face="Verdana, Arial, Helvetica, sans-serif" size="2"><b>Message:<br>
<textarea name="message" rows="10" cols="60"></textarea>
</b></font></td>
</tr>
<tr>
<td align="right"> <font face="Verdana, Arial, Helvetica, sans-serif" size="2">Click
&quot;Send&quot; only once.</font><br>
<input type="submit" value="Send">
</td>
</tr>
</table>
</form>
<?
}

require('footer.php');
?>


If you are not using a templated site or don't know what one is, remove the lines require('header.php') and require('footer.php') and just put this entire chunk of code where you want the form to appear in your page.

When the user sends an e-mail to you, they never see your e-mail address. You see something like:


Message here
----------
This e-mail was sent by a user with the IP address 12.34.56.78, which resolved to 'hostname.isp.com'

HanZnSolo
November 3 '02, 12:59 PM
Hello all!

Filburt was kind enough to put up the PHP tutorial for me to be able to make an e-mail form for my site that wouldn't prompt the user before sending the information, for which I'm VERY grateful!

Since I have absolutely NO experience with PHP, I'm having a little problem with the form. I've pretty much tried to hack the code out to get it customized for my form, but when I try to actually click 'Send', I get the following error:

Warning: mail() expects at most 5 parameters, 6 given in /home/yuyusist/public_html/agentform.php on line 30
There was an error while sending the mail.

How can I get that fixed so that it'll accept all the variables I have there on that line?

Thanks! :confused:

HanZnSolo
November 3 '02, 01:24 PM
Oops....

Here's the URL to it....forgot about that!

http://www.yuyusisterhood.com/agentform.php

filburt1
November 3 '02, 01:31 PM
Hmm...copy and paste the line you put in your script here that looks similar to this one: if (mail('YOUR E-MAIL ADDRESS', $subject, $message, "From: $from\r\nReply-To: $from\r\n")) (i.e., post what your line looks like)

HanZnSolo
November 3 '02, 01:49 PM
Here it is:

if (mail('Your name', $from, $company, $phone, $charity, "From: $mail\r\nReply-To: $mail\r\n"))

I realize I've got 5 different variables in there, but is that all necessary for the mail go to through?

Thanks again!

filburt1
November 3 '02, 02:39 PM
That would explain it.

Try:

if (mail('your (hanznsolo) e-mail address', $subject,
"$message\n\nCompany: $company\n$Phone: $phone\nCharity: $charity",
"From: $mail\r\nReply-To: $mail\r\n"))

HanZnSolo
November 3 '02, 03:09 PM
Okay, I'm gonna try that now...but what about the (hanznsolo) part? That name has nothing to do with the actual site, so can that be omitted from the line?

HanZnSolo
November 3 '02, 03:15 PM
Well, I tried it. Now, I'm getting the less specific 'there was an error in generating the mail' message....now what?

filburt1
November 3 '02, 03:29 PM
Replace your (hanznsolo) e-mail address with your actual e-mail address.

HanZnSolo
November 3 '02, 03:41 PM
Cool, thanks! As soon as the stupid server comes back up (it just went down), I'll check it out.

Now, if I can just get this STUPID menu centered....

http://www.yuyusisterhood.com/menu2.html

Do you happen to have any ideas on that one?

filburt1
November 3 '02, 03:44 PM
The server's down, I can't view it :p

HanZnSolo
November 3 '02, 03:46 PM
yeah, yeah, yeah!

shaddup!!! :p

HanZnSolo
November 3 '02, 03:50 PM
Okay, it's back up now! heheheh...

HanZnSolo
November 3 '02, 04:05 PM
That form's working PERFECTLY now. Man, thanks a lot for the help with that one! Y'know, the syntax of PHP doesn't look all that difficult to learn. Is it pretty easy?

HanZnSolo
November 3 '02, 04:30 PM
Actually, nevermind the menu thing...I've just been told by a friend who uses a Mac that it looks like crap on a Mac, in both IE and in Netscape and Opera....SO, I'm not even gonna go there, I guess. He's doing some graphic button stuff for me, so hopefully that'll look good.

Thanks anyway!

filburt1
November 3 '02, 04:37 PM
PHP is reasonably easy to learn and a very forgiving language...

HanZnSolo
November 3 '02, 04:41 PM
Cool...Well, I'm off now to work on other parts of the site...*sigh* This project is getting tiring. I guess if I didn't already work 50 hours a week, it wouldn't be so bad. I don't know how you do it, man, having a regular job and working on this thing all the time...

filburt1
November 3 '02, 04:45 PM
Bah, Jelsoft already wrote vBulletin for me so it's pretty easy now ;)

HanZnSolo
November 3 '02, 04:49 PM
Still...whew!

JR
April 26 '03, 06:59 PM
i need some help, i have a contact form where the user puts their e-mail in, but i want to use the php to make sure that it is a valid e-mail address. I am not sure how this is done but i assume it is by making sure the @ and the . is in there.

If i made php i would have it like this - if (email = *@*.*)etc. LOL :p

JR
April 26 '03, 07:39 PM
also can i store the code in whatever.php file and have something like this <form name="whateverform" method="post" action="whatever.php">

krazy
June 19 '03, 05:08 AM
ur code dont work filburt...?? i tried. when i click the send button, it goes to the index page. when i check my email, theres nothing.

iKwak
June 29 '03, 02:39 PM
Your Contact page is down. :beard:

splufdaddy
June 29 '03, 02:53 PM
Even if Filburt's contact page was up, the contact page specifically states to only contact him if he has told you to do so. Questions from WDF should be posted right here, so others can read up and learn, and so the same question doesn't get answered 100 times in private. If you must contact Filburt privately, PM him.

iKwak
July 2 '03, 12:18 PM
How can I add some background colors onto this php ?

Mr.djc
July 2 '03, 12:45 PM
This is the error that i get, doesent this mean i need a
$subject =(" ")
and emm $from =

or am i totally wrong?

please help! :D

Notice: Undefined variable: subject in C:\Program Files\Apache Group\Apache2\htdocs\Trial\Contact3.php on line 18

Notice: Undefined variable: from in C:\Program Files\Apache Group\Apache2\htdocs\Trial\Contact3.php on line 18

Notice: Undefined variable: from in C:\Program Files\Apache Group\Apache2\htdocs\Trial\Contact3.php on line 18

filburt1
July 2 '03, 01:22 PM
You can safely ignore the notices. If you want, try putting this at the top of the page (after the <?):

error_reporting(E_ALL and ~E_NOTICE);

ajaspers
July 2 '03, 01:51 PM
Wouldn't that be error_reporting(E_ALL & ~E_NOTICE)?

filburt1
July 2 '03, 02:01 PM
Yes, whoops.

iKwak
July 2 '03, 03:28 PM
Originally posted by CPInteract
How can I add some background colors onto this php ?


:cry:

Mr.djc
July 2 '03, 04:22 PM
okay, thank''s for the quick reply!
Now i have the other error ;)
Warning: mail() expects at most 5 parameters, 7 given in C:\Program Files\Apache Group\Apache2\htdocs\Trial\Contact2.php on line 42
There was an error while sending the mail.
(yes i have tried what you said earlier, but it still dident do what i wanted it to)

My mail line is that.
if (mail('Mr.djc@zonnet.nl', $subject, $message, $Product, $Price, $Link, "From: $from\r\nReply-To: $from\r\n"))

AND i have added something else
$message = $_POST['message'];
$product = $_POST['product'];
$Price = $_POST['price'];
$Link = $_POST['link'];
$From = $_POST['from'];

i have been busy changing stuff for 30 minutes now, but still havent got it right....

Eighter it just sends the message, and the IP thing... or it sends the message and the ip thing with a dodgey comment under it giving half of one of the commands.

Please help me! :D

AmSkateboarder
July 2 '03, 11:17 PM
What if you want to make a form to allow users to upload a file to your email? How would you do that?

krazy
August 3 '03, 09:17 AM
i think that would require a different script :)

justlivyalife
August 3 '03, 09:36 AM
I have a question - The rest of my site is currently in html code. can I just make my contact page have a .php ending and insert this code where it is requried, or do I need to edit anything else??

<?
require('header.php');

if (isset($_POST['from']))
{
$ip = $_SERVER['REMOTE_ADDR'];
$resolved = gethostbyaddr($ip);
if ($resolved != $ip)
{
$footer = "This e-mail was sent by a user with the IP address $ip, which resolved to '$resolved'.";
}
else
{
$footer = "This e-mail was sent by a user with the IP address $ip, which could not be resolved to a hostname.";
}
$message = $_POST['message'];
$message = "$message\n\n------------\n$footer";

if (mail('YOUR E-MAIL ADDRESS', $subject, $message, "From: $from\r\nReply-To: $from\r\n"))
{
echo 'Your mail was successfully sent.';
}
else
{
echo 'There was an error while sending the mail.';
}
}
else
{
?><form method="post" action="<? echo $_SERVER['PHP_SELF']; ?>">
<p>Please be aware that your IP address (<? echo $_SERVER['REMOTE_ADDR'] ?> ) will be sent along with the message
to prevent against abuse of this mail form.</p>
<table border="0" cellspacing="0" cellpadding="3">
<tr>
<td><b><font face="Verdana, Arial, Helvetica, sans-serif" size="2">Your
e-mail address:</font></b><i><font face="Trebuchet MS, Verdana, Tahoma, Arial, sans-serif" size="1"><br>
<input type="text" name="from" size="60">
</font></i></td>
</tr>
<tr>
<td><b><font face="Verdana, Arial, Helvetica, sans-serif" size="2">Subject:</font></b>
<i><font face="Trebuchet MS, Verdana, Tahoma, Arial, sans-serif" size="1"><br>
<input type="text" name="subject" size="60">
</font></i></td>
</tr>
<tr>
<td><b><font face="Verdana, Arial, Helvetica, sans-serif" size="2"><a href="/forums/">Forums</a>
Username:</font></b><br>
<i><font face="Trebuchet MS, Verdana, Tahoma, Arial, sans-serif" size="1">Leave
this field blank if you are not registered.<br>
<input type="text" name="forumsusername" size="60">
</font></i></td>
</tr>
<tr>
<td><font face="Verdana, Arial, Helvetica, sans-serif" size="2"><b>Message:<br>
<textarea name="message" rows="10" cols="60"></textarea>
</b></font></td>
</tr>
<tr>
<td align="right"> <font face="Verdana, Arial, Helvetica, sans-serif" size="2">Click
&quot;Send&quot; only once.</font><br>
<input type="submit" value="Send">
</td>
</tr>
</table>
</form>
<?
}

require('footer.php');
?>

splufdaddy
August 3 '03, 03:58 PM
If your server is php enabled, put everything between the
<?
...
?>

in your contact page and give it a php extension (contact.php). You'll also want to take out require('header.php'); and require('footer.php'); because you aren't using SSI (Server Side Includes).

Meringue
August 13 '03, 07:54 AM
I've managed to put up my own contact page, mainly derived from Filburt's excellent PHP script at the top of the page. However, I had a couple of problems with his script - the reply-to and from fields didn't seem to fill in correctly (not that that's a massive problem, but it's there.) Also, I wanted to have email validation of some sort so, for example, just clicking "send" on its own wouldn't fire off a blank email.

I used the email validation code from here:
http://www.phpfreaks.com/quickcode/function_to_check_for_valid_email_address/83.php

to create the following modification of the PHP email form. I hope this helps someone out there.

You can view the completed contact form at:

http://www.shahidhussain.com/contact/



<?

// If the from variable has been set, display the afterwards message and post the email.
if (isset($_POST['from']))

{

$ip = $_SERVER['REMOTE_ADDR'];

$resolved = gethostbyaddr($ip);

if ($resolved != $ip)
{
$footer = "Sent from IP address $ip - resolves to '$resolved'";
}
else
{
$footer = "Sent from IP address $ip - could not resolve";
}

$message = $_POST['message'];

$message = "$message\n\n$footer";


if(ereg("^.+@.+\\..+$", $from))
{
if (mail('email@yourdomain.com', $subject, $message,
"From: $from\r\n"."Reply-To: $from\r\n"))
{
echo 'Your mail was successfully sent.';
}
else
{
echo 'There was an error while sending the mail.';
}
}
else
{
echo 'It looks like you didnt enter a valid email address. Try again.';
}


}


else
// If the from variable has not been set, put the form up to set the variables.

{


?><form method="post" action="<? echo $_SERVER['PHP_SELF']; ?>">

<table border="0" cellspacing="0" cellpadding="3">

<tr>
<td>If you want to get in touch, just fill in the following form and
hit "Send".
</td>
</tr>

<tr>
<td></td>
</tr>

<tr>

<td><b>Your email address:</b><br>

<input type="text" name="from" size="60">

</td>

</tr>

<tr>

<td><b>Subject:</b>

<br>

<input type="text" name="subject" size="60">

</td>

</tr>


<tr>

<td><b>Plop in your message here:<br>

<textarea name="message" rows="10" cols="60"></textarea>

</b></td>

</tr>

<tr>

<td align="right">Just click Send once, it might take a little while to respond.<br>

<input type="submit" value="Send">

</td>

</tr>

</table>

</form>

<?

}


?>

justlivyalife
June 14 '04, 05:04 PM
I know this thread is ages old, but I've tried loads of different forms and couldn't get them to work, so I resorted to this option. I have posted below my entire PHP code. I know it is long, but I couldn't figure out what the error was. <?

if (isset($_POST['from']))
{
$ip = $_SERVER['REMOTE_ADDR'];
$resolved = gethostbyaddr($ip);
if ($resolved != $ip)
{
$footer = "This e-mail was sent by a user with the IP address $ip, which resolved to '$resolved'.";
}
else
{
$footer = "This e-mail was sent by a user with the IP address $ip, which could not be resolved to a hostname.";
}
$message = $_POST['message'];
$message = "$message\n\n------------\n$footer";

if (mail('email@email.com', $subject, $message, "From: $from\r\nReply-To: $from\r\n"))
{
echo 'Your mail was successfully sent.';
}
else
{
echo 'There was an error while sending the mail.';
}
}
else
{
?><form method="post" action="<? echo $_SERVER['PHP_SELF']; ?>">
<h1>Contact RN Design</h1>
<p>You can contact RN Design by emailing us directly,or you can fill in the
contact form below which will be sent to us directly. We aim to respond to
all emails within 48 hours of the time of sending, though this is not always
possible. Please ensure that you give an accurate Subject to your message
when emailing us.</p>
<p>Please be aware that your IP address (<? echo $_SERVER['REMOTE_ADDR'] ?>) will be sent along with the message
to prevent against abuse of this mail form.</p>
Your name:<br /><input name="name" type="text" id="name" class="form" onfocus="this.className='form-on'" onblur="this.className='form'" style="width: 380px;" /><br /><br />
Your email:<br /><input name="from" type="text" id="from" class="form" onfocus="this.className='form-on'" onblur="this.className='form'" style="width: 380px;" /><br /><br />
Subject:<br /><input name="subject" type="text" id="subject" class="form" onfocus="this.className='form-on'" onblur="this.className='form'" style="width: 380px;" /><br /><br />
Message:<br /><textarea name="message" cols="40" rows="6" id="message" class="form" onfocus="this.className='form-on'" onblur="this.className='form'" style="font-family: trebuchet ms, verdana, arial, sans-serif; font-size: 12px; width: 380px;"></textarea>
</form>I get the following error:Parse error: parse error, unexpected $ in contact1.php on line 84Line 84 is the closing </html> tag in the page however, so I wondered if you could have a nosey around for me at all to see if you could find a glitch?

splufdaddy
June 14 '04, 08:30 PM
This is the corrected line here:<p>Please be aware that your IP address (<? echo $_SERVER['REMOTE_ADDR']; ?>)

You're missing a semi-colon after the echo command.

ajaspers
June 14 '04, 08:38 PM
You don't need the semicolon if it's only one line of PHP code. justlivyalife, please post the full script - that block of code is 46 lines and the error is on line 84. ;)

splufdaddy
June 14 '04, 08:44 PM
My mistake, I thought it was necessary. Here's another shot in the dark: You have an open bracket for your "else" statement if the form was not submitted. Did you close that bracket? It's not posted, it should be right after the closing </form> tag.

justlivyalife
June 15 '04, 08:45 AM
Thanks, splufdaddy I tried your first tip but it made no difference, the second point makes no sense, as the end of the PHP tag is before the start of the form, but that else statement doesn't get closed. I added<?
}
?>After the form tag and solved that problem and now it works, well sort o at least.

I have another problem though :(. Upon clicking submit, I get the following error.Warning: Server Error in contact.php on line 33
There was an error while sending the mail.I've put my code below (all of it ;)) so that youi can see the error, i've marked line 33.<!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=windows-1252" />
<meta http-equiv="Content-Style-Type" content="text/css" />
<title>RN Design - Low cost professional website design and development - Lichfield, UK</title>
<link href="rndesignco.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div class="logo">
<img src="images/logo.jpg" alt="Low Cost Website Design and Development in Lichfield, UK" width="680" height="80" />
</div>
<div class="bodytexthome">
<div class="pagecontent">
<!-- Start Main Body Content -->
<?

if (isset($_POST['from']))
{
$ip = $_SERVER['REMOTE_ADDR'];
$resolved = gethostbyaddr($ip);
if ($resolved != $ip)
{
$footer = "This e-mail was sent by a user with the IP address $ip, which resolved to '$resolved'.";
}
else
{
$footer = "This e-mail was sent by a user with the IP address $ip, which could not be resolved to a hostname.";
}
$message = $_POST['message'];
$message = "$message\n\n------------\n$footer";

LINE 33 if (mail('justlivyalife@btopenworld.com', $subject, $message, "From: $from\r\nReply-To: $from\r\n"))
{
echo 'Your mail was successfully sent.';
}
else
{
echo 'There was an error while sending the mail.';
}
}
else
{
?>
<form method="post" action="<? echo $_SERVER['PHP_SELF']; ?>">
<h1>Contact RN Design</h1>
<p>You can contact RN Design by emailing us directly,or you can fill in the
contact form below which will be sent to us directly. We aim to respond to
all emails within 48 hours of the time of sending, though this is not always
possible. Please ensure that you give an accurate Subject to your message
when emailing us.</p>
<p>Please be aware that your IP address (<? echo $_SERVER['REMOTE_ADDR']; ?>) will be sent along with the message
to prevent against abuse of this mail form.</p>
Your name:<br /><input name="name" type="text" id="name" class="form" onfocus="this.className='form-on'" onblur="this.className='form'" style="width: 380px;" /><br /><br />
Your email:<br /><input name="from" type="text" id="from" class="form" onfocus="this.className='form-on'" onblur="this.className='form'" style="width: 380px;" /><br /><br />
Subject:<br /><input name="subject" type="text" id="subject" class="form" onfocus="this.className='form-on'" onblur="this.className='form'" style="width: 380px;" /><br /><br />
Message:<br /><textarea name="message" cols="40" rows="6" id="message" class="form" onfocus="this.className='form-on'" onblur="this.className='form'" style="font-family: trebuchet ms, verdana, arial, sans-serif; font-size: 12px; width: 380px;"></textarea><br /><br />
Click &quot;Send&quot; only once.<br />
<input type="submit" value="Send" class="button">
</form>
<?

}

?>
</div>
<!-- End Main Body Content -->
<!-- Start Navigation -->
<div class="navigation">
<h1>Navigation</h1><br />
<span class="youare">You are: Contact</span><br /><br />
<a href="index.php" class="nav">Home</a><br />
<a href="portfolio.php" class="nav">Portfolio</a><br />
<a href="services.php" class="nav">Services</a><br />
<a href="tutorials.php" class="nav">Tutorials</a><br />
<a href="contact.php" class="nav">Contact</a><br /><br />
<!-- End Navigation -->
<!-- Start Page Image -->
<img src="images/landscape.jpg" alt="Low Cost Website Design and Development
in Lichfield, UK" width="180" height="309" /><br /><br />
<!-- End Page Image -->
<!-- Start Copyright Notice -->
<span class="copyright">
<a href="http://validator.w3.org/check/referer" class="copyright">xHTML 1.0 Transitional Valid</a>
</span>
<!-- End Copyright Notice -->
</div>
</div>
</body>
</html>

splufdaddy
June 15 '04, 12:52 PM
What doesn't make sense? You seemed to point out what I was saying in your post. The else statement that you needed to close was this one:else
{
?><form method="post" action="...You opened up the else statement and then closed the php code, and you needed to close that else statement. Now that you have closed it, your script works (for me).

I copied your code and compiled it on my server, and it sent successfully.

justlivyalife
June 15 '04, 01:01 PM
Okay, thanks.

I've checked it out and seems like it is a server issue on my end. But at least I know it isn't the code. Thanks for testing it :).

Edit: Not really sure what kind of server problem though.

iKwak
June 20 '04, 12:52 AM
Where would you add the CSS codes to decorate the code page?
I noticed there was no </body> tag so I added it myself.
Not sure if I can add a <head> tag.

justlivyalife
June 20 '04, 12:22 PM
The code in the first post just replaces your form. It goes between your body tags. The head tag goes above the entire form.

Don't forget to remove the two 'require' lines however.

ItalianMike
December 9 '04, 05:49 PM
I'm having some trouble using this script can anyone take a look and tell me what is wrong. What you click send on the form it attempts to go this "http://www.infinite-style.com/%3C?%20echo%20$_SERVER['PHP_SELF'];%20?%3E" non existent URL. Here is a direct link (http://www.infinite-style.com/contact1.html) and here is the source code:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<!-- Produced by Infinite Style www.infinite-style.com //-->
<title>Infinite Style: Contact Us</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<meta name="MSSmartTagsPreventParsing" content="TRUE" />
<meta http-equiv="expires" content="-1" />
<meta http-equiv= "pragma" content="no-cache" />
<meta name="robots" content="all" />
<meta name="author" content="Infinite Style" />
<meta name="description" content="Do you have a question, comment, or suggestion to pass along
to us? Here you will find all our contact information." />
<link href="common.css" type="text/css" rel="stylesheet" />
</head>
<body>
<!-- This is the allover god-wrapping div //-->
<div id="wrapdiv">
<div id="upperdiv">
</div>
<div id="navigation">
<ul>
<li><a href="index.html">Index</a></li>
<li><a href="about.html">About</a></li>
<li><a href="articles.html">Articles</a></li>
<li><a href="audio.html">Audio</a></li>
<li><a href="video.html">Video</a></li>
<li><a href="pictures.html">Pictures</a></li>
<li><a href="events.html">Events</a></li>
<li><a href="contact.html">Contact</a></li>
<li><a href="links.html">Links</a></li>
</ul>
<div id="search">
<form action="/search/search.cgi">
<label for="fdse_TermsEx">Search:</label>
<input name="Terms" id="fdse_TermsEx" size="10" />
<input name="goButton" type="image" src="pics/tech/search_button.gif" border="0" value="go" />
</form>
</div>
<div id="navibottom"></div>
</div>
<div id="content"><br />
<h2 class="pageName">&gt;&gt; CONTACT:</h2>
<div id="contentleft">
<div class="story">
<h3>Send us your feedback</h3>
<?
if (isset($_POST['from']))
{
ip = $_SERVER['REMOTE_ADDR'];
$resolved = gethostbyaddr($ip);
if ($resolved != $ip)
{
$footer = "This e-mail was sent by a user with the IP address $ip, which resolved to '$resolved'.";
}
else
{
$footer = "This e-mail was sent by a user with the IP address $ip, which could not be resolved to a hostname.";
}
$message = $_POST['message'];
$message = "$message\n\n------------\n$footer";

if (mail('mike@infinite-style.com', $subject, $message, "From: $from\r\nReply-To: $from\r\n"))
{
echo 'Your mail was successfully sent.';
}
else
{
echo 'There was an error while sending the mail.';
}
}
?>
<form method="post" action="<? echo $_SERVER['PHP_SELF']; ?>">
<p>
Do you have a question, comment, or suggestion to pass along to us? If you have a question or comment,
please use the feedback form below to send us an e-mail. Please be aware that your IP address
(<? echo $_SERVER['REMOTE_ADDR'] ?>) will be sent along with the message to prevent against abuse of
this mail form.
</p>
<table border="0" cellspacing="0" cellpadding="3">
<tr>
<td>
<b>Your e-mail address:</b><br />
<em><input type="text" name="from" size="50"></em>
</td>
</tr>
<tr>
<td>
<b>Subject:</b><br />
<em><input type="text" name="subject" size="50"></em>
</td>
</tr>
<tr>
<td>
<b>Message:</b><br />
<textarea name="message" rows="10" cols="50"></textarea>
</td>
</tr>
<tr>
<td align="center">
Click &quot;Send&quot; only once.<br />
<input type="submit" value="Send">
</td>
</tr>
</table>
</form>
</div>
</div>
<div id="eventright">
<div class="rightpic">
<img src="pics/tech/white.gif" alt="Spacer" />
</div>
<div class="rightpic">
<img src="pics/tech/white.gif" alt="Spacer" />
</div>
<div class="rightpic">
<img src="pics/tech/white.gif" alt="Spacer" />
</div>
<br />
<br />
<br />
</div>
</div>
<div id="footer">
<a href="index.html" title="Index">INDEX</a> -
<a href="about.html" title="About">ABOUT</a> -
<a href="articles.html" title="Articles">ARTICLES</a> -
<a href="audio.html" title="Audio">AUDIO</a> -
<a href="video.html" title="Video">VIDEO</a> -
<a href="pictures.html" title="Pictures">PICTURES</a> -
<a href="events.html" title="Events">EVENTS</a> -
<a href="contact.html" title="Contact">CONTACT</a> -
<a href="links.html" title="Links">LINKS</a><br />
&copy; 2004 Infinite Style |
<a href="mailto:webmaster@infinite-style.com">Webmaster</a> |
Standard–compliant according to W3C's standards for <a href="http://validator.w3.org/check/referer" title="Valid HTML!">HTML</a> &amp;
<a href="http://jigsaw.w3.org/css-validator/validator?uri=http://www.infinite-style.com/common.css" title="Valid CSS!">CSS</a>.
</div>
</div>
</body>
</html>

mossoi
December 9 '04, 08:43 PM
<form method="post" action="<? echo $_SERVER['PHP_SELF']; ?>">

This bit of the above line, <? echo $_SERVER['PHP_SELF']; ?>, is a PHP variable to tell the form to reload the current page when you click submit. It's not working because the page is unable to handle the PHP and is writing out the actual piece of code rather than the variable value.

You could replace that with content1.html (the name of the page) but it won't do much good because it looks as though your page isn't parsing PHP.

Basically this whole thing isn't going to work at all within standard HTML. The page must be named [filename].php to handle this code (unless you've got a customised server setup which is unlikely) and your server must be able to parse PHP pages.

BTW - I like the way you've hidden the email address in the contact.html form - sadly I don't think that's going to stop spiders from getting at it.

ItalianMike
December 10 '04, 03:37 AM
So can I simply rename the file to contact1.php and maintain the code as I have it? I've tried that and I get this error: Parse error: parse error in /home2/www/infinite-style/contact1.php on line 50.

Line 50 on my source reads: ip = $_SERVER['REMOTE_ADDR'];

Do I need to remove the headers and footers (HTML) and use includes?

mossoi
December 10 '04, 10:18 AM
It looks like the site is capable of running PHP which is good.

You need to put a $ in front of ip on this line:

ip = $_SERVER['REMOTE_ADDR'];

So that it reads

$ip = $_SERVER['REMOTE_ADDR'];

$xxxx assigns a value to a variable, without the $ PHP tries to read ip as a command and returns a parse error because it doesn't exist.

ItalianMike
December 10 '04, 10:23 AM
I got PHP

mossoi
December 10 '04, 10:26 AM
The form seems to be working from my server after I changed line 50. You should receive a test email.

ItalianMike
December 10 '04, 10:32 AM
yeah it works now, I have one more question ... I'f I want to change to display properties for "Your mail was successfully sent" do I do it by adding HTML syntax to where it appears in the PHP?

** EDIT **

How can I make it so it appears after the little explanatory rant, right about the form boxes?

mossoi
December 10 '04, 10:38 AM
Yeah - you might find it easier to change:

{
echo 'Your mail was successfully sent.';
}

to:

{
?>
Whatever HTML you want to display when the form is sent
<?php
}

It can avoid the HTML screwing up the PHP if you not sure of the ECHO command.

mossoi
December 10 '04, 10:45 AM
How can I make it so it appears after the little explanatory rant, right about the form boxes?


The easiest way is to move this:


<p>
Do you have a question, comment, or suggestion to pass along to us? If you have a question
or comment, please use the feedback form below to send us an e-mail. Please be aware that
your IP address (<? echo $_SERVER['REMOTE_ADDR'] ?>) will be sent along with the message
to prevent against abuse of this mail form.
</p>

To just below this:

<h3>Send us your feedback</h3>

ItalianMike
December 10 '04, 11:23 AM
Tight ... thanks for the reply ... I pretty much got everything figured out through trial and error. There is however one last thing that doesn't seem to work. I want to make the message which loads after you hit "send" appear in red. What I've tried to do is add HTML and CSS elements within in the PHP script, like so:


echo '<div id="confirm"><strong>** Your mail was successfully sent **</strong></div>';
}
else
{
echo '<div id="confirm"><strong>** There was an error while sending the mail **</strong></div>';
}


Then in my CSS file I created a listing for this div (confirm) with a text color of red ... it however still comes up the color it's been displaying in. I've also tried to move the div outside of the PHP script, but it doesn't seem to work.

Am I missing something?

www.infinite-style.com/common.css
www.infinite-style.com/contact.php

mossoi
December 10 '04, 11:32 AM
There doesn't seem to be a confirm object in your CSS. Other than that it should work as normal HTML.

ItalianMike
December 10 '04, 01:01 PM
working fine now ... thanks for all the help

mossoi
December 11 '04, 02:00 AM
Word!

obe1ben
April 1 '05, 02:15 AM
Where do I put this php code in the rest of the code?

if (mail('your (hanznsolo) e-mail address', $subject,

p.s. I am new with PHP :ichatmoney:

Wired
April 1 '05, 02:32 AM
You don't. That is specific to that one person who asked it.

if (mail('YOUR E-MAIL ADDRESS', $subject, $message, "From: $from\r\nReply-To: $from\r\n"))

That's the actual code. Copy/paste the code from the first post into a php page, upload it, check it out.

oracus
April 25 '05, 09:25 PM
Sorry..The only think that we must do is to put these line in a HTML editor??And thats it?

raja
May 6 '05, 04:58 PM
Hey iam a novice designer from india aged 19 .one of my friend hosted my site free of cost which supports php mysql cgi n perl.pls any on can help me how to use php mail form plzz. iam a novice plzz. help me iam greatful to u people.Thanks in advance
raja :surprised:

Wired
May 6 '05, 07:02 PM
Can you be more specific?

raja
May 7 '05, 05:26 AM
sure i want to add a form to mail to my web page using php or cgi any one can help me plzzz.
cheers
Raja

zionlocke
August 25 '05, 03:25 PM
I decided to take a whirl at this and also fell in love with tekp's beautiful use of fieldset and legend. Here a little bit better code so the whole thing runs on one page as intended. The redirect page is used to clear the server's $_POST vars.

I still have one question. How do I keep people from adding a BCC to the mail function. I didn't think it would be possible, but someone was able to send an email with the BCC column filled in. Is there a way to ensure more protection into email forms?

source of the contact page<?php

function validate_email($str) {
$str = strtolower($str);

if(strlen($str) == 0) {
return 0;
}

if(ereg("^([^[:space:]]+)@(.+).(ad|ae|af|ag|ai|al|am|an|ao|aq|ar|arpa|as|at|au|aw|az|ba|bb|bd|be|bf|bg| bh|bi|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|com|c r|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|eh|er|es|et|fi|fj|fk|fm|fo|fr|fx |ga|gb|gov|gd|ge|gf|gh|gi|gl|gm|gn|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id| ie|il|in|int|io|iq|ir|is|it|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|l i|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|mg|mh|mil|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|mv|mw |mx|my|mz|na|nato|nc|ne|net|nf|ng|ni|nl|no|np|nr|nu|nz|om|org|pa|pe|pf|pg|ph|pk| pl|pm|pn|pr|pt|pw|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr |st|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|u z|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)$",$str) && strlen($str) <= 150){
return 1;
} else {
return 0;
}
}

function validate_input($str) {
if(strlen($str) == 0) {
return 0;
}

if(strlen($str) <= 25) {
return 1;
} else {
return 0;
}
}

function validate_message($str) {
if(strlen($str) == 0) {
return 0;
}

if(strlen($str) <= 1200) {
return 1;
} else {
return 0;
}
}

function not_valid() {
if (!validate_input($_POST['name']) || !validate_email($_POST['from']) || !validate_input($_POST['subject']) || !validate_message($_POST['message'])) {
return 1;
}
}


if (validate_input($_POST['name']) && validate_email($_POST['from']) && validate_input($_POST['subject']) && validate_message($_POST['message'])) {

$subject = $_POST['subject'];
$from = $_POST['from'];
$name = $_POST['name'];
$message = $_POST['message'];
$message = "$message\n\n$name\n";

if (mail('you@email.com', $subject, $message, "From: $from\r\nReply-To: $from\r\n")) {
ob_clean();
header ("Location: redirect.php");
exit();
echo '<b>Your mail was successfully sent.</b>';
} else {
echo '<b>There was an error while sending the mail.</b>';
}

} else {

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Contact</title>

<?php
include('header.php');
?>

<form method="post" action="<? echo $_SERVER['PHP_SELF']; ?>">
<fieldset>
<legend>Compose</legend>
<p>
<label for="name">Name: </label> <small>(25 character limit)</small> <br/>
<input type="text" value="<? if($_POST['name'] != "") echo $_POST['name'] ?>" id="name" name="name"/>
</p>
<p>
<label for="from">Email: </label> <small>(150 character limit)</small> <br/>
<input type="text" value="<? if ($_POST['from'] != "") echo $_POST['from'] ?>" id="from" name="from"/>
</p>
<p>
<label for="subject">Subject:</label> <small>(25 character limit)</small> <br/>
<input type="text" value="<? if($_POST['subject'] != "") echo $_POST['subject'] ?>" id="subject" name="subject"/>
</p>
<p>
<label for="message">Message:</label> <small>(1200 character limit)</small> <br/>
<textarea name="message" id="message" rows="8" cols="30"><? if($_POST['message'] != "") echo $_POST['message'] ?></textarea>
</p>
<p>
<? if(not_valid()) echo '<small>The composed letter is not yet valid.</small>' ?>
</p>
</fieldset>
<input type="submit" value=" Send "/> <input type="reset" value=" Clear "/>
</form>

<br/>

<?php
include('footer.php');
}
?>
source for redirect.php<?php
ob_clean();
header ("Location: http://www.zioncreation.com/");
exit();
?>

eKstreme
August 25 '05, 04:37 PM
I still have one question. How do I keep people from adding a BCC to the mail function. I didn't think it would be possible, but someone was able to send an email with the BCC column filled in. Is there a way to ensure more protection into email forms?


Well a mail BCC header is just "BCC:" followed by the addresses. So just replace BCC: with an empty space and that will do it. You can be crafty and remove everything from the BCC: till the next newline.

zionlocke
August 25 '05, 10:17 PM
This technique is fairly new. I'm glad it has already happened to me so I can stop it before it gets too serious. The mail function for php has a vulnerability that allows people to spam with your account:
http://us3.php.net/manual/en/ref.mail.php

You can find out how to stop the mail injection here:
http://securephp.damonkohler.com/index.php/Email_Injection

Karloff
April 12 '06, 02:46 PM
hi,
trying to use this for a site of mine, can't get it to work. what do i have to change??

implemented it and added my e-mail but when on the site it give me a
'error 404: page not found'
message!?

any tips please??

bfsog
April 12 '06, 03:11 PM
Could you post your code?

It sounds like the file you are submitting to (which you specify in the <form action="file">) does not exist.

Edit: it may be the require/include statements (I forgot which one is used).

Karloff
April 12 '06, 03:19 PM
check it here,

http://www.paulcurley.com/Enquiries.htm

bfsog
April 12 '06, 03:31 PM
You are referencing that page, but it is a .htm file

Open enquiries.htm and rename as enquiries.php

As right now your trying to submit to <?php echo... and so on.

Karloff
April 12 '06, 04:06 PM
i've done that but no mail is comin through??

any thought, sorry if its a daft question

bfsog
April 12 '06, 04:22 PM
So what message do you get?

Post your answer in the other thread instead of us going off topic.

If you get the message that indicates success, then maybe it is just a slow one..

Karloff
April 12 '06, 05:53 PM
sorry man for the delay, it keep on comin up with the message
'There was an error while sending the mail.'

what could be wrong?

bfsog
April 12 '06, 07:35 PM
Paste the line in your file that looks similar to

if (mail('YOUR E-MAIL ADDRESS', $subject, $message, "From: $from\r\nReply-To: $from\r\n"))

Karloff
April 13 '06, 09:18 AM
cool, but where do i specify the e-mail address to send the info to???

bfsog
April 13 '06, 09:23 AM
Look at my last post, that is where you specify which email address.

Lets say your joe@bloggs.com, the line of code would look like..


if (mail('joe@bloggs.com', $subject, $message, "From: $from\r\nReply-To: $from\r\n"))

Karloff
April 13 '06, 09:25 AM
thanks man, in work but will do that when i get home.

thumbs up! cheers

Karloff
July 12 '06, 09:33 AM
sorry, i'm back with another dumb question!

i implemented this into a site for someone and they have told me that it doesn't send to e-mail address of the sender in the e-mail. thought it did. can anyone tell me why.... not sure if they have messed with the code in some way that i can't see?

supposidly (spellin!) it only send ip address

cheers

Shadowfiend
July 12 '06, 09:36 AM
(Supposedly) Where are you pulling the `from' address from? If there's no `from' header, I think PHP inserts a default one, likely set to the IP address in your case.

Karloff
July 13 '06, 09:22 AM
sorry for the delay, don't you hate power cuts!!!

anyways, i thought the e-mail address input in the form would send it (as text) in the e-mail. if it doesn't and is like you say, how can i develop the script to do so?

cheers

Shadowfiend
July 13 '06, 09:52 AM
Theoretically, it does. Why don't you post your code so we can see? Better post it as an attachment.

Karloff
July 13 '06, 10:03 AM
can't do it till later, in work now. will try post in a few hrs. thanks

Karloff
July 13 '06, 03:03 PM
here is the file man:)

Shadowfiend
July 13 '06, 04:09 PM
You never set your from address. Replace this:

if (isset($_POST['from']))
{
$ip = $_SERVER['REMOTE_ADDR'];
with this:

if (isset($_POST['from']))
{
$from = $_POST['from'];
$ip = $_SERVER['REMOTE_ADDR'];

Karloff
July 13 '06, 04:44 PM
thanks man, i'll give that a try

rosland
July 13 '06, 05:16 PM
There are a number of questions regarding PHP email functionality popping up again and again here.

Sending basic emails with PHP is quite easy. Sending email with HTML formatting is quite easy as well, you just need t add headers to your script.

Sending email with an attachment offer, is a bit more challenging, but is generally just an extention of the headers section.
To say you "just" need knowledge of HTML, would be a mild understatement.

However, you would get a comprehensive understanding of how a HTML document is buildt through headers (how a browser presents information) if you care to study how you can build a script that not only sends formatted mail, but also accepts attachments, or multiple attachments.

There's a script for that availble on Zend.com.

If you can't find it, I'll pass you the link.

smoseley
July 13 '06, 08:11 PM
Email formatting requires knowledge of MIME, RTF, and HTML. Multipart mime emails can get tricky. It's nice to have a function-set or class that handles everything for you.

I started building this PHP 5 Mail class today, based upon Filburt's email form.

It's still a little incomplete. I'm going to add mime content-type control and templating to it (for use in an app I'm building).


<?php
class Mail {
private $from = null;
private $to = null;
private $subject = null;
private $body = null;
private $type = null;
private $template = null;
private $alias = null;
private $ipAddress = null;
private $hostName = null;

public function __construct($from=null, $alias=null, $to=null, $subject=null, $body=null) {
$this->from = $from;
$this->alias = $alias;
$this->to = $to;
$this->subject = $subject;
$this->body = $body;
}

// Properties
public function setSender($sender) {
$this->from = $sender;
}
public function setRecipient($recipient) {
$this->to = $recipient;
}
public function setSubject($subject) {
$this->subject = $subject;
}
public function setBody($body) {
$this->body = $body;
}
public function setType($type) {
$this->type = $type;
}
public function setTemplate($template) {
$this->template = $template;
}
public function setAlias($alias) {
$this->alias = $alias;
}
public function setRemoteAddress($ipAddress) {
$this->ipAddress = $ipAddress;
$this->hostName = gethostbyaddr($ipAddress);
}

/**
* Sends the email
*/
public function send() {
$from = $this->from;

// Generate mail body
$body = $this->body;
if (!is_null($this->ipAddress)) {
$body .= "\n\n----------------------------------------\n\n" .
"This e-mail was sent by a user with the IP address $ip, ";
if ($this->hostName == $this->ipAddress) {
$body .= "which could not be resolved.";
} else {
$body .= "which resolved to '$resolved'.";
}
}
mail($this->to, $this->subject, $body, "From: $from\r\nReply-To: $from\r\n");
}
}
?>

And it is used like this:

<?php
$mail = new Mail($from, $senderName, $to, $subject, $message);
$mail->setRemoteAddress($_SERVER["REMOTE_ADDR"]);
$success = $mail->send();
?>


Fun !!!! :-D

Shadowfiend
July 13 '06, 08:36 PM
I don't suppose I could point (http://lwest.free.fr/doc/php/lib/index.php3?page=mail&lang=en) you (http://pear.php.net/package/Mail_Mime) to existing (http://phpmailer.sourceforge.net/) solutions (http://framework.zend.com/manual/en/zend.mail.html) and imply reinventing the wheel, while educational, can be less secure? :-P

Joking and further resources aside though, looks cool :-)

smoseley
July 14 '06, 02:23 AM
:-D Yeah, there's always something better out there.... only problem is that they're better "for everyone" - which inately means they're worse for any individual than a customized solution.

What I'm doing will eliminate the "do it all" functionality (sendmail and smtp support, for example), and will add some functionality that I need (although maybe in a separate class).

Also, mine will be far superior coding.

If you look at the source of PHPMailer, for example, they have one big class for the whole thing, with a million functions in it. They should be using Polymorphism, with something like this:

interface Mailer;
class MailerImpl implements Mailer;
class SendmailMailerImpl implements Mailer;
class SMTPMailerImpl implements Mailer;

Instead, they have a public variable that allows the user to set his mailer type. WTF??? :confused:

Bottom line: they made a jumble of their crap, and I don't want to mess with other people's crap. Instead, I'll take the snippets I can use (like header info) and make something better. :)

Shadowfiend
July 14 '06, 09:20 AM
Do remember most of these were written pre-PHP5, where doing crazy things like that, while not the best idea, was in no way explicitly avoidable I see your point, however :-)

Make sure you don't put GPL'ed code in your code ;-)

smoseley
July 15 '06, 02:50 AM
Ok, I've got my mailer component working! :-D

I called it "SMail" (Steve Mail, i guess... my initials are SM, too :)).

There are some issues with spam filters using the mail() function of PHP, though, unless you have access to configure your server... I think I'm giong to abstract it and create an SMTP implementation down the road. :-P

beans
October 3 '06, 01:29 AM
Hi guys, I am using the form as posted by filburt1 at the start of the thread. I was wondering how easy it would be to chuck in a radio button so that the user could choose which email address they would like to send to.

The scenario is that our company has one website, but two stores (one in Melbourne, one in Sydney) which both have different email addresses. So I want something of the lines of
'Please choose which studio you would like this mail to be sent to:
*Melbourne
*Sydney'

And by clicking one of these the form gets sent to the right email address.
Yeah i know, i'm being slack and hoping someone else has already done this so i can just copy :nervous:

chinna732003
May 15 '08, 02:29 AM
hi
iam beginner in website design
i want to know which language like html,dhtm,xml,php,javascript should learn first ,so that i can create a meaning ful site.
plz specify the language name

Shadowfiend
May 15 '08, 10:03 AM
chinna: please do a search on the forum. There have been more than a dozen posts on this matter and they all have excellent replies. Also, please avoid posting such messages on unrelated threads -- start a new thread if it doesn't fit into an existing topic, and try to search for an existing answer before posting. WDF has been around a long time, so a LOT of questions have been answered.