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:
$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"; }
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"; }
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"; }
$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"; }
$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();
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
for html emails
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>