You are invited to Log in or Register a free Frihost Account!

Mail with PHP (Simple Tutorial)


One of the major uses of a server side scripting language is to provide a way of sending e-mail from the server and, in particular, to take form input and output it to an e-mail address. In this part I will show you how to send e-mail messages using PHP.

The Mail Command

Mail is extremely easy to send from PHP, unlike using scripting languages which require special setup (like CGI). There is actually just one command, mail() for sending mail. It is used as follows:


In this example I have used variables as they have descriptive names but you could also just place text in the mail command. Firstly, $to. This variable (or section of the command) contains the e-mail address to which the mail will be sent. $subject is the section for the subject of the e-mail and $body is the actual text of the e-mail.

The section $headers is used for any additional e-mail headers you may want to add. The most common use of this is for the From field of an e-mai but you can also include other headers like cc and bcc.

Sending An E-mail

Before sending your mail, if you are using variables, you must, of course, set up the variable content beforehand. Here is some simple code for sending a message:

$to = "";
$subject = "PHP Is Great";
$body = "PHP is one of the best scripting languages around";
$headers = "From:\n";
echo "Mail sent to $to";

This code will acutally do two things. Firstly it will send a message to with the subject 'PHP Is Great' and the text:

PHP is one of the best scripting languages around

and the e-mail will be from It will also output the text:

Mail sent to

to the browser.

Formatting E-mail

Something you may have noticed from the example is that the From line ended with \n. This is acutally a very important character when sending e-mail. It is the new line character and tells PHP to take a new line in an e-mail. It is very important that this is put in after each header you add so that your e-mail will follow the international standards and will be delivered.

The \n code can also be used in the body section of the e-mail to put line breaks in but should not be used in the subject or the To field.

Mail Without Variables

The e-mail above could have been sent using different variable names (it is the position of the variables in relation to the commas, not the name of them which decides on their use). It could also have been done on one line using text like this:

mail("","PHP Is Great","PHP is one of the best scripting languages around","From:\n");

But that would make your code slightly harder to read.

Error Control

As anyone who has been scripting for a while will know, it is extremely easy to make mistakes in your code and it is also very easy to input an invalid e-mail address (especially if you are using your script for form to mail). Because of this, you can add in a small piece of code which will check if the e-mail is sent:

if(mail($to,$subject,$body,$headers)) {
echo "An e-mail was sent to $to with the subject: $subject";
} else {
echo "There was a problem sending the mail. Check your code and make sure that the e-mail address $to is valid";

This code is quite self explanitory. If the mail is sent successfully it will output a message to the browser telling the user, if not, it will display an error message with some suggestions for correcting the problem.
Does there need to be anything enabled on the PHP server? For instance, whatever properties in the Apache server configuration file???
Nothing specific...u just have to set the SMTP settings in php.ini file...

Anyways if u wanna use it on Frihost or other will simply work without any changes....just upload and run..
please have a look at:

here u can easily setup the files to send online datas to our email address!
I Think, it can help you!


Could we just use the code and apply it to frihost?
Any recommendation for starter PHP book?
Related topics
PHP: Feedback form issues.. help =P
Complete Tutorial LINKS for every language
Simple and Detailed Mail Script
PHP OOP Tutorial
php mail and BCC
Game Maker
A very good PHP MySQL Tutorial
Can You Get HTML in Your PHP Mail Script?
Looking to make an online database using php and mysql
Php Code/Tutorial Needed.
Looking For a Simple Example of Linking php to mysql
Vedio tutorial
PhP Image gallery?
Reply to topic    Frihost Forum Index -> Miscellaneous -> Tutorials

© 2005-2011 Frihost, forums powered by phpBB.