Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Standard view
Full view
of .
Look up keyword
Like this
0 of .
Results for:
No results containing your search query
P. 1
Advanced Email in PHP

Advanced Email in PHP



|Views: 653 |Likes:
Published by Abhilash V Pillai
A detailed tutorial about configuring and using PHP mail() function effectively. Code snippets on how to send plain text mail - html mail - css mail and mail with file attachments.
A detailed tutorial about configuring and using PHP mail() function effectively. Code snippets on how to send plain text mail - html mail - css mail and mail with file attachments.

More info:

Published by: Abhilash V Pillai on Jan 07, 2009
Copyright:Attribution Non-commercial


Read on Scribd mobile: iPhone, iPad and Android.
download as DOC, PDF, TXT or read online from Scribd
See more
See less





Advanced email in PHP
I wish I could remember the very first email message I ever sent. Unfortunately, the truth is thatI've come to take email for granted; for instance, I easily send more email messages than I makephone calls. Because the novelty has long since worn off, the memory of that first email is as lost tome as my first phone call; but I doubt my sense of wonder was any less complete at the time. Tosomeone who has never seen email in action before, that first message can be magical.
In this article, I'll try to recapture some of that magic for those of you who have never created a Web sitethat sends email messages. We'll see how the PHPserver-side scripting language may be set up to send email, and explore how to send complex message types such asHTMLemail or emails with fileattachments.
This article was written back when things like HTML email and file attachments were a lot moredifficult to do in PHP than they are today. Before you dive into the from-scratch solutions presented inthis article, you might consider investigatingPHPMailer , a free library for PHP that provides all of thesefeatures with minimal hair-pulling and gnashing of teeth.
 PHP Email Setup
Before we can send email with PHP, we need to set it up to do so, just as you need to set up your email program before it can send messages. Configuration for sending email in PHP is done with the
file, so open up your Web server's
in whichever editor you normally use.If you don't run your own server, but instead have a PHP-equipped Web host, you can safely assume thateverything in this section has been done for you, and skip ahead.In the section entitled
[mail function]
in the
file, you'll find three settings:
, and
. If your server runs on a Windows machine, you'll want to set the
option to point to your SMTP server (or your ISP's SMTP server, if you're setting up PHP on your home machine). If instead you're setting up PHP on a Linux(or other Unix-based OS) server, you'll want to set the
option to point to the
program on your server, passing it the
option. You can use the SMTP option in Linux instead if you don't have
set up.In either case, you'll want to set the
option to your email address, or whichever addressyou'd like to appear as the default 'from' address for emails sent from PHP scripts.Here's how the section might look on a typical Windows server, or on a Linux server without sendmail:
[mail function]; Setup for Windows systemsSMTP =
 sendmail_from =
And here's how it might look on a Linux server with sendmail:
[mail function]; Setup for Linux systemssendmail_path = /usr/sbin/sendmail -tsendmail_from = me@myserver.com
With those settings in place, restart your Web server and you're ready to go!
 Sending Plain Email 
It doesn't come much easier than the procedure to send plain text email inPHP. In fact, you can do it in just one line in a PHP script:
<?phpmail('recipient@some.net', 'Subject', 'Your message here.');?>
The above line would send an email message to
with '
' as the subject lineand '
Your message here.
' as the message body. As you can see, PHP's
function makes sendingemail exceedingly simple, but there are a few advanced tricks we can use to get more out of this simplefunction.First of all, if the mail system you configured in your 
file rejects a message you try to send (for example, if the 'to' address is not a valid email address), this function will display an error message in theuser's browser. Like most PHP functions, however, error messages may be suppressed by preceding thefunction name with an @ symbol. Combine this with the fact that the mail function returns either true ofalse depending on whether the email was accepted by the mail sending system, and you have a formulato send email with appropriate error checking:
<?phpif (@mail($to, $subject, $message)) {echo('<p>Mail sent successfully.</p>');} else {echo('<p>Mail could not be sent.</p>');}?>
 Note that just because an email message could be sent doesn't guarantee it will arrive at its destination. Anemail address can be valid (i.e. correctly formed) but not actually exist. For instance, you can successfullysend a message to
-- that is, the mail function will return true -- but themessage will bounce because no such user exists. PHP provides no built-in means of detecting when thishappens.When you want to send a message to multiple recipients, you can just list their email addresses one after another, separated by commas, in the first parameter. For example:
<?phpmail('recipient1@some.net, recipient2@some.net','An email to two people', 'Message goes here.');?>
That about covers the basics of the
function; now let's really get fancy and explore mail headers andwhat we can do with them!
 HTML Email and Other Headers
So now you can send email from your PHPscripts. How exciting! Although I'm sure you're already drunk with power, would you like to know how to send HTML email too? Of 
you would!To understand HTML email, first you need to understand mail headers. Any given email message is madeup of two parts: the headers and the message body. Here's how a simple message looks when your email program fetches it from your ISP:
Return-Path: <sender@elsewhere.com>Delivered-To: you@some.net Received: ...several lines like this...From: Sender <sender@elsewhere.com>To: You <you@some.net>Subject: A Simple MessageDate: Mon, 11 Feb 2002 16:08:19 -0500Organization: Sender's CompanyX-Mailer: Microsoft Outlook, Build 10.0.2616Hi there! <tap> <tap> Is this thing on?
Everything up to the blank line makes up the headers for this message. In actual fact, most emailmessages will have many more header lines than this; however, to keep our focus I trimmed this exampledown to the bare essentials.As you can see, every line of the headers begins with the name of the header (
, etc.), followed by some value. Most headers are standardized, and have a specific meaning, either to your mail program or to the mail servers that were responsible for getting the message to you. Non-standard headers exist as well, and they all begin with
, a non-standard header, oftenappears to indicate the program that was used to send the message).
If a header's value needs more than one line, additional lines should begin with a space. We'll seean example of this in the next section.As soon as your mail program gets to a blank line, it knows the headers are over and the rest of the emailis the message body, which it should display. In this case, the message body is the last line above.PHP's
function lets you specify your own headers, which it adds to those it generates automaticallyto get your message where it needs to go. For example, the following call to the
function will add an
header to the outgoing message, identifying PHP 4.x as the sending program:
<?phpmail('recipient@some.net', 'Subject', 'Your message here.','X-Mailer: PHP 4.x');?>
This optional fourth parameter is most often used to specify a 'from' address other than the defaultspecified in
. Let's add a From: header to do just that:
<?phpmail('recipient@some.net', 'Subject', 'Your message here.',"From: sender@some.net\nX-Mailer: PHP 4.x");?>
 Note that since headers each appear on a single line, we must separate our two headers with a new linecharacter (
), which means I need to use double quotes around the header string (single quoted stringsdon't recognize special character codes like
).Additional headers also let you assign names to email addresses by specifying them in the format
. Here's our example again, but this time with names "The Sender" and "The Receiver" attachedto the relevant addresses:
<?phpmail('recipient@some.net', 'Subject', 'Your message here.',

Activity (10)

You've already reviewed this. Edit your review.
1 hundred reads
1 thousand reads
Harish Kumar Mandiyal added this note
Thanks Scribd
fatullahdonie liked this
sareeee liked this
misspikun liked this
joko_rich liked this
Shafeez liked this

You're Reading a Free Preview

/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->