Now one of the best part of Learning PHP is realising that you can do so much more than just get a record from Database and print it to the page.I’m talking about the cool parts of PHP, stuff like making a zip file, PDF files or sending emails.
In this part of out PHP Learning experience we will look at how to send an email. Here is the function:
mail($to, $subject , $message , $additional_headers , $additional_parameters );
In the mail() function you need $to, $subject and $message the rest are optional and here is what each means:
$to; it is the email address that will receive this email from you.
$subject; As it suggests it is the subject of the mail that you are sending.
$message; this is the main message the email that you are sending;
$additional_hearders; these are used so you can define: From, Cc, Bcc. These will be added to the end of the header of the email they are optional but it’s much better to get the email from Admin than from server itself(Try it and you’ll see what I mean). You have to separate each using ‘\r\n’ which means end of this line and go to new line so PHP can separate them. In some PHP installations depend on the settings in php.ini you don’t need to set the from part however in most cases if you do not set it it will give you an error so always set it.
$additional_parameters; You can set parameters for the program that will send the email as command line parameters. In this Tutorial we won’t be looking at these as they are a bit more advanced than a basic PHP training.
So lets send an email:
<?php
$to=”me@example.com”;
$message=”This is an Example Message”;
$subject=”This is a Test subject”;
mail( $to , $subject , $message ); //in some PHP installations will not work as we did not define a from but in most cases it will work
$headers=’From: SomeOneElse@example.com’.”\r\n”
.’Reply-To: me@example.com’.” \r\n”
.’Cc: You@example.com’;
mail($to , $subject , $message , $header);// will send the email to me@example.com and cc it to You@example.com and also Reply-To: me@example.com
?>
You can also send a HTML email by defining the media type:
<?php
$to=”Someone@example.com”;
$subject=”This is my first HTML formated email”;
$message=”
<html>
<header>My First HTML email is here</header>
<body>
<h1>This is the H1 tag title here</h1>
<p>
This is thetest message that I have put in a paragraph.
</p>
</body>
</html>”;//—————-end of the message
$headers = ‘MIME-Version: 1.0′ . “\r\n”. ‘Content-type: text/html; charset=iso-8859-1’ . “\r\n”; // this will set the media type to HTML
$headers = $header. ‘From: Me the Admin <me@example.com>’ . “\r\n”. ‘Cc: You@example.com’ . “\r\n”;
mail($to,$subject,$message,$headers);// will send an HTML formatted email to Someone@example.com, cc that to You@example.com and send it From me@example.com and showing the name “Me the Admin” as the name of the sender.
?>
Now lets make a email form using HTML and PHP:
email.html: This file is the HTML that will show the contact us form to the client just copy the HTML code bellow and paste it in your web page:
<html>
<head>
E-Mail_%28Unix%29″ title=”Mail (Unix)” rel=”wikipedia” target=”_blank”>Mail Form
</head>
<body>
<form name=”form1″ method=”post” action=”mail.php”>
<table border=”0″>
<tr>
<td>
<label for=”to”>To:</label>
</td>
<td>
<input name=”to” type=”text” id=”to” size=”80″>
</td>
</tr>
<tr>
<td>
<label for=”from”>From:</label>
</td>
<td>
<input name=”from” type=”text” id=”from” size=”80″>
</td>
</tr>
<tr>
<td>
<label for=”subject”>Subject:</label>
</td>
<td>
<input name=”subject” type=”text” id=”subject” size=”80″>
</td>
</tr>
<tr>
<td>
<label for=”Message”>Message:</label>
</td>
<td>
<textarea name=”Message” cols=”80″ rows=”15″></textarea>
</td>
</tr>
<tr>
<td>
<input name=”submit” type=”submit” value=”Send Email”>
</td>
<td>
<input name=”reset” type=”reset” value=”Clear Form”>
</td>
</tr>
</table>
</form>
</body>
</html>mail.php: This part is the PHP code that will validate the data and then send the email. Copy the code bellow and paste in a file called mail.php in the same folder as the email.html on your PHP server:
<?php
// ———————————————————-get all the information
$to=$_POST[‘to’];
$from=$_POST[‘from’];
$message=$_POST[‘Message’];
$header = ‘MIME-Version: 1.0’ . “\r\n”
. ‘Content-type: text/html; charset=iso-8859-1’ . “\r\n”
. ‘From: ‘.$from . “\r\n”;
// ———————————————————-validate the emails
if(!isset($_POST[‘to’]) or !isset($_POST[‘from’]) or !isset($_POST[‘Message’])){// checking to see if all fields are fille in or not
echo “Please use the back button to fill<b>All the fields</b>. “;
die;//ends the script if any of the fields are empty.
}
if(!filter_var($to, FILTER_VALIDATE_EMAIL)){// making sure $to is a valid email
echo “$to is not a valid email. <br /> Please use the back button to go back to the form.”;// showing an error message
die;// ends the script
}if(!filter_var($from, FILTER_VALIDATE_EMAIL)){// making sure $from is a valid email
echo “$from is not a valid email. <br /> Please use the back button to go back to the form.”;// showing an error message
die;// ends the script
}// ———————————————————-send the email
mail($to,$subject,$message,$header);
?>
To send HTML mail in PHP, you need to use some additional headers. Below is the code to send HTML mail in PHP.
$to = “recipient@domain.com“;
$subject= “Subject of email”;
$message= “This is HTML mail.”;
$fromName = “Name of the sender”;
$fromEmail = “sender@domain.com“;
// To send HTML mail, the Content-type header must be set
$headers = “MIME-Version: 1.0″ . “
”;
$headers .= “Content-type: text/html; charset=iso-8859-1″ . “
”;
// Additional headers
$headers .=”From: $fromName ”.”
”;
//Mail function
mail($to, $subject, $message, $headers);
Thanks for that Aneeq.
Great post. Found a simple working tutorial plus demo at http://goo.gl/34r5n