Simple SMTP Mailer

By Louis Stoltz (Enchy)
This is a simple tutorial on how to send a plain text message via email.
Firstly the Mailer Class
This goes in /cake/app/controllers/components/mailer.php

Model Class:

Download code <?php 
class MailerComponent extends Object
{

var 
$to              = array();
var 
$from            'info@strutstuff.co.za';
var 
$fromname        'Strutstuff.co.za';
var 
$Subject         null;
var 
$Message         null;

function 
socketmail() {

        
//ini_set("smtp_port", "25");  // Optional
       //ini_set("SMTP", "smtp.yoursite.com"); // Optional

        
ini_set("sendmail_from"$this->from);

        
$connect fsockopen(ini_get("SMTP"), ini_get("smtp_port"), $errno$errstr30) or die("Could not talk to the sendmail server!");

        
$rcv fgets($connect1024);

      
fputs($connect"HELO {$_SERVER['SERVER_NAME']}\r\n");
      
        
$rcv .= fgets($connect1024);

  while (list(
$toKey$toValue) = each($this->to)) {

      
fputs($connect"MAIL FROM:$this->from\r\n");

        
$rcv fgets($connect1024);

      
fputs($connect"RCPT TO:$toValue\r\n");
        
$rcv .= fgets($connect1024);

      
fputs($connect"DATA\r\n");
        
$rcv .= fgets($connect1024);

          
   
fputs($connect"Subject: $this->Subject\r\n");
   
fputs($connect"From: $this->fromname <$this->from>\r\n");
   
fputs($connect"To: $toKey  <".$toValue.">\r\n");
   
fputs($connect"X-Sender: <$this->from>\r\n");
   
fputs($connect"Return-Path: <$this->from>\r\n");
   
fputs($connect"Errors-To: <$this->from>\r\n");
   
fputs($connect"X-Mailer: PHP\r\n");
   
fputs($connect"X-Priority: 3\r\n");
   
fputs($connect"Content-Type: text/plain; charset=iso-8859-1\r\n");
   
fputs($connect"\r\n");
   
fputs($connectstripslashes($this->Message)." \r\n");
   
fputs($connect".\r\n");

     
$rcv .= fgets($connect1024);

   
fputs($connect"RSET\r\n");
     
$rcv .= fgets($connect1024);


  }

   
fputs ($connect"QUIT\r\n");
     
$rcv .= fgets ($connect1024);

   
fclose($connect);
   
ini_restore("sendmail_from");

    }

function 
AddAddress($name "",$address ) {

        
$cur count($this->to);
        
$this->to["$name"] = trim($address);

    }

}
?>


Then all you need to do is set these in your controller:

Model Class:

Download code <?php 
var $components = array ('Mailer'); // 'Mailer','comp2'  if Multiple

// Set up mail
    
$this->Mailer->Subject "Nice Subject";
    
$this->Mailer->Message "That was easy";
    
$this->Mailer->AddAddress("yourname","youremail");
    
$this->Mailer->socketmail();

?>

Let me know what you think.

 

Comments 306

CakePHP Team Comments Author Comments
 

Comment

1 That was easy

That was real easy! Thanks! No problems here. 1.5 minute setup.
Posted Apr 10, 2007 by Diona Kidd
 

Comment

2 Thank you

Thanks for a great tutorial.
I'll definitely be trying this out today.
Posted Jun 23, 2007 by Antonie Potgieter
 

Comment

3 That was fast

Tnx just what I need it!
Posted Jul 22, 2007 by Lupo Sette
 

Comment

4 Cool that was handy little component

thanks for the component it definily save me time..

for those who might need authentication simply after this line of code
     
fputs($connect, "HELO {$_SERVER['SERVER_NAME']}\r\n"); 
add the following:

    // authentication
      fputs($connect, "auth login\r\n");
      $rcv .=fgets($connect,256);
      
      fputs($connect, base64_encode("smtp_user@mail.server.com")."\r\n");
      $rcv .=fgets($connect,256);      
      
      fputs($connect, base64_encode("password")."\r\n");
      $rcv .=fgets($connect,256);    
of course change the smtp_user@mail.server.com and password to an authenticated user required by your server...

you can be better then me by placing them as variables on top of your class.
Posted Mar 26, 2008 by Hanibaal Diab
 

Comment

5 Thank you

After 7 hours of fruitless tries with other tutorials and with the build-in email component, this ist the first code which sent an email for me.

Thank you!

Anja
Posted May 15, 2008 by Anja
 

Question

6 SMTP Help

I have purchase a pre made site and it sends user registrations, password reminders and news items to it's useres but it will not send the information via smtp. Here is the array - why isn't working or is a server specific issue? I am on a linux machine.

),

'Email' => array(
'delivery' => 'smtp',
'sendAs' => 'html',
'host' => 'mail.whatever.com',
'port' => 25,
'timeout' => 60,
'username' => 'admin@whatever.com',
'password' => 'mypassword'
),
Posted Mar 14, 2009 by Clark Jones
 

Comment

7 debug

You can debug it by echoing this:
$rcv .= fgets ($connect, 1024);
echo $rcv;
See what it says.
Posted Apr 23, 2009 by Louis Stoltz