How to send email with PHP

Many web applications often require the ability to automatically send out notifications through email to members or site owners.  

PHP makes it very easy to achive this using the built-in mail function as demonstrated in the code below:

<?php

// BELOW WE SET THE TO: FIELD
// This is where the email message will be sent to.

$to = "youremail@somedomain.com";

// BELOW WE SET THE SUBJECT AND MESSAGE
// Pretty self explanatory.  These are the Subject
// and Message of the email you want to send out.

$subject = "Your Email Subject";
$message = "Write your email message text in here.";

// BELOW WE SETUP THE HEADER OF THE EMAIL MESSAGE.

$headers = "From: TypeYourName";
$headers .= "Reply-To:" 'test@yourdomain.com'."\r\n";
$headers .= "Return-Path:" 'test@yourdomain.com'. "\r\n";
$headers .= "X-Mailer: PHP5\n";
$headers .= 'MIME-Version: 1.0' . "\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
mail($to,$subject,$message,$headers);
?>

 
That’s it, it is that simple.

For advanced PHP programmers, you can easily replace the subject, message, to, from fields and other portions of the above code to pull the information from a database in order to send multiple emails or more customized messages.

For now we just want to cover the basics and might add another post at a later time to expand deeper into what can be done using the PHP mail capabilities.



Related Posts


No related posts.



This entry was posted in Tutorials. Bookmark the permalink.

Get a Trackback link

No Comments Yet

You can be the first to comment!

Leave a comment