Home arrow PHP arrow Page 4 - Working with the Email Class in Code Igniter

Sending email messages via an HTML form - PHP

Unless youíve already built a PHP toolkit that lets you develop web applications by reusing its components, the best way to create complex PHP programs quickly is by means of a third-party framework, such as Code Igniter (http://codegniter.com). This package will let you build robust object-based PHP applications in literally minutes, thanks to its extremely friendly interface. So, if youíre interested in learning how to put this framework to work for you, start reading this article now!

TABLE OF CONTENTS:
  1. Working with the Email Class in Code Igniter
  2. Sending email with Code Igniter
  3. Defining a simple view file
  4. Sending email messages via an HTML form
By: Alejandro Gervasio
Rating: starstarstarstarstar / 7
October 01, 2008

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Essentially, the PHP application that I plan to build now will perform really basic tasks, such as sending email by utilizing an HTML form. However, the most interesting facet in developing such a program is that Iím going to combine the functionality of two classes of Code Igniter: the one that sends email, and another one that validates input data.

In addition, Iím going to use the ďurlĒ and ďformĒ helper functions that you learned in previous tutorials to dynamically generate certain links. So, having explained how this sample email application is going to work, itís time to create its first building block, the controller class, whose signature is shown below:   

class Email extends Controller {

function Email(){

// load controller parent

parent::Controller();

// load 'url' helper

$this->load->helper('url');

// load 'form' helper

$this->load->helper('form');

// load 'validation' class

$this->load->library('validation');

// load 'email' class

$this->load->library('email');

}

function index(){

// set validation rules

$rules['firstname']="required|min_length[6]|max_length[15]";

$rules['lastname']="required|min_length[6]|max_length[15]";

$rules['email']="required|valid_email";

$this->validation->set_rules($rules);

// set values to repopulate fields

$fields['firstname']='First Name';

$fields['lastname']= 'Last Name';

$fields['email']='Email Address';

$this->validation->set_fields($fields);

// check if user form has been submitted properly

if ($this->validation->run()==FALSE){

// redisplay user form and repopulate fields

$this->load->view('form_view');

}

// display confirmation web page and send email

else{

// set email class settings

$this->email->from($_POST['email'],$_POST['firstname']. ' '.$_POST['lastname']);

$this->email->to('you@domain.com');

$this->email->cc('migirlfriend@domain.com');

$this->email->bcc('myothergirlfriend@domain.com');

$this->email->subject($_POST['subject']);

$this->email->message($_POST['message']);

$data['title']='Sending email...';

$data['header']='Sending email now...';

$data['message']=$this->email->send()?'Message was sent successfully!':'Error sending email!';

$this->load->view('email_view.php',$data);

}

}

}


Actually, I donít wan to be excessively optimistic, but in this case the logic implemented by the above ďEmailĒ controller is very easy to grasp. As you can see, its constructor begins loading all of the source classes and functions that will be required later on, and this naturally includes all of the classes and helpers that were mentioned previously.

Now, focusing on the implementation of the ďindex()Ē method, itís clear to see that it first sets the validation rules that will be applied to the data entered in the HTML form, then validates this incoming information, and finally submits it via email, assuming that itís been filled in correctly. Otherwise, the online form will be redisplayed, indicating which entries should be corrected.

As youíll possibly agree with me, understanding how the above controller class work isnít something that will make you scratch your head, is it? However, thereís still one step that remains undone if we want to get this email application working as expected.

Yes, you guessed right! In this case, itís necessary to create the view file that informs users that the HTML form has been submitted successfully. We also need to code the online form that lets them enter their personal information.

So, taking into account those last requirements, hereís the signature of these simple view files:

(definition of 'email_view.php' file)


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<title><?php echo $title;?>title>

</head>

<body>

<h1><?php echo $header;?></h1>

<p><?php echo $message;?></p>

<p><?php echo anchor('email','Go and submit the form again');?></p>

</form>

</body>

</html>



(definition of 'form_view.php' file)


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<title>Entering user data</title>

<head>

<body>

<h1>Entering user data</h1>

<?php echo $this->validation->error_string;?>

<?php echo form_open('email');?>

<p>First Name <input type="text" name="firstname" value="<?php echo $this->validation->firstname;?>" size="50" /></p>

<p>Last Name <input type="text" name="lastname" value="<?php echo $this->validation->lastname;?>" size="50" /></p>

<p>Email <input type="text" name="email" value="<?php echo $this->validation->email;?>" size="50" /></p>

<p><input type="submit" value="Send Data" /></p>

</form>

</body>

</html>


Here you have it. Assuming that the controller class and the respective view files have been saved to the correct destination folders, the previous email application now should work like a charm. Of course, as I explained in preceding articles, you can test it by typing into your browse the following URL:


http://localhost/codeigniter/index.php/email/


And with this last hands-on example, Iím finishing this overview of building an email application with Code Igniter. As always, feel free to tweak the entirety of the code samples included in this article, so you can improve your skills in using this powerful PHP framework.

Final thoughts

In this eighth part of the series, I explained how to build a web application with Code Igniter that permits users to send email via a simple HTML form. Although this example may seem pretty simplistic, it hopefully will be useful enough to demonstrate how to combine several classes and helpers into one single program.

The last article will be the fruit of the cake, since Iíll be showing you how to use Code Igniter to develop a content management system that will permit you to insert, update and delete information about a collection of movies.

Yes, this time weíre going to Hollywood, so donít miss the last article!



 
 
>>> More PHP Articles          >>> More By Alejandro Gervasio
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: