PDA

View Full Version : PHP Contact form



starr_2005
08-31-2011, 07:58 PM
I've been asking around on different programming forums on how to make a php contact form where I am hosting the clients website doesnt allow phpmail () and they allow php 5. Also, the client has a gmail account. I've been going around this contact form and trying different scripts and such and none of the scripts I've tried work. I've had people suggest a setting the email address up with SMPT, phpmailer and phpmailer class, php is not my strongest point, but I know a little. I would greatly appreciate any help. Thanks!

weegillis
08-31-2011, 09:32 PM
Going in, we want to be sure of one thing, does your client's hosting plan come with a mail server? Usually a hosting plan will allow hundreds, or thousands of e-mail accounts on the hosted domain. In the hosting control panel will be an e-mail section in which the client can create an info@example.com account and give it a password. Now forward that to the gmail account. In this way the form handler can be set to send only to recipients on the same domain as the contact form.

Many hosting companies are strict on that point. Yours may be one of them. I know MegaServers is, and they are a biggy. You won't see their name in the front, it will be Telus, or some other equally big hosting provider, all piggy backing on MegaFTPServers and MegaMailServers.

Did you create a temporary script to read back the PHP INFO from the server? A quick search will find you the method. Once you get the results, save or print the page, then remove that script. You don't want it laying around telling the world all about your PHP installation.

Armed with that, you know what minimum requirements to shoot for. You might even try an older script that's been around for awhile and doesn't need the latest PHP version. Jack's Scripts has Jack's FormMail.php (http://www.dtheatre.com/scripts/formmail.php), that is easy to configure. Read the documentation. It's worth the read.

As to anything newer, I can't say.

starr_2005
08-31-2011, 09:45 PM
No, my clients host doesnt have a mail server because they are using a free host subdomain. How would I create the temporary script? Thats whats kind of confusing me. I am about to do a Google search to see, but if possible could I have some more information?

I also tried downloading Jacks FormMail and I get the 404 error.

Nervermind, I got it working.

If possible, can I get a little more information about creating a temporary script to read back the php info from the server? I've seen a few things on Google, but just to make sure that I am looking at the right thing.

dean
09-03-2011, 02:42 PM
Did you mean phpmail() or mail() ? I don't know of a built-in php function named phpmail().

To get the php and other info from the server, create a file containing:
<?php phpinfo(); ?>
Save it as anythingyouwant.php, upload it and visit it in a browser.
It's a good idea to delete it from the server when finished.

starr_2005
09-03-2011, 03:15 PM
Thats what I meant mail (). I've got my contact form working now, I just need help with a few little things. When I test it, the email says its from an (unknown sender) I would like it for to say the users first and last name. Then, I would also like to add a captcha code in order to avoid spam. Then finally, I would like for it so that when I reply to someone, their email address shows so that I dont have to manually put it in.






<?php


$first_name=$_POST['name'];
$email_address=$_POST['email'];
$subject=$_POST['subject'];
$message=$_POST['text'];

mail("starr@gmail.com","Subject: $subject",
$message, "From: $email");

echo "Thank you for using our mail form.<br/>";
echo "Your email has been sent.";

?>

dean
09-03-2011, 04:19 PM
You define the email field as $email_address, but have something different in your mail function. You might also try "From: $first_name <$email_address>"
I don't know if the form is safe the way you have it now. Where is the error checking, sanitizing and/or validating?

starr_2005
09-03-2011, 04:52 PM
Like this? And I was planning on adding validation later on once I got my form working first.





$first_name=$_POST['name'];
$email_address=$_POST['email'];
$subject=$_POST['subject'];
$message=$_POST['text'];

mail("starr@gmail.com","Subject: $subject",
$message, "From: $first_name <$email_address>");

echo "Thank you for using our mail form.<br/>";
echo "Your email has been sent.";

?>

dean
09-03-2011, 06:01 PM
Yes, did that work? Did it solve the sender and the reply to problem? I'm no expert, but I have a working PHP contact form and it has something like that.

starr_2005
09-03-2011, 07:20 PM
Yes, it worked and did the job right! Thanks! And its very simple, which was something that I wanted.

xenon2010
09-21-2011, 10:00 PM
if you want to send html emails use this awesome function I always use:



function sendHTMLemail($HTML,$from,$to,$subject)
{
// First we have to build our email headers
// Set out "from" address

$headers = "From: $from\r\n";

// Now we specify our MIME version

$headers .= "MIME-Version: 1.0\r\n";

// Create a boundary so we know where to look for
// the start of the data

$boundary = uniqid("HTMLEMAIL");

// First we be nice and send a non-html version of our email

$headers .= "Content-Type: multipart/alternative;".
"boundary = $boundary\r\n\r\n";

$headers .= "This is a MIME encoded message.\r\n\r\n";

$headers .= "--$boundary\r\n".
"Content-Type: text/plain; charset=UTF-8\r\n".
"Content-Transfer-Encoding: base64\r\n\r\n";

$headers .= chunk_split(base64_encode(strip_tags($HTML)));

// Now we attach the HTML version

$headers .= "--$boundary\r\n".
"Content-Type: text/html; charset=UTF-8\r\n".
"Content-Transfer-Encoding: base64\r\n\r\n";

$headers .= chunk_split(base64_encode($HTML));

// And then send the email ....

mail($to,$subject,"",$headers);

}

starr_2005
09-21-2011, 10:27 PM
Thanks! Looks just like what I am looking for. :smile:

monstercoder
11-02-2011, 02:12 PM
The formmail from Tectite.com is superior to using the methods above. The code above allows security holes for SQL injection. You can't just accept fields verbatim. You need to trim stuff out for security reasons. If you're importing the data to a MySQL table, then you will have to filter out apostrophes. The methods above will allow people to execute SQL statements.

The Tectite form mail will also send you warning emails. You can also use form validation to make sure people properly fill in fields when required instead of leaving them blank. You put in hidden fields in the form that will tell it the behaviors of what it should do. You can also put in additional hidden fields to stop bots from form spamming. I can go into those in more detail if needed.