Code for Dynamic Email

wayne.regehr 03 Jan, 2014
I am passing a variable containing the 'email to' email using the Custom Code area (v5). At the moment I am then passing the variable to a hidden field, which I then specify to be the field containing the value for the "Dynamic Email". This works just fine and does not display my email on screen, however in the source code the email is still shown.

I would like to keep the email hidden from possible bots and/or scrappers. Is there a code snippet to set the "Dynamic Email" directly using the Custom Code section? If not are there any alternatives to accomplish this goal?
wayne.regehr 03 Jan, 2014
I added two Custom Code pieces as suggested, one for On Load and one for On Submit. This was keeping the email hidden (even on source) however the result was a Mailer Error: You Must provide at least one recipient email address.

Just to be clear, what is the code snippet for setting the "Dynamic Email" field value.
GreyHead 03 Jan, 2014
Hi Wayne,

What method are you using to get it On Load? Could you do the same thing On Submit instead?

Bob
wayne.regehr 03 Jan, 2014
I attempted to execute both code snippets On Submit (using Custom Code). Returns same Mailer Error. I ensured the custom code is executed before the Email action.

In the suggested code I am a little confused.
$_SESSION['_default']['email'] = $email_addr;

This part makes sense, set a session variable equal to my email variable (replacing $email_addr with variable name) for later use.

$form->data['email_addr'] = $_SESSION['_default']['email'];

What does the $form->data string do exactly? I'm supposed to replace email_addr with my variable name, how does this set the From Email or Dynamic Email field value?
GreyHead 04 Jan, 2014
Hi Wayne,

I have a different question - where does $email_addr, or the email it represents, come from in the first place??? That makes a difference about how you handle it.

Bob
wayne.regehr 06 Jan, 2014
Thanks for the explanation Sloanthrasher.

Bob, the $email_addr variable is coming from a K2 Extra Field. I have a DB lookup, that isolates my variable from the multiple strings in that table, that is then parsed.

Here is the code retriving my variable from the table:
<?php

// Get ID
$contactID = $_SESSION['emailid'];

// Table Lookup
$db =& JFactory::getDBO();
$db->setQuery('SELECT extra_fields FROM #__k2_items WHERE id="'.$contactID.'"');

// Load Table Lookup Results
$tabledata = $db->loadResult();

// Explode Table Data into Array
$extrafieldsarray = (explode('},{',$tabledata));

// For Each Exploded Array
foreach ($extrafieldsarray as $value) {


	// Find Where ID = 19 (Email value)
	if(preg_match('"19"',$value))
	{ $contactEmail = $value;
	  unset($extrafieldsarray);
	};
};

$patterns = array ();
$patterns[1] = '/\[/';
$patterns[2] = '/\]/';
$patterns[3] = '/{/';
$patterns[4] = '/}/';
$patterns[5] = '/"/';
$patterns[6] = "/:/";
$patterns[7] = "/,/";
$patterns[8] = "/id/";
$patterns[9] = "/19/";
$patterns[10] = "/value/";

$replacements = "";
$sendtoEmail = preg_replace ($patterns, $replacements, $contactEmail);

?>


When I used that same variable as a hidden input field and set that field name to use as Dynamic Email, it worked just fine. The issue I ran into was trying to keep the email hidden from possible scrappers, as the email was not visually displayed on screen but displayed in the page source as text.
GreyHead 07 Jan, 2014
Answer
Hi Wayne,

The DB look up should work equally well in the On Submit event but to use the value you need to add it to the $form->data array otherwise it will be lost at the end of the Custom Code action.

Also it looks as though you are having to work hard to extract a value from a JSON encoded array. Please try something like this:
<?php
// Get ID
$session =& JFactory::getSession();
$contactID = $session->get('emailid', '');
if ( !$contactId ) {
  return;
}

// Table Lookup
$db =& JFactory::getDBO();
$query = "
  SELECT `extra_fields` 
    FROM `#__k2_items` 
    WHERE `id` = '{$contactID}' ;
";
$db->setQuery($query);

// Load Table Lookup Results
$extrafieldsarray = $db->loadResult();

// Explode Table Data into Array
$extrafieldsarray = json_decode($extrafieldsarray);
$form->data['sendtoEmail'] = $extrafieldsarray[19];
?>
I'm not sure exactly how the json encoded values are set up so this may need tweaking a bit.

Bob
wayne.regehr 07 Jan, 2014
Thanks for the assistance Bob and Sloanthrasher. Everything is working as intended now.

FYI - Enjoy a cold beer on me gentlemen!
wayne.regehr 07 Jan, 2014
Hmm..

You may or may not know this already, however when attempting to buy you a beer the page in question was having issues loading. Also the customer satisfaction survey returned a 404. I will try again later.
GreyHead 07 Jan, 2014
Hi Wayne,

Thanks for the heads-up. I've removed the links from my signature for the moment. It looks as though the satisfaction survey hasn't been upgraded to the new ChronoForms site. And my site is slowly dying, the back end is fine but the front-end is getting slower and slower :-( I must find the time to upgrade that one too.

Bob
This topic is locked and no more replies can be posted.