From 0c3cab70490de8aded1c4aa44ceacc065f839f84 Mon Sep 17 00:00:00 2001 From: Marten Date: Wed, 8 Jul 2020 22:14:29 +0200 Subject: [PATCH 01/10] Adding extra fields for custom thank you mail --- .../en-GB/en-GB.mod_jdsimplecontactform.ini | 4 +++ .../nl-NL/nl-NL.mod_jdsimplecontactform.ini | 4 +++ mod_jdsimplecontactform.xml | 25 +++++++++++++++++-- 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/language/en-GB/en-GB.mod_jdsimplecontactform.ini b/language/en-GB/en-GB.mod_jdsimplecontactform.ini index 6f4f9dd..7e30ec0 100644 --- a/language/en-GB/en-GB.mod_jdsimplecontactform.ini +++ b/language/en-GB/en-GB.mod_jdsimplecontactform.ini @@ -177,6 +177,10 @@ MOD_JDSCF_SINGLE_SEND_COPY_EMAIL_FIELD_LABEL="Enter Email Field Name" MOD_JDSCF_SINGLE_SEND_COPY_EMAIL_FIELD_DESC="Enter the name of the email field from the Form Layout on which you want to send copy" MOD_JDSCF_SINGLE_SEND_COPY_LABEL="Enter Send Copy Label" MOD_JDSCF_SINGLE_SEND_COPY_DESC="Label to show for the checkbox" +MOD_JDSCF_SINGLE_SEND_COPY_TEMPLATE_LBL="Thank you Email Template" +MOD_JDSCF_SINGLE_SEND_COPY_TEMPLATE_DESC="Select the email template to send to the mail address provided by the visitor, The default template lists all fields in the order they exist in
{field:label}: {field:value}
format." +MOD_JDSCF_SINGLE_SEND_COPY_CUSTOM_TEMPLATE_LBL="Custom thank you Email Template" +MOD_JDSCF_SINGLE_SEND_COPY_CUSTOM_TEMPLATE_LBL="Enter your custom email template to send to the mail address provided by the visitor. You can use {field:label} for the field name, and {field:value} for user input value. Change field to the actual name of your input field." MOD_JDSCF_EMAIL_NAME="Enter name of email" MOD_JDSCF_EMAIL_NAME_DESCRIPTION="Name of email input field to send a copy to." diff --git a/language/nl-NL/nl-NL.mod_jdsimplecontactform.ini b/language/nl-NL/nl-NL.mod_jdsimplecontactform.ini index 9c9b165..684c6ac 100644 --- a/language/nl-NL/nl-NL.mod_jdsimplecontactform.ini +++ b/language/nl-NL/nl-NL.mod_jdsimplecontactform.ini @@ -177,6 +177,10 @@ MOD_JDSCF_SINGLE_SEND_COPY_EMAIL_FIELD_LABEL="Vul een label in voor het Email Ve MOD_JDSCF_SINGLE_SEND_COPY_EMAIL_FIELD_DESC="Vul de naam in van het formulier veld van de Formulier Opmaak op basis waarvan je een kopie wilt versturen." MOD_JDSCF_SINGLE_SEND_COPY_LABEL="Vul een Stuur Kopie label in" MOD_JDSCF_SINGLE_SEND_COPY_DESC="Label om te tonen voor het vink veld." +MOD_JDSCF_SINGLE_SEND_COPY_TEMPLATE_LBL="Bedankt Email Template" +MOD_JDSCF_SINGLE_SEND_COPY_TEMPLATE_DESC="Selecteer de email template voor de email die naar het mailadres van de bezoeker wordt gestuurd. De default template toont alle velden in de volgorde zoals ze in het formulier getoond worden volgens het formaat
{field:label}: {field:value}
." +MOD_JDSCF_SINGLE_SEND_COPY_CUSTOM_TEMPLATE_LBL="Eigen bedankt Email Template" +MOD_JDSCF_SINGLE_SEND_COPY_CUSTOM_TEMPLATE_LBL="Vul een eigen email template in voor de email die naar het mailadres van de bezoeker wordt gestuurd. Je kunt {field:label} gebruiken voor de veldnaam, en {field:value} voor de veld waarde. Verander field naar de ingestelde naam van het veld." MOD_JDSCF_EMAIL_NAME="Vul de naam van de email in" MOD_JDSCF_EMAIL_NAME_DESCRIPTION="Naam van het email veld waar een kopie naar toegestuurd moet worden." diff --git a/mod_jdsimplecontactform.xml b/mod_jdsimplecontactform.xml index 01448c8..cc87244 100644 --- a/mod_jdsimplecontactform.xml +++ b/mod_jdsimplecontactform.xml @@ -79,7 +79,7 @@ useglobal="true" showon="params.captcha:1" > - + + /> + + + + + Date: Fri, 10 Jul 2020 07:21:57 +0200 Subject: [PATCH 02/10] Adding extra fields --- mod_jdsimplecontactform.xml | 35 ++++++++++++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/mod_jdsimplecontactform.xml b/mod_jdsimplecontactform.xml index cc87244..0fb2fd2 100644 --- a/mod_jdsimplecontactform.xml +++ b/mod_jdsimplecontactform.xml @@ -204,9 +204,10 @@ > + + + + + JGLOBAL_CUSTOM Date: Fri, 10 Jul 2020 21:20:40 +0200 Subject: [PATCH 03/10] Extracting sendmail function --- helper.php | 236 ++++++++++++++++++++++++++++++----------------------- 1 file changed, 133 insertions(+), 103 deletions(-) diff --git a/helper.php b/helper.php index 1a2d042..0fcf340 100644 --- a/helper.php +++ b/helper.php @@ -213,108 +213,7 @@ public static function submitForm($ajax = false) { ); } - if ($params->get('email_template', '') == 'custom') { - $html = $params->get('email_custom', ''); - if ( empty( $html ) ) { - $layout = new JLayoutFile('emails.default', JPATH_SITE . '/modules/mod_jdsimplecontactform/layouts'); - $html = $layout->render(['contents' => $contents]); - } else { - $html = self::renderVariables($contents, $html); - } - } else { - $layout = new JLayoutFile('emails.default', JPATH_SITE . '/modules/mod_jdsimplecontactform/layouts'); - $html = $layout->render(['contents' => $contents]); - } - - // sending mail - $mailer = JFactory::getMailer(); - $config = JFactory::getConfig(); - $title = $params->get('title', ''); - if (!empty($title)) { - $title = ' : ' . $title; - } - // Sender - if (!empty($params->get('email_from', ''))) { - $email_from = $params->get('email_from', ''); - $email_from = self::renderVariables($contents, $email_from); - if (!filter_var($email_from, FILTER_VALIDATE_EMAIL)) { - $email_from = $config->get('mailfrom'); - } - } else { - $email_from = $config->get('mailfrom'); - } - - if (!empty($params->get('email_name', ''))) { - $email_name = $params->get('email_name', ''); - $email_name = self::renderVariables($contents, $email_name); - if (empty($email_name)) { - $email_name = $config->get('fromname'); - } - } else { - $email_name = $config->get('fromname'); - } - - $sender = array($email_from, $email_name); - $mailer->setSender($sender); - - // Subject - $email_subject = !empty($params->get('email_subject', '')) ? $params->get('email_subject') : JText::_('MOD_JDSCF_DEFAULT_SUBJECT', $title); - $email_subject = self::renderVariables($contents, $email_subject); - $mailer->setSubject($email_subject); - - // Recipient - $recipients = !empty($params->get('email_to', '')) ? $params->get('email_to') : $config->get('mailfrom'); - $recipients = explode(',', $recipients); - if (!empty($recipients)) { - $mailer->addRecipient($recipients); - } - - // Reply-To - if (!empty($params->get('reply_to', ''))) { - $reply_to = $params->get('reply_to', ''); - $reply_to = self::renderVariables($contents, $reply_to); - if (!filter_var($reply_to, FILTER_VALIDATE_EMAIL)) { - $reply_to = ''; - } - $mailer->addReplyTo($reply_to); - } else { - $reply_to = ''; - } - - // CC - $cc = !empty($params->get('email_cc', '')) ? $params->get('email_cc') : ''; - $cc = empty($cc) ? [] : explode(",", $cc); - if(!empty($cc_emails)){ - $cc = array_merge($cc, $cc_emails); - $cc = array_unique($cc); - } - - if (!empty($cc)) { - $mailer->addCc($cc); - } - // BCC - $bcc = !empty($params->get('email_bcc', '')) ? $params->get('email_bcc') : ''; - $bcc = empty($bcc) ? [] : explode(',', $bcc); - if (!empty($bcc)) { - $mailer->addBcc($bcc); - } - $mailer->isHtml(true); - $mailer->Encoding = 'base64'; - $mailer->setBody($html); - foreach($attachments as $attachment){ - $mailer->addAttachment($attachment); - } - if(!empty($errors)) { - $app = JFactory::getApplication(); - $send = false; - // showing all the validation errors - foreach ($errors as $error) { - $app->enqueueMessage(\JText::_($error), 'error'); - } - } - else { - $send = $mailer->Send(); - } + $send = self::sendMail($params, $contents, $attachments, $cc_emails, $errors, $app); if ($send !== true) { switch($params->get('ajaxsubmit')) @@ -424,7 +323,13 @@ public static function getJS($moduleid) { return $GLOBALS['mod_jdscf_js_' . $moduleid]; } - //for single email field (at bottom) + + /** + * Is Single CC enabled. For single email field (at bottom) + * + * @param \Joomla\Registry\Registry $params Parameters of the module + * @return boolean + */ public static function isSingleCCMail($params) { $singlesendcopy_email = $params->get('single_sendcopy_email', 0); $singlesendcopyemail_field = $params->get('singleSendCopyEmail_field', ''); @@ -435,6 +340,13 @@ public static function isSingleCCMail($params) { } } + /** + * Upload a file and return it's full path + * + * @param [type] $name + * @param [type] $src + * @return string Full path file name + */ public static function uploadFile($name, $src) { jimport('joomla.filesystem.file'); jimport('joomla.application.component.helper'); @@ -469,4 +381,122 @@ public static function uploadFile($name, $src) { return $return; } } + + /** + * Send mail with configured content to configured mail address + * + * @param \Joomla\Registry\Registry $params + * @param array $contents + * @param string[] $attachments + * @param string[] $cc_emails + * @param string[] $errors + * @param \Joomla\CMS\Application\CMSApplication $app + * @return boolean + */ + private static function sendMail($params, $contents, $attachments, $cc_emails, $errors, $app) { + + if ($params->get('email_template', '') == 'custom') { + $html = $params->get('email_custom', ''); + if ( empty( $html ) ) { + $layout = new JLayoutFile('emails.default', JPATH_SITE . '/modules/mod_jdsimplecontactform/layouts'); + $html = $layout->render(['contents' => $contents]); + } else { + $html = self::renderVariables($contents, $html); + } + } else { + $layout = new JLayoutFile('emails.default', JPATH_SITE . '/modules/mod_jdsimplecontactform/layouts'); + $html = $layout->render(['contents' => $contents]); + } + + // sending mail + $mailer = JFactory::getMailer(); + $config = JFactory::getConfig(); + $title = $params->get('title', ''); + if (!empty($title)) { + $title = ' : ' . $title; + } + // Sender + if (!empty($params->get('email_from', ''))) { + $email_from = $params->get('email_from', ''); + $email_from = self::renderVariables($contents, $email_from); + if (!filter_var($email_from, FILTER_VALIDATE_EMAIL)) { + $email_from = $config->get('mailfrom'); + } + } else { + $email_from = $config->get('mailfrom'); + } + + if (!empty($params->get('email_name', ''))) { + $email_name = $params->get('email_name', ''); + $email_name = self::renderVariables($contents, $email_name); + if (empty($email_name)) { + $email_name = $config->get('fromname'); + } + } else { + $email_name = $config->get('fromname'); + } + + $sender = array($email_from, $email_name); + $mailer->setSender($sender); + + // Subject + $email_subject = !empty($params->get('email_subject', '')) ? $params->get('email_subject') : JText::_('MOD_JDSCF_DEFAULT_SUBJECT', $title); + $email_subject = self::renderVariables($contents, $email_subject); + $mailer->setSubject($email_subject); + + // Recipient + $recipients = !empty($params->get('email_to', '')) ? $params->get('email_to') : $config->get('mailfrom'); + $recipients = explode(',', $recipients); + if (!empty($recipients)) { + $mailer->addRecipient($recipients); + } + + // Reply-To + if (!empty($params->get('reply_to', ''))) { + $reply_to = $params->get('reply_to', ''); + $reply_to = self::renderVariables($contents, $reply_to); + if (!filter_var($reply_to, FILTER_VALIDATE_EMAIL)) { + $reply_to = ''; + } + $mailer->addReplyTo($reply_to); + } else { + $reply_to = ''; + } + + // CC + $cc = !empty($params->get('email_cc', '')) ? $params->get('email_cc') : ''; + $cc = empty($cc) ? [] : explode(",", $cc); + if(!empty($cc_emails)){ + $cc = array_merge($cc, $cc_emails); + $cc = array_unique($cc); + } + + if (!empty($cc)) { + $mailer->addCc($cc); + } + // BCC + $bcc = !empty($params->get('email_bcc', '')) ? $params->get('email_bcc') : ''; + $bcc = empty($bcc) ? [] : explode(',', $bcc); + if (!empty($bcc)) { + $mailer->addBcc($bcc); + } + $mailer->isHtml(true); + $mailer->Encoding = 'base64'; + $mailer->setBody($html); + foreach($attachments as $attachment){ + $mailer->addAttachment($attachment); + } + if(!empty($errors)) { + $app = JFactory::getApplication(); + $send = false; + // showing all the validation errors + foreach ($errors as $error) { + $app->enqueueMessage(\JText::_($error), 'error'); + } + return $send; + } + else { + return $mailer->Send(); + } + } } From 1ccd8ccdf935ac5a8b7211e94ec8989b601ef85c Mon Sep 17 00:00:00 2001 From: Marten Date: Fri, 10 Jul 2020 22:02:50 +0200 Subject: [PATCH 04/10] Adding field prefixes --- helper.php | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/helper.php b/helper.php index 0fcf340..a73bcaf 100644 --- a/helper.php +++ b/helper.php @@ -213,7 +213,7 @@ public static function submitForm($ajax = false) { ); } - $send = self::sendMail($params, $contents, $attachments, $cc_emails, $errors, $app); + $send = self::sendMail($params, 'email', $contents, $attachments, $cc_emails, $errors, $app); if ($send !== true) { switch($params->get('ajaxsubmit')) @@ -386,6 +386,7 @@ public static function uploadFile($name, $src) { * Send mail with configured content to configured mail address * * @param \Joomla\Registry\Registry $params + * @param string $fieldPrefix Prefix with fields of the email * @param array $contents * @param string[] $attachments * @param string[] $cc_emails @@ -393,10 +394,10 @@ public static function uploadFile($name, $src) { * @param \Joomla\CMS\Application\CMSApplication $app * @return boolean */ - private static function sendMail($params, $contents, $attachments, $cc_emails, $errors, $app) { + private static function sendMail($params, $fieldPrefix, $contents, $attachments, $cc_emails, $errors, $app) { - if ($params->get('email_template', '') == 'custom') { - $html = $params->get('email_custom', ''); + if ($params->get($fieldPrefix . '_template', '') == 'custom') { + $html = $params->get($fieldPrefix . '_custom', ''); if ( empty( $html ) ) { $layout = new JLayoutFile('emails.default', JPATH_SITE . '/modules/mod_jdsimplecontactform/layouts'); $html = $layout->render(['contents' => $contents]); @@ -416,8 +417,8 @@ private static function sendMail($params, $contents, $attachments, $cc_emails, $ $title = ' : ' . $title; } // Sender - if (!empty($params->get('email_from', ''))) { - $email_from = $params->get('email_from', ''); + if (!empty($params->get($fieldPrefix . '_from', ''))) { + $email_from = $params->get($fieldPrefix . '_from', ''); $email_from = self::renderVariables($contents, $email_from); if (!filter_var($email_from, FILTER_VALIDATE_EMAIL)) { $email_from = $config->get('mailfrom'); @@ -426,8 +427,8 @@ private static function sendMail($params, $contents, $attachments, $cc_emails, $ $email_from = $config->get('mailfrom'); } - if (!empty($params->get('email_name', ''))) { - $email_name = $params->get('email_name', ''); + if (!empty($params->get($fieldPrefix . '_name', ''))) { + $email_name = $params->get($fieldPrefix . '_name', ''); $email_name = self::renderVariables($contents, $email_name); if (empty($email_name)) { $email_name = $config->get('fromname'); @@ -440,20 +441,24 @@ private static function sendMail($params, $contents, $attachments, $cc_emails, $ $mailer->setSender($sender); // Subject - $email_subject = !empty($params->get('email_subject', '')) ? $params->get('email_subject') : JText::_('MOD_JDSCF_DEFAULT_SUBJECT', $title); + $email_subject = !empty($params->get($fieldPrefix . '_subject', '')) ? $params->get($fieldPrefix . '_subject') : JText::_('MOD_JDSCF_DEFAULT_SUBJECT', $title); $email_subject = self::renderVariables($contents, $email_subject); $mailer->setSubject($email_subject); // Recipient - $recipients = !empty($params->get('email_to', '')) ? $params->get('email_to') : $config->get('mailfrom'); + $recipients = !empty($params->get($fieldPrefix . '_to', '')) ? $params->get($fieldPrefix . '_to') : $config->get('mailfrom'); $recipients = explode(',', $recipients); if (!empty($recipients)) { $mailer->addRecipient($recipients); } // Reply-To - if (!empty($params->get('reply_to', ''))) { - $reply_to = $params->get('reply_to', ''); + $replyToFieldName = $fieldPrefix . 'reply_to'; + if ($fieldPrefix === 'email') { + $replyToFieldName = 'reply_to'; + } + if (!empty($params->get($replyToFieldName, ''))) { + $reply_to = $params->get($replyToFieldName, ''); $reply_to = self::renderVariables($contents, $reply_to); if (!filter_var($reply_to, FILTER_VALIDATE_EMAIL)) { $reply_to = ''; @@ -464,7 +469,7 @@ private static function sendMail($params, $contents, $attachments, $cc_emails, $ } // CC - $cc = !empty($params->get('email_cc', '')) ? $params->get('email_cc') : ''; + $cc = !empty($params->get($fieldPrefix . '_cc', '')) ? $params->get($fieldPrefix . '_cc') : ''; $cc = empty($cc) ? [] : explode(",", $cc); if(!empty($cc_emails)){ $cc = array_merge($cc, $cc_emails); @@ -475,7 +480,7 @@ private static function sendMail($params, $contents, $attachments, $cc_emails, $ $mailer->addCc($cc); } // BCC - $bcc = !empty($params->get('email_bcc', '')) ? $params->get('email_bcc') : ''; + $bcc = !empty($params->get($fieldPrefix . '_bcc', '')) ? $params->get($fieldPrefix . '_bcc') : ''; $bcc = empty($bcc) ? [] : explode(',', $bcc); if (!empty($bcc)) { $mailer->addBcc($bcc); From fa83ee88977ee99cc2c0fc1bee26a79f65f1e943 Mon Sep 17 00:00:00 2001 From: Marten Date: Sat, 11 Jul 2020 08:09:42 +0200 Subject: [PATCH 05/10] Adding mail for visitor. Still need to make it work correctly --- helper.php | 86 ++++++++++++++++++++++++++----------- mod_jdsimplecontactform.xml | 16 +++---- 2 files changed, 68 insertions(+), 34 deletions(-) diff --git a/helper.php b/helper.php index a73bcaf..9a9b7d2 100644 --- a/helper.php +++ b/helper.php @@ -98,7 +98,7 @@ public static function submitForm($ajax = false) { $labels[$field->name] = ['label' => self::getLabelText($field), 'type' => $field->type]; } - $cc_emails = []; + $singleSendCopyMailAddress = []; $values = []; foreach ($jdscf as $name => $value) { if(is_array($value)) { @@ -109,7 +109,7 @@ public static function submitForm($ajax = false) { //single cc if(isset($value['single_cc']) && $value['single_cc'] == 1) { - $cc_emails[] = $value['email']; + $singleSendCopyMailAddress[] = $value['email']; } } @@ -213,7 +213,15 @@ public static function submitForm($ajax = false) { ); } - $send = self::sendMail($params, 'email', $contents, $attachments, $cc_emails, $errors, $app); + // Send mail to backend + $mailParamsBackend = self::getMailParams($params, 'email', $singleSendCopyMailAddress); + $send = self::sendMail($mailParamsBackend, $contents, $attachments, $errors, $app); + + if ($send === true) { + // Send mail to visitor + $mailParamsVisitor = self::getMailParams($params, 'singleSendCopyEmail', $singleSendCopyMailAddress); + $send = self::sendMail($mailParamsVisitor, $contents, $attachments, $errors, $app); + } if ($send !== true) { switch($params->get('ajaxsubmit')) @@ -383,21 +391,51 @@ public static function uploadFile($name, $src) { } /** - * Send mail with configured content to configured mail address + * Get the mail paramaters from the params object * - * @param \Joomla\Registry\Registry $params + * @param \Joomla\Registry\Registry $params Contains the parameters of the module * @param string $fieldPrefix Prefix with fields of the email + * @param string[] $singleSendCopyMailAddress Mailaddress to be used to sent confirmation mail to visitor + * @return string[] Parameter values of the mail + */ + private static function getMailParams($params, $fieldPrefix, $singleSendCopyMailAddress){ + $mailParams = []; + $mailParams['template'] = $params->get($fieldPrefix . '_template', ''); + $mailParams['custom'] = $params->get($fieldPrefix . '_custom', ''); + $mailParams['from'] = $params->get($fieldPrefix . '_from', ''); + $mailParams['name'] = $params->get($fieldPrefix . '_name', ''); + $mailParams['subject'] = $params->get($fieldPrefix . '_subject', ''); + $mailParams['to'] = $params->get($fieldPrefix . '_to', ''); + $mailParams['cc'] = $params->get($fieldPrefix . '_cc', ''); + $mailParams['bcc'] = $params->get($fieldPrefix . '_bcc', ''); + $mailParams['title'] = $params->get('title', ''); + $mailParams['singleSendCopyMailAddress'] = $singleSendCopyMailAddress; + + // For custom visitor mail, we need the mailaddress of the visitor. + $mailParams['from'] = $fieldPrefix === 'singleSendCopyEmail' ? $singleSendCopyMailAddress : $params->get($fieldPrefix . '_from', ''); + + // Compensate for inconsistent naming. In future maybe update the field name. + $replyToFieldName = $fieldPrefix === 'email' ? 'reply_to' : $fieldPrefix . 'reply_to'; + $mailParams['reply_to'] = $params->get($replyToFieldName, ''); + + return $mailParams; + + } + + /** + * Send mail with configured content to configured mail address + * + * @param string[] $mailParams Parameter values needed for sending the mail * @param array $contents * @param string[] $attachments - * @param string[] $cc_emails * @param string[] $errors * @param \Joomla\CMS\Application\CMSApplication $app * @return boolean */ - private static function sendMail($params, $fieldPrefix, $contents, $attachments, $cc_emails, $errors, $app) { + private static function sendMail($mailParams, $contents, $attachments, $errors, $app) { - if ($params->get($fieldPrefix . '_template', '') == 'custom') { - $html = $params->get($fieldPrefix . '_custom', ''); + if ($mailParams['template'] == 'custom') { + $html = $mailParams['custom']; if ( empty( $html ) ) { $layout = new JLayoutFile('emails.default', JPATH_SITE . '/modules/mod_jdsimplecontactform/layouts'); $html = $layout->render(['contents' => $contents]); @@ -412,13 +450,13 @@ private static function sendMail($params, $fieldPrefix, $contents, $attachments, // sending mail $mailer = JFactory::getMailer(); $config = JFactory::getConfig(); - $title = $params->get('title', ''); + $title = $mailParams['title']; if (!empty($title)) { $title = ' : ' . $title; } // Sender - if (!empty($params->get($fieldPrefix . '_from', ''))) { - $email_from = $params->get($fieldPrefix . '_from', ''); + if (!empty($mailParams['from'])) { + $email_from = $mailParams['from']; $email_from = self::renderVariables($contents, $email_from); if (!filter_var($email_from, FILTER_VALIDATE_EMAIL)) { $email_from = $config->get('mailfrom'); @@ -427,8 +465,8 @@ private static function sendMail($params, $fieldPrefix, $contents, $attachments, $email_from = $config->get('mailfrom'); } - if (!empty($params->get($fieldPrefix . '_name', ''))) { - $email_name = $params->get($fieldPrefix . '_name', ''); + if (!empty($mailParams['name'])) { + $email_name = $mailParams['name']; $email_name = self::renderVariables($contents, $email_name); if (empty($email_name)) { $email_name = $config->get('fromname'); @@ -441,24 +479,20 @@ private static function sendMail($params, $fieldPrefix, $contents, $attachments, $mailer->setSender($sender); // Subject - $email_subject = !empty($params->get($fieldPrefix . '_subject', '')) ? $params->get($fieldPrefix . '_subject') : JText::_('MOD_JDSCF_DEFAULT_SUBJECT', $title); + $email_subject = !empty($mailParams['subject']) ? $mailParams['subject'] : JText::_('MOD_JDSCF_DEFAULT_SUBJECT', $title); $email_subject = self::renderVariables($contents, $email_subject); $mailer->setSubject($email_subject); // Recipient - $recipients = !empty($params->get($fieldPrefix . '_to', '')) ? $params->get($fieldPrefix . '_to') : $config->get('mailfrom'); + $recipients = !empty($mailParams['to']) ? $mailParams['to'] : $config->get('mailfrom'); $recipients = explode(',', $recipients); if (!empty($recipients)) { $mailer->addRecipient($recipients); } // Reply-To - $replyToFieldName = $fieldPrefix . 'reply_to'; - if ($fieldPrefix === 'email') { - $replyToFieldName = 'reply_to'; - } - if (!empty($params->get($replyToFieldName, ''))) { - $reply_to = $params->get($replyToFieldName, ''); + if (!empty($mailParams['reply_to'])) { + $reply_to = $mailParams['reply_to']; $reply_to = self::renderVariables($contents, $reply_to); if (!filter_var($reply_to, FILTER_VALIDATE_EMAIL)) { $reply_to = ''; @@ -469,10 +503,10 @@ private static function sendMail($params, $fieldPrefix, $contents, $attachments, } // CC - $cc = !empty($params->get($fieldPrefix . '_cc', '')) ? $params->get($fieldPrefix . '_cc') : ''; + $cc = !empty($mailParams['cc']) ? $mailParams['cc'] : ''; $cc = empty($cc) ? [] : explode(",", $cc); - if(!empty($cc_emails)){ - $cc = array_merge($cc, $cc_emails); + if(!empty($mailParams['singleSendCopyMailAddress'])){ + $cc = array_merge($cc, $mailParams['singleSendCopyMailAddress']); $cc = array_unique($cc); } @@ -480,7 +514,7 @@ private static function sendMail($params, $fieldPrefix, $contents, $attachments, $mailer->addCc($cc); } // BCC - $bcc = !empty($params->get($fieldPrefix . '_bcc', '')) ? $params->get($fieldPrefix . '_bcc') : ''; + $bcc = !empty($mailParams['bcc']) ? $mailParams['bcc'] : ''; $bcc = empty($bcc) ? [] : explode(',', $bcc); if (!empty($bcc)) { $mailer->addBcc($bcc); diff --git a/mod_jdsimplecontactform.xml b/mod_jdsimplecontactform.xml index 0fb2fd2..bf68189 100644 --- a/mod_jdsimplecontactform.xml +++ b/mod_jdsimplecontactform.xml @@ -216,7 +216,7 @@ /> JGLOBAL_CUSTOM Date: Sat, 11 Jul 2020 21:10:56 +0200 Subject: [PATCH 06/10] Making visitor mail work --- helper.php | 5 ++--- mod_jdsimplecontactform.xml | 2 +- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/helper.php b/helper.php index 9a9b7d2..63145bb 100644 --- a/helper.php +++ b/helper.php @@ -405,17 +405,16 @@ private static function getMailParams($params, $fieldPrefix, $singleSendCopyMail $mailParams['from'] = $params->get($fieldPrefix . '_from', ''); $mailParams['name'] = $params->get($fieldPrefix . '_name', ''); $mailParams['subject'] = $params->get($fieldPrefix . '_subject', ''); - $mailParams['to'] = $params->get($fieldPrefix . '_to', ''); $mailParams['cc'] = $params->get($fieldPrefix . '_cc', ''); $mailParams['bcc'] = $params->get($fieldPrefix . '_bcc', ''); $mailParams['title'] = $params->get('title', ''); $mailParams['singleSendCopyMailAddress'] = $singleSendCopyMailAddress; // For custom visitor mail, we need the mailaddress of the visitor. - $mailParams['from'] = $fieldPrefix === 'singleSendCopyEmail' ? $singleSendCopyMailAddress : $params->get($fieldPrefix . '_from', ''); + $mailParams['to'] = $fieldPrefix === 'singleSendCopyEmail' ? $singleSendCopyMailAddress[0] : $params->get($fieldPrefix . '_to', ''); // Compensate for inconsistent naming. In future maybe update the field name. - $replyToFieldName = $fieldPrefix === 'email' ? 'reply_to' : $fieldPrefix . 'reply_to'; + $replyToFieldName = $fieldPrefix === 'email' ? 'reply_to' : $fieldPrefix . '_reply_to'; $mailParams['reply_to'] = $params->get($replyToFieldName, ''); return $mailParams; diff --git a/mod_jdsimplecontactform.xml b/mod_jdsimplecontactform.xml index bf68189..8313237 100644 --- a/mod_jdsimplecontactform.xml +++ b/mod_jdsimplecontactform.xml @@ -264,7 +264,7 @@ Date: Sat, 11 Jul 2020 22:29:31 +0200 Subject: [PATCH 07/10] Get visitor mailaddress from form fields and not from singlesendcopymailaddress --- helper.php | 41 ++++++++++++++++++++++++++++++++++------- 1 file changed, 34 insertions(+), 7 deletions(-) diff --git a/helper.php b/helper.php index 63145bb..37377d3 100644 --- a/helper.php +++ b/helper.php @@ -187,7 +187,7 @@ public static function submitForm($ajax = false) { } } - $contents[] = [ + $contents[$name] = [ "value" => $value, "label" => $fld['label'], "name" => $name, @@ -206,7 +206,7 @@ public static function submitForm($ajax = false) { $ipAddress = $_SERVER['REMOTE_ADDR']; } - $contents[] = array( + $contents["jdsimple_ip"] = array( "value" => "$ipAddress", "label" => "IP Address", "name" => "ip" @@ -214,12 +214,12 @@ public static function submitForm($ajax = false) { } // Send mail to backend - $mailParamsBackend = self::getMailParams($params, 'email', $singleSendCopyMailAddress); + $mailParamsBackend = self::getMailParams($params, 'email', $singleSendCopyMailAddress, $contents); $send = self::sendMail($mailParamsBackend, $contents, $attachments, $errors, $app); if ($send === true) { // Send mail to visitor - $mailParamsVisitor = self::getMailParams($params, 'singleSendCopyEmail', $singleSendCopyMailAddress); + $mailParamsVisitor = self::getMailParams($params, 'singleSendCopyEmail', $singleSendCopyMailAddress, $contents); $send = self::sendMail($mailParamsVisitor, $contents, $attachments, $errors, $app); } @@ -390,15 +390,31 @@ public static function uploadFile($name, $src) { } } + /** + * Returns the value for a specific field + * + * @param string $fieldname Name of the field for which the value should be returned + * @param array[] $contents Collections of fields and there values + * @return string + */ + private static function getContentsValue($fieldname,$contents){ + $contentValue = $contents[$fieldname]; + if (!empty($contentValue)) { + return $contentValue['value']; + } + return ''; + } + /** * Get the mail paramaters from the params object * * @param \Joomla\Registry\Registry $params Contains the parameters of the module * @param string $fieldPrefix Prefix with fields of the email * @param string[] $singleSendCopyMailAddress Mailaddress to be used to sent confirmation mail to visitor + * @param array[string] $contents Values filled in to the form * @return string[] Parameter values of the mail */ - private static function getMailParams($params, $fieldPrefix, $singleSendCopyMailAddress){ + private static function getMailParams($params, $fieldPrefix, $singleSendCopyMailAddress, $contents){ $mailParams = []; $mailParams['template'] = $params->get($fieldPrefix . '_template', ''); $mailParams['custom'] = $params->get($fieldPrefix . '_custom', ''); @@ -408,10 +424,21 @@ private static function getMailParams($params, $fieldPrefix, $singleSendCopyMail $mailParams['cc'] = $params->get($fieldPrefix . '_cc', ''); $mailParams['bcc'] = $params->get($fieldPrefix . '_bcc', ''); $mailParams['title'] = $params->get('title', ''); - $mailParams['singleSendCopyMailAddress'] = $singleSendCopyMailAddress; + + if ($fieldPrefix === 'email') { + $mailParams['singleSendCopyMailAddress'] = $singleSendCopyMailAddress; + } // For custom visitor mail, we need the mailaddress of the visitor. - $mailParams['to'] = $fieldPrefix === 'singleSendCopyEmail' ? $singleSendCopyMailAddress[0] : $params->get($fieldPrefix . '_to', ''); + if ($fieldPrefix === 'singleSendCopyEmail') { + $mailField = $params->get('singleSendCopyEmail_field', ''); + $mailValue = self::getContentsValue($mailField, $contents); + if (!empty($mailValue)) { + $mailParams['to'] = $mailValue; + } + } else { + $mailParams['to'] = $params->get($fieldPrefix . '_to', ''); + } // Compensate for inconsistent naming. In future maybe update the field name. $replyToFieldName = $fieldPrefix === 'email' ? 'reply_to' : $fieldPrefix . '_reply_to'; From 15358e476d2a7b5cc5ffe186b083f2477d948b61 Mon Sep 17 00:00:00 2001 From: Marten Date: Sun, 12 Jul 2020 21:59:29 +0200 Subject: [PATCH 08/10] Adding variable support for Name and From field --- helper.php | 37 +++++++++++++++++++++++++++++++++---- 1 file changed, 33 insertions(+), 4 deletions(-) diff --git a/helper.php b/helper.php index 37377d3..3f32f44 100644 --- a/helper.php +++ b/helper.php @@ -265,6 +265,19 @@ public static function renderVariables($variables, $source) { return $source; } + /** + * Checks if string has one or more variables in it + * + * @param string $content String to check if it contains one or more variables + * @return boolean + */ + private static function hasVariable($content) { + if (preg_match('/\{.+?:((label)|(value))\}/', $content)) { + return true; + } + return false; + } + public static function getModuleParams() { $app = JFactory::getApplication(); $jinput = $app->input->post; @@ -405,6 +418,22 @@ private static function getContentsValue($fieldname,$contents){ return ''; } + /** + * Replaces variables with field label/field value, if token is present + * + * @param string $fieldname Name of the field + * @param \Joomla\Registry\Registry $params Contains the parameters of the module + * @param array[string] $contents Values filled in to the form + * @return string + */ + private static function getRenderedValue($fieldname, $params, $contents){ + $returnValue = $params->get($fieldname); + if (!empty($returnValue) && self::hasVariable($returnValue)) { + $returnValue = self::renderVariables($contents, $returnValue); + } + return $returnValue; + } + /** * Get the mail paramaters from the params object * @@ -418,13 +447,13 @@ private static function getMailParams($params, $fieldPrefix, $singleSendCopyMail $mailParams = []; $mailParams['template'] = $params->get($fieldPrefix . '_template', ''); $mailParams['custom'] = $params->get($fieldPrefix . '_custom', ''); - $mailParams['from'] = $params->get($fieldPrefix . '_from', ''); - $mailParams['name'] = $params->get($fieldPrefix . '_name', ''); $mailParams['subject'] = $params->get($fieldPrefix . '_subject', ''); $mailParams['cc'] = $params->get($fieldPrefix . '_cc', ''); $mailParams['bcc'] = $params->get($fieldPrefix . '_bcc', ''); $mailParams['title'] = $params->get('title', ''); - + $mailParams['from'] = self::getRenderedValue($fieldPrefix . '_from', $params, $contents); + $mailParams['name'] = self::getRenderedValue($fieldPrefix . '_name', $params, $contents); + if ($fieldPrefix === 'email') { $mailParams['singleSendCopyMailAddress'] = $singleSendCopyMailAddress; } @@ -442,7 +471,7 @@ private static function getMailParams($params, $fieldPrefix, $singleSendCopyMail // Compensate for inconsistent naming. In future maybe update the field name. $replyToFieldName = $fieldPrefix === 'email' ? 'reply_to' : $fieldPrefix . '_reply_to'; - $mailParams['reply_to'] = $params->get($replyToFieldName, ''); + $mailParams['reply_to'] = self::getRenderedValue($replyToFieldName, $params, $contents); return $mailParams; From 44456d25a69b40cc44aa56e69de0227407c4f8fd Mon Sep 17 00:00:00 2001 From: Marten Date: Sun, 12 Jul 2020 22:22:50 +0200 Subject: [PATCH 09/10] Hide visitor singleCCMail question for custom mails --- helper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/helper.php b/helper.php index 3f32f44..6703e82 100644 --- a/helper.php +++ b/helper.php @@ -354,7 +354,7 @@ public static function getJS($moduleid) { public static function isSingleCCMail($params) { $singlesendcopy_email = $params->get('single_sendcopy_email', 0); $singlesendcopyemail_field = $params->get('singleSendCopyEmail_field', ''); - if($singlesendcopy_email && !empty($singlesendcopyemail_field)){ + if($singlesendcopy_email && $singlesendcopy_email === "1" && !empty($singlesendcopyemail_field)){ return true; } else { return false; From 71deb8c4935775529bef43547972c8f3107df052 Mon Sep 17 00:00:00 2001 From: Marten Date: Mon, 13 Jul 2020 20:48:05 +0200 Subject: [PATCH 10/10] Adding check for valid email and adding texts for new fields --- helper.php | 4 +++- language/en-GB/en-GB.mod_jdsimplecontactform.ini | 10 +++++++++- language/nl-NL/nl-NL.mod_jdsimplecontactform.ini | 10 +++++++++- 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/helper.php b/helper.php index 6703e82..8b14d3d 100644 --- a/helper.php +++ b/helper.php @@ -463,7 +463,9 @@ private static function getMailParams($params, $fieldPrefix, $singleSendCopyMail $mailField = $params->get('singleSendCopyEmail_field', ''); $mailValue = self::getContentsValue($mailField, $contents); if (!empty($mailValue)) { - $mailParams['to'] = $mailValue; + if (filter_var($mailValue, FILTER_VALIDATE_EMAIL)) { + $mailParams['to'] = $mailValue; + } } } else { $mailParams['to'] = $params->get($fieldPrefix . '_to', ''); diff --git a/language/en-GB/en-GB.mod_jdsimplecontactform.ini b/language/en-GB/en-GB.mod_jdsimplecontactform.ini index 7e30ec0..9bf784a 100644 --- a/language/en-GB/en-GB.mod_jdsimplecontactform.ini +++ b/language/en-GB/en-GB.mod_jdsimplecontactform.ini @@ -172,11 +172,19 @@ MOD_JDSCF_SUN="SUN" ; Single Email Fields MOD_JDSCF_SINGLE_SEND_COPY="Send Copy of Email" MOD_JDSCF_SINGLE_SEND_COPY_LBL_TITLE="Send me a copy" -MOD_JDSCF_SINGLE_SEND_COPY_DESCRIPTION="Displays a checkbox on bottom for users to send a copy of email to themselves." +MOD_JDSCF_SINGLE_SEND_COPY_DESCRIPTION="'Yes': Displays a checkbox on bottom for users to send a copy of email to themselves. 'Custom': Send a custom mail message to the visitor." MOD_JDSCF_SINGLE_SEND_COPY_EMAIL_FIELD_LABEL="Enter Email Field Name" MOD_JDSCF_SINGLE_SEND_COPY_EMAIL_FIELD_DESC="Enter the name of the email field from the Form Layout on which you want to send copy" MOD_JDSCF_SINGLE_SEND_COPY_LABEL="Enter Send Copy Label" MOD_JDSCF_SINGLE_SEND_COPY_DESC="Label to show for the checkbox" +MOD_JDSCF_SINGLE_SEND_COPY_FROM_LBL="From Thank you email" +MOD_JDSCF_SINGLE_SEND_COPY_FROM_DESC="Enter the from email for the Thank you email" +MOD_JDSCF_SINGLE_SEND_COPY_NAME_LBL="From Name Thank you email" +MOD_JDSCF_SINGLE_SEND_COPY_NAME_DESC="Enter the from name for the Thank you email" +MOD_JDSCF_SINGLE_SEND_COPY_SUBJECT_LBL="Email Subject Thank you email" +MOD_JDSCF_SINGLE_SEND_COPY_SUBJECT_DESC="Subject of the email (You can use {field:label} & {field:value} to render dynamic values in this field)." +MOD_JDSCF_SINGLE_SEND_COPY_REPLY_TO_LBL="Reply-to Email Thank you mail" +MOD_JDSCF_SINGLE_SEND_COPY_REPLY_TO_DESC="Enter the Reply-to email address for the Thank you mail" MOD_JDSCF_SINGLE_SEND_COPY_TEMPLATE_LBL="Thank you Email Template" MOD_JDSCF_SINGLE_SEND_COPY_TEMPLATE_DESC="Select the email template to send to the mail address provided by the visitor, The default template lists all fields in the order they exist in
{field:label}: {field:value}
format." MOD_JDSCF_SINGLE_SEND_COPY_CUSTOM_TEMPLATE_LBL="Custom thank you Email Template" diff --git a/language/nl-NL/nl-NL.mod_jdsimplecontactform.ini b/language/nl-NL/nl-NL.mod_jdsimplecontactform.ini index 684c6ac..26e1a01 100644 --- a/language/nl-NL/nl-NL.mod_jdsimplecontactform.ini +++ b/language/nl-NL/nl-NL.mod_jdsimplecontactform.ini @@ -172,11 +172,19 @@ MOD_JDSCF_SUN="ZON" ; Single Email Fields MOD_JDSCF_SINGLE_SEND_COPY="Stuur een kopie van de Email" MOD_JDSCF_SINGLE_SEND_COPY_LBL_TITLE="Stuur mij een kopie" -MOD_JDSCF_SINGLE_SEND_COPY_DESCRIPTION="Toon een vink veld onder aan het formulier voor gebruikers om een kopie naar zichzelf te sturen." +MOD_JDSCF_SINGLE_SEND_COPY_DESCRIPTION="'Ja': Toon een vink veld onder aan het formulier voor gebruikers om een kopie naar zichzelf te sturen. 'Custom': Verstuur een aangepaste mail bericht naar de bezoeker." MOD_JDSCF_SINGLE_SEND_COPY_EMAIL_FIELD_LABEL="Vul een label in voor het Email Veld" MOD_JDSCF_SINGLE_SEND_COPY_EMAIL_FIELD_DESC="Vul de naam in van het formulier veld van de Formulier Opmaak op basis waarvan je een kopie wilt versturen." MOD_JDSCF_SINGLE_SEND_COPY_LABEL="Vul een Stuur Kopie label in" MOD_JDSCF_SINGLE_SEND_COPY_DESC="Label om te tonen voor het vink veld." +MOD_JDSCF_SINGLE_SEND_COPY_FROM_LBL="Van Bedankt email" +MOD_JDSCF_SINGLE_SEND_COPY_FROM_DESC="Vul het Van email adres in van de Bedankt mail" +MOD_JDSCF_SINGLE_SEND_COPY_NAME_LBL="Van naam van de Bedankt mail" +MOD_JDSCF_SINGLE_SEND_COPY_NAME_DESC="Vul de naam in die getoond wordt als afzender van de Bedankt email." +MOD_JDSCF_SINGLE_SEND_COPY_SUBJECT_LBL="Email beschrijving van de Bedankt mail" +MOD_JDSCF_SINGLE_SEND_COPY_SUBJECT_DESC="Onderwerp van de email (Je kunt {field:label} & {field:value} gebruiken om dynamische waardes in dit veld te tonen)." +MOD_JDSCF_SINGLE_SEND_COPY_REPLY_TO_LBL="Reply-to Email van de Bedankt mail" +MOD_JDSCF_SINGLE_SEND_COPY_REPLY_TO_DESC="Vul het Reply-to email adres in voor de Bedankt mail." MOD_JDSCF_SINGLE_SEND_COPY_TEMPLATE_LBL="Bedankt Email Template" MOD_JDSCF_SINGLE_SEND_COPY_TEMPLATE_DESC="Selecteer de email template voor de email die naar het mailadres van de bezoeker wordt gestuurd. De default template toont alle velden in de volgorde zoals ze in het formulier getoond worden volgens het formaat
{field:label}: {field:value}
." MOD_JDSCF_SINGLE_SEND_COPY_CUSTOM_TEMPLATE_LBL="Eigen bedankt Email Template"