Introduction to the PHP class "MailHub"

Description

On basically all my webprojects I have to send emails to somebody. The users wants to get a new password or I want to send a confirmation or an error report to a specific email address. Of cause you simply could use the mail function of php but as soon as you want to send HTML mails or mails with an attachment it becomes a little bit tricky. Also if you need some dynamic error reporting...

Therefore I wrote this "MailHub class" to reduce the necessary time to develop email notifications. Usually I define a set of emails with placeholders and replace the placeholders dynamically. In this way I am very flexible and can implement email notifications within seconds.

I created some examples to show you how easy you can use this PHP class. The source code should enable you to boost your learning curve of this class functions. In the end there is a complete list of the public functions of this class.

I hope this class saves you a lot of time so you can focus on the real challenging programming tasks.

Index of this page:

Example 1: plain email


$mh = new MailHub();
$mh->setBody("This is a plain email from the administrator");
$mh->setSubject("Example 1");
$mh->setFrom("admin@gmail.com", "Admin");
$mh->setTo("john.doe@example.de");
$mh->setTo("mrX@example.de");
$mh->setCC("mrY@example.de");
$mh->setBCC("mrZ@example.de", "Mr. Z");
$mh->sendMail();
if($mh->send() == true){ echo "success"; } else { echo "something went wrong"; }

Example 2: plain email with word replacements

This is my absolute favorit function of this php class. With replacePlaceholders you can easily create customized emails and create billing statements, send customized links or something like that. I always define my mail texts with placeholders upfront in the MailHub_class.php and mostly replace the placeholders with values from a sql database. You will see this function saves you a lot of time.


$mh = new MailHub();
$mh->setBody(EXAMPLE_BODY_PLAIN);				// EXAMPLE_BODY_PLAIN is defined in the MailHub_class.php
$mh->setSubject("Example 2");
$mh->setFrom("admin@gmail.com", "Admin");
$mh->setTo("john.doe@example.de");
$mh->replacePlaceholders( array(
	"surname" 		=> "John",
	"lastname" 		=> "Doe"
	));
$mh->sendMail();
if($mh->send() == true){ echo "success"; } else { echo "something went wrong"; }

Example 3: plain email with attachments

I tried to make the usage of attachments as easy as possible. No matter if you write a plain or a html email, no matter if the attachment is already stored on your webspace or if you want to send the file from an input field... For every situation it is the same function call.


$mh = new MailHub();
$mh->setBody("see the attachments);
$mh->setSubject("Example 3");
$mh->setFrom("admin@gmail.com", "Admin");
$mh->setTo("john.doe@example.de");
$mh->addAttachment("example_image.png");		// if your file is already saved on your webspace, add the storage location
$mh->addAttachment("attachment_file");  		// if the file is given by an html form like <input type="file" name="attachment_file">, add the "name" value
$mh->sendMail();
if($mh->send() == true){ echo "success"; } else { echo "something went wrong"; }

Example 4: html email


$mh = new MailHub();
$mh->setBody(EXAMPLE_BODY_HTML);				// EXAMPLE_BODY_HTML is defined in the MailHub_class.php
$mh->setMailType("html");
$mh->setSubject("Example 4");
$mh->setFrom("admin@gmail.com", "Admin");
$mh->setTo("john.doe@example.de");
$mh->sendMail();
if($mh->send() == true){ echo "success"; } else { echo "something went wrong"; }

Example 5: debug


$mh = new MailHub();
$mh->setSubject("Example 5");
$mh->setFrom("admin@gmail.com", "Admin");
$mh->setTo("john.doe@example.de");
$mh->setMailType("html");
$mh->setBody(EXAMPLE_BODY_HTML);
$mh->replacePlaceholders( array(
	"SUBJECT" 		=> "Example Title",
	"CURRENT_DATE" 	=> date("m/d/Y", time()),
	"MESSAGE"		=> "This is the body of the html email..."
	));
$mh->debug();

Installation

The installation of "MailHub" class is very simple and saves you a lot of time. Just follow the following installation steps and you will see how easy it is to create forms and validate them.

for plain text emails

  1. Copy the MailHub_class.php file to any folder you want (e.g. the directy /MailHub).
  2. Initialise the class: "require_once('/MailHub/MailHub_class.php');"
  3. You are ready to start... (best would be to start with one of the examples from this page...)
  4. Have fun, save a lot of time and give me a good rating on code canyon :-)

for html emails

  1. Do all the preparation for plain text emails
  2. Copy the other two php files to same location like the MailHub_class.php
  3. Open the MailHub_class.php and update your email settings (SMTP Variables)
  4. Have fun, save a lot of time and give me a good rating on code canyon :-)

Here is the smallest file to start with:

<html>
<head>
	<title>Demo - MailHub PHP Class</title>
</head>

<body>

<?php
require_once("MailHub_class.php");
$mh = new MailHub();
$mh->setTo(...);
$mh->setSubject(...);
$mh->setBody(...);
$mh->sendMail();
?>

</body>
</html>

Public functions of php class 'MailHub'

setTo( string $email [, string $name = false] )
define a recipient of the mail (Use the function multiple times if you want to define multiple recipients)
setCC( string $email [, string $name = false] )
define a emailadress (with or without a name) that should receive the mail on cc: (Use the function multiple times if you want to define multiple recipients)
setBCC( string $email [, string $name = false] )
same like setTo or setCC only for blind carbon copy (BCC)
setFrom( string $email [, string $name = false] )
define the email (and the name) from the sender of the mail.
setBody( string $body )
define/load the body of the email.
setSubject( string $subject )
define/load the subject of the email.
setMailType( string $type )
choose between plain or html email type. If you choose html type you have to define the SMTP values in the mailhub_class.php. Default value is plain.
replacePlaceholders( array $replacements )
This function could be used to replace placeholders in the mail body. Placeholders are always text snippets surounded with an opening # and an ending # (like #surname#). See the examples to get a feeling of the usage.
addAttachment( string $file )
you can use the addAttachment function in two ways to add an attachment to the email (could be used with plain and html mails):
- either you add a relative path like "logo.png" or
- you enter the name value of an input field (input type='file' name='xxx').
debug()
use debug to get further information or to search for errors.
sendMail()
this function sends the mails.
send()
use this function to check if the mails were send out correctly. send() will returns true or false and you can print a success or failure message based on that information.