Articles

PHP Socket based Mail class with CRAM MD5 Authentication

Author: nathacof
Published: Monday 18th of June 2007

As if I didn't have enough email in my life:

So I finally took some time to get my mail program working to send mail using a remote host.

View the source of class.mail.php
Right click on the link and save the file as 'class.mail.php'.

Here you can see example usage:

Plain Text:

require_once('class.mail.php');

$mailer           = new mail();
$mailer->host     = 'mail.example.com';
$mailer->username = 'me@example.com';
$mailer->password = 'myPassword';
$mailer->sender   = 'Anybody@example.com';
$mailer->subject  = 'Testing Mailer Class';
$mailer->rcpt     = array("me@example.com", "support@neranjara.org");
$mailer->body     = "Hello,

This is a test of Nathan Coffield's PHP mailer class using
CRAM-MD5, or plain old LOGIN style authentication to relay through a remote host.

Sweet!
";

if($mailer->send())
{
  echo "Success!";
}
else
{ 
  echo "Failure! Please check [". $mailer->log ."] for details!;
}

HTML Email:

require_once('class.mail.php');

$mailer           = new mail();
$mailer->host     = 'mail.example.com';
$mailer->username = 'me@example.com';
$mailer->password = 'myPassword';
$mailer->sender   = 'Anybody@example.com';
$mailer->subject  = 'Testing Mailer Class';
$mailer->rcpt     = array("me@example.com", "support@neranjara.org");
$mailer-htmlEmail();
$mailer->body     = file_get_contents('http://example.com');

if($mailer->send())
{
  echo "Success!";
}
else
{ 
  echo "Failure! Please check [". $mailer->log ."] for details!;
}

It's important to note that when sending HTML emails that all of your links are absolute. This is necessary because the email client will not be able to access relative URLs.

Security Concerns

As with any mail tool there is extreme potential for abuse. It is important to note that any user supplied input should be validated before being used in any of your code.

Do everyone a favor and read this article!! Email Injection

Article Search

Social Networks