Insane Visions - Sending Mail with PHP Tutorial :: Premium PHP Scripts - AdaptCMS, AdaptBB
Sending Mail with PHP at Dec 26, 07 - 11:01 pm

Views: 4,543 Type: PHP Experience Level: Beginner
Despite what you may think, sending mail is a widely used practice in most scripts. Some use other methods, but the simple PHP mail() function is safe, reliable, easy to use and very useful.
Below we'll show you step-by-step different ways to use the mail() function. Including sending a basic e-mail with sender, receiver, subject and body, an e-mail with a few receivers and then a full blown example with headers, HTML e-mail and the works.
Simple E-Mail
<?php
$ to = "billgates@yahoo.com";
$ subject = "Hey! What's up?";
$ message = wordwrap("This is an example message", 70);
mail($ to, $ subject, $ message);
?>
Above we set a "to" address (the receiver), a subject and a message. Simple, isn't it? Next we move onto using some basic headers with this mail, as well as using wordwrap() to make the email look nice. :)
E-Mail Headers
<?php
$ to = "billgates@yahoo.com";
$ subject = "Hey! What's up?";
$ message = wordwrap("This is an example message", 70);
$ headers = "MIME-Version: 1.0";
$ headers .= "Content-type: text/html; charset=iso-8859-1";
$ headers .= "From: Charlie Page <webmaster@sitename.com>";
$ headers .= "Reply-To: webmaster@sitename.com";
mail($ to, $ subject, $ message, $ headers);
?>
The headers shown above are pretty simple. Setting a MIME version as well as setting it to to content-type HTML, used for HTML emails (that's next) and then putting the from and reply-to. You can set one, some or none of those, it's up to you. Now for the grandaddy, sending an HTML email along with a CC, BCC and multiple receivers.
Full HTML E-Mail
<?php
$ to = "billgates@yahoo.com, stevejobs@microsoft.com,
webmaster@insanevisions.com";
$ subject = "Hey! What's up?";
$ message = "
<html>
<head>
<title>Hey, whats up?!</title>
</head>
<body>
<b>Here is some bold text</b><br><br>
<table><tr><td>Table!</td></tr></table>
</body>
</html>";
$ headers = "MIME-Version: 1.0";
$ headers .= "Content-type: text/html; charset=iso-8859-1";
$ headers .= "To: Bill Gates <billgates@yahoo.com>,
Steve Jobs <stevejobs@microsoft.com>,
Charlie Page <webmaster@insanevisions.com>";
$ headers .= "From: Charlie Page <webmaster@sitename.com>";
$ headers .= "Reply-To: webmaster@sitename.com";
$ headers .= "Cc: heythere@example.com";
$ headers .= "Bcc: dummyname@example.com";
mail($ to, $ subject, wordwrap($ message, 70), $ headers);
?>
CC and BCC is as simple as putting it into the headers variable and with the content-type set to HTML, you can use HTML in the message. For multiple recipients, simple seperate with a comma!
Conclusion
This wraps up another tutorial, this one showing and explaining the usage of the PHP mail() function. With this function you can quickly, easily and securely send an email. From a newsletter, to order confirmation, the usage is almost endless and is an important function to know.
Download: 
Rating:    
|