PHP Email: Try to understand it

· Dev & Design
Authors

The major advantage of server side scripting language is that, it provides a way
to take a form input from a server and send e-mail to a perticular e-mail address.


The Mail Command

Many of the scripting languages require special set up(like CGI) to send an e-mail,
But with PHP it is very easy, since it uses one simple command called mail().
This command is used as follows.

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

The recepients address to whom mail has to be sent is mentioned using the $to variable.
Subject line is mentioned using the $subject. And finally the body of the mail is denoted by
$body variable.

If at all any additional headers has to be added to the mail then $headers is used.
For example if we want to add cc(carbon copy) and bcc(blank carbon copy) headers then this variable can be used.


Sending An E-mail

Befopre sending the mail you should have to set the variable content that are to
be used in mail command. Here is a simple code that illustrate this.

$to = “php@vyom.co.in”;
$subject = “This is PHP Script”;
$body = “PHP is one of the best scripting language”;
$headers = “From: webmaster@gowansnet.com\n”;
mail($to,$subject,$body,$headers);
echo “Mail sent to $to”;

Here e-mail is from webmaster@vyom.co.in. This code performs two things.

  • Firstly it will send a message to php@vyom.co.in
    with
    subject:’This is PHP Script’and
    text:
    PHP is one of the best scripting language
  • Secondly It will output the

    text:Mail sent to php@vyom.co.in to the browser.

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


Error Control

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”;
}

In this code the mail() function is taken into a IF loop, If the the funtion executes properly,
then the echo statement inside the IF funtion is executed saying that the
mail has been sent. Otherwise it displays the error message saying that the mail has not sent
and suggests to correct the problem.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s