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

Advanced Email in PHP

Ratings:

4.6

(5)
|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

Availability:

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

11/18/2012

pdf

text

original

 
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.
Note:
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
php.ini
file, so open up your Web server's
php.ini
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
php.ini
file, you'll find three settings:
SMTP
,
sendmail_from
, and
sendmail_path
. If your server runs on a Windows machine, you'll want to set the
SMTP
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
sendmail_path
option to point to the
sendmail
program on your server, passing it the
-t
option. You can use the SMTP option in Linux instead if you don't have
sendmail
set up.In either case, you'll want to set the
sendmail_from
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 =
smtp.my.isp.net
 sendmail_from =
me@myserver.com
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
recipient@some.net
with '
Subject
' as the subject lineand '
Your message here.
' as the message body. As you can see, PHP's
mail
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 
php.ini
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
nonexistant.user@hotmail.com
-- 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
mail
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 
course
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 (
From:
,
To:
,
Subject:
,
Date:
, 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
X-
(e.g.
X-Mailer:
, a non-standard header, oftenappears to indicate the program that was used to send the message).
NOTE:
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
mail
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
mail
function will add an
X-Mailer:
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
php.ini
. 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 (
\n
), which means I need to use double quotes around the header string (single quoted stringsdon't recognize special character codes like
\n
).Additional headers also let you assign names to email addresses by specifying them in the format
name<email>
. 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

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