Advanced PHP for Beginners

Mailer Class

Sends html, includes attachments, embeds images, and uses an smtp server - the mailman never had it so easy.

Download Now!
Mailer ( [ $from [, $debug ]] )
smtp ( [ $server [, $port [, $username [, $password ]]]] )
attach ( $file_name, $file_path )
send ( $to, $subject, $text [, $html [, $images ]] )
close ( )
debug ( )
An Advanced PHP for Beginners Example
The Emails Output
object Mailer ( [ mixed $from [, bool $debug ]] )top

Creates an email object.

$from:
The email address of the sender. To include your name make it an array. The default is your servers configuration 'sendmail_from' value.
eg. $from['Your Name'] = 'your@email.com';
eg. array('Your Name'=>'your@email.com')
$debug:
Set to true if you're just testing the waters, and want to see what is going on.
Example:
<?php

$email 
= new Mailer (array('Yours Truly'=>'best@website.com'), true);

?>
bool smtp ( [ string $server= 'localhost' [, string $port= 25 [, string $username [, string $password ]]]] )top

Connects you to your SMTP server. Once you're in, you can send as many emails as you like without connecting over and over again. SMTP is much more reliable than mail(), and if this method returns true you can be confident that your email was sent and will be received. If you don't use this, or if you don't check to make sure that this method worked, then your email will be sent using PHP's native mail() function.

$server:
Your outgoing (SMTP) server. The default is 'localhost'.
$port:
The port to connect to. The default is 25.
$username:
Your username.
$password:
Your password.
Returns:
Returns true if successful, false if not.
Example:
<?php

if ($email->smtp('mail.yourwebsite.com'25)) { // Proceed . . .

?>
attach ( string $file_name, string $file_path )top

This will include an attachment to your email, and you can call this method as many times as you like.

$file_name:
The name of the file.
$file_path:
The full server path to the attachments directory. Don't forget the trailing slash.
Example:
<?php

$email
->attach ('report.pdf''/base/directory/mysite.com/include/attachments/');

?>
bool send ( mixed $to, string $subject, string $text [, string $html [, string $images ]] )top

Just as advertised. When you include the directory to the images contained in your html, it will embed them in your email. Just keep calling this method for every email you want to send.

$to:
The email address of the receiver. To include their name make it an array:
$to['Their Name'] = 'receiver@email.com';
$subject:
The subject of the email.
$text:
The body of your message. You don't need to worry about newlines ("\n\n") - it will take either newlines, or <br /> tags.
$html:
To display an html message. The $text string above is not needed if you declare this, but it is recommended for good form anyways.
$images:
The full server path to where the images in your email reside. Don't forget the trailing slash.
Returns:
Returns true if successful, false if not.
Example:
<?php

$to 
'some@email.com';
$subject 'Friendly Reminder';
$text 'Tell your friends about me!';
$page = new page ('Friendly Reminder');
$html $page->display ('<img src="logo.gif" /><br />Tell your friends about me!');
unset (
$page);

$email->send($to$subject$text$html'/base/directory/mysite.com/include/images/');

?>
string close ( )top

This will close your open SMTP connection, and you should call this method after you $email->send() everything.

Example:
<?php

$email
->close();

?>
array debug ( )top

Did some of your emails not get sent? Want to know what is going on so you can figure out what to do about it? This method will send you an informational array of all your interactions with SMTP, the expected responses in parentheses, and the actual response you were given.

Returns:
An array of SMTP status codes.
Example:
<?php

trigger_error
(serialize($email->debug());

?>
An Advanced PHP for Beginners Example:top
<?php

$from
['Yours Truly'] = 'best@website.com';

$email = new Mailer ($fromtrue); // We're not going to actually send this email, just display the output

$to 'your@email.com';
$subject 'Friendly Reminder';
$text 'Tell your friends about me!';
$page = new Page ('Friendly Reminder');
$html $page->display ('Tell your friends about me!');
unset (
$page);

$email->send($to$subject$text$html);
unset (
$email);


/* A more likely scenario
$email = new Mailer ('my@email.com');
if ($email->smtp()) {
  foreach ($address as $to) {
    if ($email->send($to, $subject, $text, $html)) {
      // Update your database so you don't have to try again
    }
  }
  $email->close();
} else {
  trigger_error(serialize($email->debug());
}
unset ($email);
*/

?>
The Emails Output:top
Date: Sun, 05 Sep 2010 08:30:19 +0000
From: Yours Truly <best@website.com>
Return-Path: <best@website.com>
MIME-Version: 1.0
Content-Type: multipart/alternative; boundary="alternative-665c710b5bacf3e3ca17ecaf00cea79c90d33f0e6393"
Message-ID: <-www.advancedphpforbeginners.com-665c710b5bacf3e3ca17ecaf00cea79c90d33f0e6393>

--alternative-665c710b5bacf3e3ca17ecaf00cea79c90d33f0e6393
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: 7bit

Tell your friends about me!

--alternative-665c710b5bacf3e3ca17ecaf00cea79c90d33f0e6393
Content-Type: text/html; charset=iso-8859-1
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/htm=
l4/strict.dtd">
<html>
<head>
  <title>Friendly Reminder</title>
  <meta name=3D"description" content=3D"Friendly Reminder">
  <meta name=3D"keywords" content=3D"Friendly Reminder">
  <meta http-equiv=3D"content-type" content=3D"text/html; charset=3Diso-885=
9-1">
</head>
<body>
  Tell your friends about me!
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                                                           =
                                           </body>
</html>

--alternative-665c710b5bacf3e3ca17ecaf00cea79c90d33f0e6393--


-- End --


Bookmark This Page
Home
Download
Contact Us