Php developers,Php Tutorials ,Web development, Web Developers, Php hosting, open-source,community
 
Serving the LAMP developers all over the word
LampDevelopers is a pure LAMP Developers community
Php developers,Php Tutorials ,Web development, Web Developers, Php hosting, open-source,community
LAMP Developers
   
   
   
   
   
   
Web Development
   
Developer / Programmer
   
Related Links
   
Member
   
Enter Keyword
E-mail with php

Send E-mail with php:

The one of 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 we will show you how to send e-mail messages using PHP script.

The Mail Command:

The email is extremely easy to send from PHP language, 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:

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

In this PHP mail example we 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 of $headers is used for any additional e-mail headers files you may want to add. The most common use of this header 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 you 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 = "php@gowansnet.com";
$subject = "PHP Is Great";
$body = "PHP is one of the best scripting languages around";
$headers = "From: webmaster@gowansnet.com\n";
mail($to,$subject,$body,$headers);
echo "Mail sent to $to";

This PHP script code will acutally do two things. Firstly it will send a message to php@gowansnet.com 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 webmaster@gowansnet.com. It will also output the text:

Mail sent to php@gowansnet.com

to the browser.

Formatting E-mail

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

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

Mail Without Variables

The e-mail listing 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@gowansnet.com","PHP Is Great","PHP is one of the best scripting languages around","From:

webmaster@gowansnet.com\n");

But this would make your code slightly harder to read.

Error Control

As we have 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) here also. Because of this mistake, 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 will be quite self explanitory code. If the mail is sent successfully to it will output a message to the browser to telling the user, if not, it will display an error message with some suggestions for correcting the problems occured.

News
this is the news
this is the news this is the news this is the news....
openWYSIWYG 1.01 bet....
small description
Links