April 23, 2008

PHP E-mail with Pear

Filed under: The Internet Author: Cody

The php mail() function is wonderful in its simplicity but as anyone starting out in php soon discovers, that simplicity has a price - you have no ability to use more advanced functions such as SMTP. For anyone who sends out newsletters and gets half of them back from spam filters you’ll know this can be a problem.

This is where an email class such as Pear can help out; so on to the next problem. - What if you are building an application to put on a third party server where Pear mail isn’t installed? All of the tutorials on the Net tell you to go and install it and then re-boot your server; this is all well and good unless the on/off button is 6000 miles away on a shared host that is not particularly keen on turning it off for you.

Fortunately you don’t have to go to those lengths and you can incorporate all the Pear classes you need with a simple call to php ini_set() in your script.

The classes:

Mail:

This is the main mail class and it is needed for all the classes below. It can be downloaded at http://pear.php.net/package/Mail

Once downloaded extract it to temporary folder and rename the main folder to ‘mail’. Upload the folder and all the subfolders in it to the same directory on your server that contains your script for sending emails.

Net_SMTP:

This is the class you will need if you want to send e-mails via smtp, download at http://pear.php.net/package/Net_SMTP

Once downloaded and extracted you will need to change the main folder name to ‘Net’ (make sure you use a capital ‘N’.) Upload it, and put it inside the ../mail/Mail folder as shown in Fig. 1. Once again leaving all sub folders intact.

Path treeFIG. 1

Net_Socket:

This class is required to connect to smtp servers, download at; http://pear.php.net/package/Net_Socket

Once downloaded and extracted you will need to change the main folder name to ‘Net’ (make sure you use a capital ‘N’.) Upload it, and put it inside the ../mail/Mail/Net folder as shown in Fig. 2. Once again leaving all sub folders intact.

Path treeFIG. 2.

Now you have the basic classes in place to send smtp emails all you need now is the code to use it.

Create a new php file called ‘mail.inc.php’ and insert the following code;

<?php
ini_set(
“include_path”, (
“public_html/yourdomain.tld/mail/” .
PATH_SEPARATOR .
ini_get(”include_path”)
)
);

require_once ‘mail/Mail.php’;
require_once ‘mail/mime.php’;

$host = “mail.yourdomain.tld“;
$username = “youraddress@yourdomain.tld“;
$password = “yourpassword“;

?>

You will need to change the parts in red to the settings for the email account you wish to use. Save the file and upload it to the root of your server.

Finally, use the following code in your script that sends emails.

//send the email

require_once (’mail.inc.php’);

$message = new Mail_mime();
//Create a plain text part for the email

//Single new line character

$newline = “\n”;

/Double new line characters

$doubleNewline = “\n\n”;

$text = ‘Testing new email class‘.$doubleNewline.
This is some text to test the new classes‘.$doubleNewline.
Name: Mr Tester‘.$newline.
Hello! This is my test email‘.$doubleNewline;

//Create an html version of the email

$html = ‘<html>
<head>
<title>Testing new email class</title>
</head>
<body>
<h1><center>Testing new email class</center></h1>
<p>This is some text to test the new classes</p>
<p>Name: Mr Tester</p>
<p>Hello! This is my test email</p>

</body>
</html>’;

$message->setTXTBody($text);
$message->setHTMLBody($html);
$body = $message->get();
$from = “My Script<noreply@mydomain.tld>”;
$to = “Someone <address@someplace.com>”;
$subject = “Test Email“;

$extraheaders = array(’From’ => $from,
‘To’ => $to,
‘Subject’ => $subject);
$headers = $message->headers($extraheaders);

$smtp = Mail::factory(’smtp’,
array (’host’ => $host,
‘auth’ => true,
‘username’ => $username,
‘password’ => $password));

$mail = $smtp->send($to, $headers, $body);

if (PEAR::isError($mail)) {
echo(”<p>” . $mail->getMessage() . “</p>”);
} else {
echo(”<p>Message successfully sent!</p>”);
}

Once again the code in red should be configured to suit your purposes.

And that’s it!

Share This Blog: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Netscape
  • Reddit
  • Slashdot
  • StumbleUpon
  • Technorati

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URL

Leave a comment

Powered by WordPress