<?php // remember to remove
// include FacileForms standard library
$this->execPieceByName('ff_InitLib');
// and the headerInfo library too
$this->execPieceByName('ff_headerInfo');
// GLOBAL VARS
global $form_field_category;
global $form_field_email;
global $form_field_message;
global $fromname;
global $date;
// ADDITIONAL ADMIN RECIPIENTS BASED ON CATEGORY
// ====================== EDIT FOLLOWING LINES =====================//
$custom_categories = array(
'general enquiry' => '[email protected]',
'contact the editor' => '[email protected]',
'admin enquiry' => '[email protected]',
'advertising' => '[email protected]',
'exhibiting' => '[email protected]',
'problem with my account' => '[email protected]',
'something else' => '[email protected]'
);
// STATIC VARS
$form_field_email = 'email'; // field name for user's email
$form_field_category = 'enquiry'; // field name to for the nature of the enquiry
$form_field_message = 'message'; // field name for the body of the message
// FORM VARIABLES (ALL)
$form_fields_raw = $this->submitdata;
$form_fields = array();
$i = 0;
foreach ($form_fields_raw as $fields) {
$form_fields[$i]['name'] = $fields[1];
$form_fields[$i]['label'] = $fields[2];
$form_fields[$i]['type'] = $fields[3];
$form_fields[$i]['value'] = $fields[4];
$i++;
}
// SITE CONFIG VARS
$config = JFactory::getConfig();
$sitename = $config->get('sitename'); // i.e. 'Main Website'
$mailfrom = $config->get('mailfrom'); // i.e. '[email protected]'
$fromname = $config->get('fromname'); // i.e. 'The ITD'
// Creates a new JDate object equal to the current time
$date = JFactory::getDate()->format('d/m/Y - H:i');
// Set the sender
$sender = array(
$mailfrom,
$fromname
);
// set default recipient to site admin and user to submitted email
$admin_recipients = array($config->get('mailfrom'));
$user_recipients = array(ff_getSubmit($form_field_email));
// check the subject and add the relevant email address to admin emails
if (count($custom_categories) > 0) {
foreach ($custom_categories as $key => $value) {
if ($key == ff_getSubmit($form_field_category)) {
array_push($admin_recipients, $value);
}
}
}
function emailSubject($s, $admin = true)
{
global $form_field_category;
global $form_field_email;
global $fromname;
if ($admin) {
$s->subject = "Message from the " . $fromname . " Website (" . ff_getSubmit($form_field_category) . ") [" . ff_getSubmit($form_field_email) . "]";
} else {
$s->subject = "Your message through the " . $fromname . " Website";
}
return $s;
}
// create the admin message object
function emailMessage($m, $form_fields = array(), $admin = true)
{
global $date;
global $fromname;
global $form_field_message;
$m->message = "<p>The following message was " . ($admin ? "received" : "sent") . " through the " . $fromname . " Website on <b>" . $date . "</b></p>";
$m->message .= "<h3 style=\"text-decoration:underline; text-transform: uppercase; font-weight: 100\">Message Details</h3>";
$m->message .= '<table border="0" cellpadding="0" cellspacing="0" height="100%" width="100%">';
$m->message .= '<tr>';
$m->message .= '<td align="left" valign="top">';
$m->message .= "<table border=\"0\" cellpadding=\"5\" cellspacing=\"0\" width=\"600\">";
// loop through all fields
foreach ($form_fields as $fields) {
$m->message .= "<tr>";
$m->message .= "<td nowrap><code>" . $fields['label'] . "</code></td><td><code><b>" . $fields['value'] . "</b></code></td>";
$m->message .= "</tr>";
}
$m->message .= "</table>";
$m->message .= '</td>';
$m->message .= '</tr>';
$m->message .= '</table>';
$m->message .= "<hr>";
// Admin message
if ($admin) {
// finally, tag on the header info (admin emails only)
$m->message .= ff_headerInfo($m);
} else {
// User message
$m->message .= "<p>We will be in touch as soon as possible.</p>";
}
return $m;
}
// SEND EMAILS
// ================================================== //
function sendEmail($sender, $recipients, $subject, $body)
{
$mailer = JFactory::getMailer();
$mailer->setSender($sender);
// make sure the recipient is in the correct format
$mailer->addRecipient($recipients);
$mailer->setSubject($subject);
$mailer->isHtml(true);
$mailer->Encoding = 'base64';
$mailer->setBody($body);
$sendEmail = $mailer->Send();
return $sendEmail;
}
// admin email
$admin_recipients = array_unique($admin_recipients);
$admin_subject = emailSubject($this, true)->subject;
$admin_body = emailMessage($this, $form_fields, true)->message;
$sendAdmin = sendEmail($sender, $admin_recipients, $admin_subject, $admin_body);
// user email
$user_recipients = array_unique($user_recipients);
$user_subject = emailSubject($this, false)->subject;
$user_body = emailMessage($this, $form_fields, false)->message;
$sendUser = sendEmail($sender, $user_recipients, $user_subject, $user_body);
if (!$sendAdmin) {
echo 'Failed sending Admin email';
}
if (!$sendUser) {
echo 'Failed sending User email';
}
Comments