In this post a I will show an example of a simple contact form which sends an email to a specified address, done using CakePHP.
The form has 3 fields: name, email address and message.
Firs of all let's see the "view" for the form displaying. It looks like this (app/views/controller_name/contact.thtml):

PHP:
  1. <?php if (isset($succes)){?> // $succes is the variable which is verified to see if email was sent
  2.      <p>The message has been sent.
  3.      <br />Thank you!
  4.      </p> // Message displayed if success
  5. <?php }else{?>
  6.      <form method="post" enctype="multipart/form-data" action=”<?php echo $this->webroot;?>controllername/contact/”>
  7.      </form>
  8.  
  9. <?php if (isset($erroare)){?> // Verify if is an error and then display error message
  10.       <p>Please complete all fields!</p>
  11. <?}?>
  12.  
  13. <p>
  14.     Name: &nbsp;&nbsp;&nbsp;&nbsp;<?php echo $html->input(’Page/name’, array(’size’ => ‘25))?>
  15. </p>
  16.  
  17. <p>
  18.     Email: &nbsp;&nbsp;&nbsp;&nbsp;<?php echo $html->input(’Page/email’, array(’size’ => ‘25))?>
  19. </p>
  20.  
  21. <p>
  22.    Message: &nbsp; &nbsp; &nbsp; <?php echo $html->textarea(’Page/message’, array(’cols’ => ‘40′, ‘rows’ => ‘10))?>
  23. </p>
  24.  
  25. <p>
  26.    <?php echo $html->submit(’Send email’)?>
  27. </p>
  28. <p>
  29.    All fields have to be completed!
  30. </p>
  31. </form>
  32.  
  33. <?php }?>

Now we need a small component to verify the email address to be a valid one and also to contain the code necessary for the email sending. The component (app/controllers/components/email.php) is looking like this:

PHP:
  1. <?php
  2.  
  3. class EmailComponent extends Object
  4. {
  5.  
  6.     function email($email, $name, $message)
  7.     {
  8.       $f_mail = "name@domain.com";
  9.       $f_name = "WebMaster domain.com";
  10.       $f_message ="\nName : $name";   // start building the message
  11.       $f_message .="\n\nE-mail : $email";
  12.       $f_message .="\n\nMessage \n : $message";
  13.  
  14.       $headers = "From: www.domain.com <administrator@domain.com>\n";
  15.       $headers .= "X-Sender: <admin @domain.com>\n";
  16.       $headers .= "X-Mailer: PHP\n";
  17.       $headers .="Reply-To:$f_mail\n\n";
  18.      
  19.       $subject = "Message from your website domain.com";
  20.      
  21.       mail("$f_mail", "$subject", "$f_message",$headers); // send the email
  22.  
  23.     }
  24.  
  25.  
  26.     function validMail($email) // this function checks to see if the email supplied by the visitor is a valid one
  27.     {
  28.         $pattern="^[-_.[:alnum:]]+@((([[:alnum:]]|[[:alnum:]][[:alnum:]-]*[[:alnum:]])\.)+(ad|ae|aero|af|ag|ai|al|am|an|ao|aq|ar|arpa|as|at|au|aw|az|ba|bb|bd|be|bf|bg|bh|bi|biz|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|com|coop|cr|cs|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|in|info|int|io|iq|ir|is|it|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|mg|mh|mil|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|museum|mv|mw|mx|my|mz|na|name|nc|ne|net|nf|ng|ni|nl|no|np|nr|nt|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pro|ps|pt|pw|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)|(([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5])\.){3}([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5]))$";
  29. // the regex for an email addres
  30.      
  31.      if(eregi($pattern, $email))
  32.         $ll=1;
  33.      else
  34.         $ll=0;
  35.      return $ll;
  36.     }
  37. }
  38.  
  39. ?>

And now becomes the logic part, the controller (app/controllers/controller_name.php):

PHP:
  1. <?php
  2.  
  3. class NameController extends AppController {
  4.  
  5.     var $uses = null;
  6.     var $components = array('Email');
  7.  
  8.     function contact(){
  9.  
  10.         $this->pageTitle = 'Contact';
  11.  
  12.         if (!empty($this->data))
  13.         {
  14.  
  15.           $email = $this->data['Page']['email'];
  16.           $name = $this->data['Page']['name'];
  17.           $message = $this->data['Page']['message'];
  18.  
  19.           $error = 0;
  20.  
  21.            // start filter vars
  22.           if(strlen(trim($email))<3)
  23.           {
  24.             $error=1;
  25.           }
  26.  
  27.           if(strlen(trim($message))<4)
  28.           {
  29.             $error=1;
  30.           }
  31.  
  32.          // verify email address
  33.           if( (strlen(trim($email))<3)||($this->Email->validMail($email)==0))
  34.           {
  35.             $error=1;
  36.           }
  37.  
  38.           if ($error==0)
  39.           {
  40.  
  41.             $this->Email->email($email, $name, $message)//here is the email sent
  42.             $this->set('succes', 'The message was sent <br />Thank you!');
  43.             // setting "succes" variable for displaying if email was sent.
  44.  
  45.           }else
  46.           {
  47.             $this->set('error', 'Please complete all fields');
  48.             // this is in case of error
  49.           }
  50.  
  51.       }else{
  52.        
  53.       }
  54.  
  55.   }
  56.  
  57. }?>