Forums

email is sent even if template is disabled

RichardC 07 Sep, 2010
Hi
I have managed to setup a form and it's working (almost) fine.
The form has a checkbox where the user can decide if a resulting email is sent to his email-address or not.
No matter what the status of checkbox the email is always sent.
Here's the code I use in OnSubmit-before:
<?php
$emails_2 = array('Info'=>'info@mail.tld', 'Sales'=>'sales@mail.tld', 'Purchasing'=>'Purchasing@mail.tld', 'Buchhaltung'=>'Buchhaltung@mail.tld');
/*
$MyForm =& CFChronoForm::getInstance('myContactForm');
$MyFormEmails =& CFEMails::getInstance($MyForm->formrow->id);
$MyFormEmails->setEmailData(1, 'to', $emails_2[$_POST['my_recipient']]);
*/
$_POST['datecreated_EN'] = date("l , F j, Y");
$_POST['timecreated_EN'] = date("h:i A");
$_POST['datecreated_DE'] = strftime("%A, %d. %B %Y");
$_POST['timecreated_DE'] = strftime("%H:%M Uhr");

$_POST['my_mail2'] = $emails_2[$_POST['my_recipient']];
$_POST['my_fromname'] = "my from name (" . $_POST['my_recipient'] . ")";
$_POST['visitor_fullname'] = $_POST['visitor_firstname'] . " " . $_POST['visitor_name'];
$_POST['subject'] = $_POST['visitor_subject'] . " (via our Website)";

$MyForm =& CFChronoForm::getInstance('myContactForm');
$MyFormEmails =& CFEMails::getInstance($MyForm->formrow->id);
$check0 = JRequest::getString('check0', '', 'post');

if ( ! $check0 ) {
  $MyFormEmails->setEmailData(2, 'enabled', false);
  $_POST['text_mailsent2you'] = "no email copy requested";
} else {
  $_POST['text_mailsent2you'] = " a copy of this email was sent to " . $_POST['visitor_email'];
}

$sess =& JFactory::getSession();
$sess->set('my_mail2', $_POST['my_mail2']);
$sess->set('text_mailsent2you', $_POST['text_mailsent2you']);

?> 


So, if check0 is NOT checked I disable template 2 (at least this is my intention :-))
Any ideas on why the email is sent eventhough template is disabled?
Thanks
RichardC
GreyHead 07 Sep, 2010
Hi RichardC,

That looks OK from a quick review of the code.

Have you checked what 'check0' is returning? It might be worth turning Form Debug on to take a look.

Bob

PS This is written up in more detail in Chapter 12 of The ChronoForms Book. I used the opposite approach of disabling the Email Setup by default and enabling it if the box was checked - but I don't think that this makes a significant difference.
RichardC 07 Sep, 2010
Hi Greyhead,
thanks for your reply.
>>Have you checked what 'check0' is returning? It might be worth turning Form Debug on to take a look.
Well, if the checkbox is not checked it returns nothing(NULL?) At least it doesn't show up in Debug.
When it is checked Debug reports:

[check0] => Array ( [0] => SendCopy ).


Unfortunattly I'm not a PHP guru so I don't know if my check in code " if ( ! $check0 ) { " might be insufficient.
Maybe you could help.
Thanks
RichardC
GreyHead 07 Sep, 2010
Hi RichardC,

Very odd for MooTools not to be loaded. This code snippet in the Form HTML should force it - but I suspect something else may be blocking it
<?php
if ( !$mainframe->isSite() ) { return; }
JHTML::_('behavior.mootools');
?>


Bob
RichardC 09 Sep, 2010
Hi Bob,

I fear I don't understand your suggestion. I've tried it though and it didn't change anthing.
Hmhhhh, any other ideas?
Cheers
RichardC
RichardC 09 Sep, 2010
Hi Bob,
just to make it clear:
In my Before-Submit code I have:
if ( ! $check0 ) {
  $MyFormEmails->setEmailData(2, 'enabled', false);
  $_POST['text_mailsent2you'] = "no email copy requested";
} else {
  $_POST['text_mailsent2you'] = " a copy of this email was sent to " . $_POST['visitor_email'];
}

I do end up with the text "no email copy requested" in my confirmation screen.
So the code walks thru the true part of the if ( ! $check9 )
It's just that the call to disable the email template doesn't seem to work
$MyFormEmails->setEmailData(2, 'enabled', false);


Other calls to setEmailData work just fine - like
$MyFormEmails->setEmailData(1, 'to', $emails_2[$_POST['cedp_recipient']]);


I will try to work the other way around (have template 2 initally disabled and enable it if check0 is checked) but maybe you can come up with another idea

Cheers
RichardC
RichardC 09 Sep, 2010
Hi Bob,

tried the other way around (template2 initially disabled and programmatically enable it).
It doesn't work.
Now email2 is not sent at all (neither if check0 is checked nor if it's unchecked).

Mhmmm
RichardC
RichardC 10 Sep, 2010
Hi Bob,

I found the cause of the problem:
$MyForm =& CFChronoForm::getInstance('myContactForm');
I specified the wrong formname :-((((

Thanks for your help anyway.
RichardC
This topic is locked and no more replies can be posted.