Simple SMTP Mailer

This article is also available in the following languages:
By 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:

<?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:

<?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

  • Posted 12/25/09 06:19:37 AM
    Thanks for tutorial. I just downloaded the Cake 1.2.5 but there is nothing in /cake/app/controllers/components/mailer.php

    What is the problem?

    Thank you...
  • Posted 04/23/09 07:41:12 AM
    You can debug it by echoing this:
    $rcv .= fgets ($connect, 1024);
    echo $rcv;
    See what it says.
  • Posted 03/14/09 05:30:28 AM
    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 05/15/08 02:40:06 AM
    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 03/26/08 01:47:15 AM
    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 07/22/07 05:15:20 AM
    Tnx just what I need it!
  • Posted 06/23/07 05:04:46 PM
    Thanks for a great tutorial.
    I'll definitely be trying this out today.
  • Posted 04/10/07 02:02:07 PM
    That was real easy! Thanks! No problems here. 1.5 minute setup.

Comments are closed for articles over a year old