We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 27672
    • 168 Posts
    i’ve been bcc’ing myself on all eForm contacts to keep a record of them, but ideally i want to have a mysql copy of each email. i know eForm2db exists, but that seems to be more about creating a table with columns for each field in the form. i just want a backup of the email. so i wrote the following addition to eForm, which creates a table with fields `timestamp`, `to`, `from`, `fromname`, `replyto`, `cc`, `bcc`, `subject`, and `body`. $body is the result of the &report chunk being processed by eForm, so it should contain all the data from your form.

    (in ’eform.inc.php’ around line #455 in v1.4.4.6):
    ##################################
    ## CUSTOM - RECORD MAIL IN DATABSE
    ##################################
    	$backupTableFull = $modx->getFullTableName('eform_backup');
    	$backupTable = explode('.', $backupTableFull);
    	
    	##################################################
    	## CHECK IF CUSTOM TABLE EXISTS. IF NOT, CREATE IT
    	##################################################
    		$tableNames = $modx->db->query("SHOW TABLES");
    		while($eachTable = $modx->db->getRow($tableNames, 'num')){
    			$allTables[] = $eachTable[0];
    		}
    		if(!in_array($backupTable, $allTables)){
    			$createTable = $modx->db->query("CREATE TABLE IF NOT EXISTS ".$backupTableFull." (`id` int(11) unsigned NOT NULL auto_increment,`timestamp` datetime NOT NULL,`to` text NOT NULL,`from` text NOT NULL,`fromname` text NOT NULL,`replyto` text NOT NULL,`cc` text NOT NULL,`bcc` text NOT NULL,`subject` text NOT NULL,`body` text NOT NULL,PRIMARY KEY  (`id`))");
    			if(!$createTable){
    				return $this->FormatMessage($modx->db->getLastError);
    			}
    		}			
    		
    	##################
    	## INSERT THE DATA
    	##################
    		$backupSQL = "INSERT INTO ".$backupTableFull." (`timestamp`,`to`,`from`,`fromname`,`replyto`,`cc`,`bcc`,`subject`,`body`) VALUES (NOW(),'".$modx->db->escape($to)."','".$modx->db->escape($from)."','".$modx->db->escape($fromname)."','".$modx->db->escape($replyto)."','".$modx->db->escape($cc)."','".$modx->db->escape($bcc)."','".$modx->db->escape($subject)."','".$modx->db->escape($report)."')";
    		$modx->db->query($backupSQL);
    ####################################
    ## / CUSTOM - RECORD MAIL IN DATABSE
    ####################################


    i just copied the ’eForm’ snippet, and all the code, and renamed it to ’eFormDB’, so in my resources i just call ’[!eFormDB? ... !]’ instead of ’[!eForm? ... !]’.
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      That’s what eForm’s events are for. Rather than hack the core eForm, use its eFormOnMailSent event to gather up the data provided by the &fields array, organize it the way you want, and store it. Since you only want to store it, not manipulate it in eForm, it doesn’t need to be a reference. Of course, it would be a bit more complex since you would have to put the $fields values together into your mail’s body structure yourself, but it’s still better than hacking eForm. You would just re-create your report template, using the $fields[’...’] values instead of placeholders.
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 27672
        • 168 Posts
        i had thought of doing an eFormOnMailSent function, however on our sites we use eForm quite frequently, which would require calling the function/snippet call in quite a few places, and would also require any future eForm calls to have the new code, which to me seems to be more of a "hack" (or at least more work, and less modular) than altering the base code, especially when i have other people managing modx as well - "okay everyone, make sure everytime you make an eForm you remember to do this other thing as well"

        i know editing the core code isn’t usually ideal, but i feel in this case it makes the most sense. i copied the entire eForm code, and renamed it and the snippet to eFormDB, so i just call [!eFormDB? ... !]. that way when i update modx, if eForm gets updated, it won’t break eFormDB (until i can fix it). it’d be a nice feature to the core of eForm i think (when is having a backup ever a bad thing?). [!eForm? ... &dbrecord=`1`!] smiley
          • 28042 ☆ A M B ☆
          • 24,524 Posts
          In that case, I take it back wink
            Studying MODX in the desert - http://sottwell.com
            Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
            Join the Slack Community - http://modx.org
            • 27672
            • 168 Posts
            there’s a reason for my madness! grin