Gmail Calendar Documents Reader Web more »
Recently Visited Groups | Help | Sign in
Google Groups Home
Group info
Members: 21
Language: English
Group categories:
Computers > Databases
Computers > Programming
Computers
More group info »
Recent pages and files
creating-a-simple-email-form-in-php    

You want your site's visitors to be able to contact you (to offer youwork, marriage, congratulations on your genius etc) but, as soon as youput your email address on the site, you're inundated with spam andNigerian gentlemen offering you $5 million. This tutorial shows you howto make a simple email form in PHP so that a form can be mailed to youwhen the user hits the submit button. Gareth then shows how to make asimple HTML mail, as well as a useful tip on how to make helpful hintsappear in the status bar while the user fills in the form.

This is suitable for beginners to advanced users of PHP.

Inthis article we're going to look at how to send emails through PHP, sothat users can enter their details on a form, and their details areemailed to you for example. We'll look at what is needed to send mailthrough PHP, and then at the PHP mail() command. In this article we'lllook at sending plain text emails first, and then at how to send HTMLemails.

What PHP requires to send Email

In order to sendemail, PHP needs to access a mail server which actually sends the emailto its final destination. If your site is hosted on a web host'sserver, it's likely that they already have a mail server running onthere. If you're developing your PHP code at home, you'll either needto have a mail server running such as Sendmail on Linux or the MailServer part of IIS on Windows. Alternatively, it may be possible to useyour ISP's SMTP (mail) server as you would through your normal emailprogram. These details are specified in the php.ini file which we'lllook at next.

SMTP settings in the PHP.ini file

Therelevant mail settings in the php.ini file are different for Windowsand Linux. We'll look at the settings for each Operating Systemindividually.

Windows

If you're running Windows on the server, the following settings apply:

SMTP = localhost

Thissetting tells PHP where your mail server is located. By default this islocalhost, which refers to the same server that the PHP page is runningon.? Alternatively, you could give the IP address or domain name of themail server you want to use.

sendmail_from = me@localhost.comThis email address is being protected from spam bots, you need Javascript enabled to view it

Thissettings tells PHP which email address to use as the From address ofthe sent emails if no from address is passed to the mail() command. Youcan change this to any value you want, although if you're using yourISP's mail server it makes sense to use the email address of youraccount.

SMTP Port = 25

This setting is only availablefor PHP version 4.3.0 or above. It allows you to specify the port thatyour mail server uses, if it doesn't use the standard port 25.

Linux

Ona Linux server, PHP expects sendmail to be running on the same serveras PHP runs on. The following setting allows you to tell PHP wheresendmail is located, and to give any custom parameters.

sendmail_path = sendmail -t -i

This example above shows the default value, which will usually not need to be changed unless you have a specific need.

Now that we have looked at what PHP requires to send emails, we'll move on and look at the PHP mail() command itself.

The PHP mail() command

At its simplest, the PHP mail() command just needs the following parameters:

$result = mail($to, $subject, $message, $headers);

These parameters are explained below:

$to = Email address(es) to send to (if multiple addresses are specified they should be separated by a comma).

$subject = The subject line for the message.

$message = The email message body.

$headers = Any email headers that you wish to include.

Atthis stage, it's a good idea to use the following test code, just tocheck that PHP is sending emails correctly on your server. Create a newPHP page in Dreamweaver MX, and add the following code to the body ofthe page. Save the file as testemail.php and upload to your server,then view the page in your browser.

";
mail($to,$subject,$message,$from);
if($result === TRUE){
???? echo "Email sent to Mail Server Successfully";
} else {
???? echo "Email sending to Mail Server Failed";
}
?>

Allbeing well, when you view the page in your browser you'll shortlyreceive an email to your chosen email address, and you'll see "Emailsent to Mail Server Successfully" in your browser window.

If youhave a problem, such as the email not being sent, double check thesettings in the php.ini file. Also it's a good idea to check the onlinePHP manual at:

http://www.php.net/manual/en/ref.mail.php

Althoughin theory email requirements should be the same on all servers,different OS's and mail servers sometimes have their own quirks, butit's likely that another user in a similar situation has written themup in the user comments of the page above.

Read More

Version: 
Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google