Simple SMTP Mailer

By Louis Stoltz aka "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 Tue, Apr 10th 2007, 14:02 by Diona Kidd

Comment

2 Thank you

Thanks for a great tutorial.
I'll definitely be trying this out today.
posted Sat, Jun 23rd 2007, 17:04 by Antonie Potgieter

Comment

3 That was fast

Tnx just what I need it!
posted Sun, Jul 22nd 2007, 05:15 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 Wed, Mar 26th 2008, 01:47 by Hanibaal Diab

Login to Submit a Comment