Sends html, includes attachments, embeds images, and uses an smtp server - the mailman never had it so easy.
| 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.
<?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.
<?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.
<?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.
<?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.
<?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.
<?php
trigger_error(serialize($email->debug());
?>
| An Advanced PHP for Beginners Example: | top |
<?php
$from['Yours Truly'] = 'best@website.com';
$email = new Mailer ($from, true); // 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!
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
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