March 1, 2010 in PHP, Tutorials by Nathan Lampe

Learning how to use PHP’s mail functions with OOP can make sending emails a lot easier and faster when building web applications. For this tutorial you will need a server with php and email. You can use this class to process contact forms, send automatic emails, and any other place where you need to use the php mail functions.

For this tutorial we will create two files

  • index.php
  • emailer.class.php

Download the source files:

Download Source Files From Creative Armory

Download Source Files From Upload-This.com Mirror

The first file we will make is index.php and will instantiate our class, define our email’s details and then send the email.

First we will include the class and define who is sending the email and what their name is.

1
2
3
4
5
6
require_once('emailer.class.php');
$sender = "admin@creativearmory.com";
$name = "Creative Armory";

Next we will instantiate our class

1
$emailer = new Emailer($sender,$name);

Now we just need to define who we are sending the email to, the subject, and the body message. Once we do that we just send the email with our emailer class.

1
2
3
4
5
6
7
8
$to = "someone@creativearmory.com";
$subject = "Creative Armory Contact Form";
$body = "Hello, \n how are you doing?";
$emailer->sendEmail($to,$subject,$body);

Here is index.php in it’s entirety.

index.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<?php
require_once('emailer.class.php');
//Define Sender Details

$sender = "admin@creativearmory.com";
$name = "Creative Armory";
//Instatiate Emailer Class

$emailer = new Emailer($sender,$name);
//Define Email Details

$to = "someone@creativearmory.com";
$subject = "Creative Armory Emailer";
$body = "Hello, \n how are you doing?";
//Send the Email

$emailer->sendEmail($to,$subject,$body);
?>

For this next part we will be creating our Emailer class which will send out our email. To start this we will name our class ‘Emailer’, define our from and name variables as well as create our constructor function with our sender and name.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Emailer{
private $from;
private $name;
function __construct($sender,$name){
$this-&gt;sender = $sender;
$this-&gt;name = $name;
}
}

Next we will create our function that will actually be doing the mailing. For this we will create our MIME headers, defining who is sending the email and who to send to. We will use the PHP mail function to then actually send the email.

Here is the Emailer class in it’s entirety.

emailer.class.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<?php
class Emailer{
private $from;
private $name;
function __construct($sender,$name){
$this->sender = $sender;
$this->name = $name;
}
function sendEmail($to,$subject,$body){
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1'."\r\n";
$headers = "From: ". $this->name . " <" . $this->sender . ">\r\n";
ini_set($this->name, $this->sender);
mail($to, $subject, $body, $headers);
}
}
?>

That’s it! You can expand on this class to allow for attachments, additional headers, etc.

Download the source files:

Download Source Files From Creative Armory