Sending Forms to Email

I have a website built in PHP and I would like to create my own forms that get sent to my email address. I am very new to PHP and html. I just searched the web on this topic but all the html/php codes/script that I found, didn’t work. I would love to create my own forms instead of paying a service to do this for me. Does anyone know which code/script I should use to make this happen? Thanks.

If you’re a novice at PHP, as I was when I did my first contact form only a month ago, you might want to keep it a bit simpler than that.

Something along the lines of this:

$mail_To = “”;
$mail_From = $_POST[‘email’];
$mail_Subject = “Contact from your website”;
$mail_Body = "Name: " . $_POST[‘name’] . "
Email: " . $_POST[‘email’] . "

Field1: " . $_POST[‘field1’] . "

Field2: " . $_POST[‘field2’];
mail($mail_To, $mail_Subject, $mail_Body, $mail_From);
header(“Location: [COLOR=“Purple”][/COLOR]”);

where the “mail to” and “subject” are what you want in the email header, the green items are the names of the input fields in the form, the blue items are the field names as you want them to appear in the email to you, every
puts a line break in the email, and the URL redirects visitors to that page when they submit the form.

$_POST means it is taking the data from the posted form. The full stops join together the inputs and text strings.

Obviously you can add more fields as needed using the same format.

Note that some web hosts may require additional details such as an initialisation line at the top, or a fifth parameter in the “mail(…)” bit at the end - their documentation should give details of that if it applies.

Then for the form itself, start it with <form method=“post” action=“contact.php”>, where you have saved the above as “contact.php”.

Thank you for your response. However, I am a novice at web design at best!
How do I go about checking the php script? Thanks.

A possible problem with those scripts not working could be that your PHP is not aware of how to use sendmail.

Use the following code to review your php settings -

        echo phpinfo();

Look for sendmail_path setting, mine looks like this -

Now if this is set, there could be an issue with your php code. By default, its value is “sendmail -t -i”. Since I use a vps, such configurations are under my control. But if you’re using a shared host, do lookup what their FAQ/Help docs have to say on mailing.

Here is what I used once, and modify it at times needed to suit the purpose. This would handle any form pointed to it via POST method with appropriate fieldnames and mail to recipients.

Though it looks a little ugly, but I’m sure your can cut it down to suit your need (remove captcha etc).

try some ajax alternatives from web resource depot
not only they works well, they’re also got enhanced user interface