Send mails
HazzelForms lets you send the form data to as many recipients as you wish. Keep in mind that validation is needed before!
Send mails with PHPMailer (SMTP)
You can pass HazzelForms a PHPMailer instance, which will then be used to send the emails via the SMTP server defined in PHP Mailer:
if($form->validate()){
// set up PHPMailer
// HINT: for clean and reusable code you might want to use e a separate function e.g. "createMailer()" that creates the PHPMailer instance
$mailer = new PHPMailer(true);
$mailer->isSMTP();
$mailer->Host = ''; // Set the SMTP server to send through
$mailer->Username = ''; // SMTP username
$mailer->Password = SMTP_PASSWORD; // SMTP password (warning: should be stored in a secure place – not hard coded)
$mailer->Port = 465;
$mailer->SMTPAuth = true;
$mailer->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
// pass PHPMailer to HazzelForms. HazzelForms automatically resets the recipents and reply-to addresses after each mail.
$form->setMailer($mailer);
// send mail
try {
$form->sendMail(
'john.doe@example.com', // to
'noreply@example.com', // from
'', // reply-to (optional, defaults to from — ⚠️ spam filters are getting more and more strict, so it is recommended to leave the reply-to address empty)
'Contact Form', // sender name (optional, defaults to "HazzelForms")
'New inquiry', // subject (optional, defaults to "New HazzelForms Message")
'basic' // template (optional, defaults to "basic")
);
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mailer->ErrorInfo}";
}
}
The straight forward way (PHP mail()
function)
If you are not ready to use PHPMailer but you still want to test your form, you can skip the $form->setMailer($mailer);
part.
HazzelForms will then try to use the PHP mail()
function to send the mail. Keep in mind that this is not recommended for production use.
if($form->validate()){
// it is as simple as that
$form->sendMail(
'john.doe@example.com' // to
'noreply@example.com' // from
);
}
Templates
Normally HazzelForms takes the 'basic' email template from the mail-templates folder. You can duplicate this template if needed and customize it as you like. Which mail template HazzelForms should use is determined by the sixth parameter of the form->sendMail()
function.
If you have more sophisticated needs, you can also create a template yourself using HazzelForms' template loader. To do this, you need a PHP file that prepares the mail content as HTML. You can use variables in the template and fill them into the template using the inject function of the template loader.
// load template file
$template = new HazzelForms\TemplateLoader('path/to/your/template.php');
// inject variables (you can access them in the template file using $vars['firstname'])
// NOTE: the form fields from HazzelForms are automatically injected as $fields array
$template->inject([
'firstname' => 'John',
'lastname' => 'Doe',
]);
// send mail
$form->sendMail(
'jane.doe@example.com',
'noreply@example.com',
'', // ⚠️ spam filters are getting more and more strict, so it is recommended to leave the reply-to address empty
'Contact Form',
'New inquiry',
$template, // pass your template loader instance to HazzelForms (instead of the template name)
);