The Coder

Mail / Mime

Original posted by: Admin on 27-02-2007

Pear is a wonderful PHP package that come preinstalled on most servers, however if your server does not contain the relevant pear packages you can always download them from the pear site.

This tutorial is going to teach you how to use the Mail & Mime packages so you can send HTML formated emails & SMTP emails.

If you don't have Pear installed you will need to download Pear.php, the Mail package and the Mime package.

Lets get started by creating a simple send_email function with the relevant variables.


function send_email($send_to, $reply_email, $email_message, $email_subject) {
include 'Mail.php';
include 'Mail/mime.php';

//Setup the Mime area
$mime_package = new Mail_mime("\n");
$mime_package->setHTMLBody($email_message);
$mime_body = $mime_package->get();

//Setup the mime header variables
$mime_header = $mime_package->headers(array(
'To' => $send_to,
'Subject' => $email_subject,
'Reply-To' => $reply_email,
'From' => 'Your Name <email@domain.com>',
'Date' => date("r", time())
));

//Now lets setup the SMTP part
//We need to supply the factory with the relevant SMTP host, username and password
$smtp = Mail::factory('smtp', array(
'host' => 'mail.domain.com',
'username' => 'smtp_username',
'password' => 'smtp_password',
'auth' => true
));

//Ok the mail is ready to be sent, lets send it...
$sent_email = $smtp->send($send_to, $mime_header, $mime_body);

//Message has been sent, lets check if it was successful or not

if(PEAR::isError($sent_email)) {
echo $mime_package->getMessage();
} else {
return true;
}
}


To make this function work correctly you will need to have a SMTP email account. This is a good function because it will help protect your emails from entering peoples spam cans.

Play around with the $email_message variable and include some html. You can create a nice email template that will display correctly in most major email systems.


//Simple HTML Message
$email_message = '<html><body><h1>Heading</h1><p>This is my cool looking template</p></body></html>';




Login to make a comment 0 Comments Found