• 1

Read this first!

We do not monitor these forums. The forum is provided to exchange information and experience with other users ONLY. Forum responses are not guaranteed.

However, please submit a ticket if you have an active subscription and wish to receive support. Our ticketing system is the only way of getting in touch with RSJoomla! and receiving the official RSJoomla! Customer Support.

For more information, the Support Policy is located here.

Thank you!

TOPIC: Creating CSV from form and attach to Admin email

Creating CSV from form and attach to Admin email 13 years 10 months ago #10900

Our company website was maintained by an outside design company who altered the components -> com_rsform -> controller -> functions.php file so that a CSV would be created and then submitted as an attachment to the Admin e-mail that is sent out. Upon updating our RSFormPro plug-in, I've broken that code and it doesn't seem to be an easy fix. I've tried to enter the below code into the new functions.php file, but uploading the altered file brought the whole site down. Good thing I made a backup.

I would ideally like to move the code to the each RSFormPro form's Script tab (Script called after form has been processed or Script called on form process?) so that I don't have to worry about future updates to the plugin, however, I'm not that familiar with how to modify the script. Each form could then have its own If/Then statement.

Could anybody assist me with this task?

The old code is shown below:
...
			//Trigger Event - onAfterStoreSubmissions
			$mainframe->triggerEvent('rsfp_f_onAfterStoreSubmissions', array(array('SubmissionId'=>$SubmissionId,'formId'=>$formId)));
			//if(defined('_RSFORM_PLUGIN_MAPPINGS')) RSmappingsWriteSubmissions($formId, $SubmissionId);
			//die();				
 
			$db->setQuery("SELECT * FROM #__rsform_forms WHERE FormId='".intval($_POST['form']['formId'])."'");
 
			$r = $db->loadAssoc();
 
			$userEmail['to']=RSprocessField($r['UserEmailTo'],$SubmissionId);
			$userEmail['cc']=RSprocessField($r['UserEmailCC'],$SubmissionId);
			$userEmail['bcc']=RSprocessField($r['UserEmailBCC'],$SubmissionId);
			$userEmail['subject']=RSprocessField($r['UserEmailSubject'],$SubmissionId);
			$userEmail['from']=RSprocessField($r['UserEmailFrom'],$SubmissionId);
			$userEmail['replyto']=RSprocessField($r['UserEmailReplyTo'],$SubmissionId);
			$userEmail['fromName']=RSprocessField($r['UserEmailFromName'],$SubmissionId);
			$userEmail['text']=RSprocessField($r['UserEmailText'],$SubmissionId);
			$userEmail['mode']=$r['UserEmailMode'];
 
			$adminEmail['to']=RSprocessField($r['AdminEmailTo'],$SubmissionId);
			$adminEmail['cc']=RSprocessField($r['AdminEmailCC'],$SubmissionId);
			$adminEmail['bcc']=RSprocessField($r['AdminEmailBCC'],$SubmissionId);
			$adminEmail['subject']=RSprocessField($r['AdminEmailSubject'],$SubmissionId);
			$adminEmail['from']=RSprocessField($r['AdminEmailFrom'],$SubmissionId);
			$adminEmail['replyto']=RSprocessField($r['AdminEmailReplyTo'],$SubmissionId);
			$adminEmail['fromName']=RSprocessField($r['AdminEmailFromName'],$SubmissionId);
			$adminEmail['text']=RSprocessField($r['AdminEmailText'],$SubmissionId);
			$adminEmail['mode']=$r['AdminEmailMode'];
 
			// todo - change rsadapter->mail to jutility::sendmail
			//mail users
			$recipients = explode(',',$userEmail['to']);
 
			if ($r['UserEmailAttach'] && file_exists($r['UserEmailAttachFile']))
				$userEmail['files'][] = $r['UserEmailAttachFile'];
 
			if(!empty($recipients))
				foreach($recipients as $recipient)
					if(!empty($recipient)){						
						$RSadapter->mail($userEmail['from'], $userEmail['fromName'], $recipient, $userEmail['subject'], $userEmail['text'], $userEmail['mode'], !empty($userEmail['cc']) ? $userEmail['cc'] : null, !empty($userEmail['bcc']) ? $userEmail['bcc'] : null, $userEmail['files'], !empty($userEmail['replyto']) ? $userEmail['replyto'] : '');
					}
 
			//mail admins
			$recipients = explode(',',$adminEmail['to']);
			if(!empty($recipients))
				foreach($recipients as $recipient){
					if(!empty($recipient))
					{
						//must be form id 5
						//if(intval($_POST['form']['formId']) == 8)
						if(intval($_POST['form']['formId']) == 3 || intval($_POST['form']['formId']) == 4 || intval($_POST['form']['formId']) == 8)
						{
							// create the csv string							
							$csv_string_key_string = "";
							$csv_string_val_string = "";						
							foreach ($_POST['form'] as $key => $val)
							{
								$val = (is_array($val) ? implode("\n",$val) : $val);
								$key = RScleanVar($key);
								$val = RScleanVar(RSstripjavaVar($val));
 
								if ($key == "terms")
									$val = "agreed";
 
								if ($key !="formId" && $key !="submit"){
									$csv_string_key_string .= $key . ",";
									$csv_string_val_string .= $val . ",";
								}
							}
							$csv_string_key_string = substr_replace($csv_string_key_string ,"",-1);
							$csv_string_val_string = substr_replace($csv_string_val_string ,"",-1);
 
							if ((!empty($csv_string_key_string)) && (!empty($csv_string_val_string)))
							{
								//Write the csv lines to a file...
								//echo $csv_string_key_string . "<br />";
								//echo $csv_string_val_string;
 
								//get config directory
								$config = new JConfig();
								$tmp_path = $config->tmp_path;
 
								$tmp_path = "/home/encom/encomwireless.com/temp";
 
								$fp = fopen($tmp_path.'temp_rsForm_file.csv', 'w');
								if ($fp)
								{
									//echo 'fopen ok';
									fputcsv($fp, split(',', $csv_string_key_string));
									fputcsv($fp, split(',', $csv_string_val_string));
									fclose($fp);
 
									$adminEmail['files'] = $tmp_path.'temp_rsForm_file.csv';
 
									//send the email with attachment
									if($RSadapter->mail($adminEmail['from'], $adminEmail['fromName'], $recipient, $adminEmail['subject'], $adminEmail['text'], $adminEmail['mode'], !empty($adminEmail['cc']) ? $adminEmail['cc'] : null, !empty($adminEmail['bcc']) ? $adminEmail['bcc'] : null, $adminEmail['files'], !empty($adminEmail['replyto']) ? $adminEmail['replyto'] : ''))
									{
										//echo 'Mail sent OK.';//Mail sent OK.<br />'; 
										//exit;
									}
									else{
										//echo 'Mail send error...';//Mail send error.<br />';
										//exit;
									}
									//delete file
									unlink($tmp_path.'temp_rsForm_file.csv');
									//echo 'The Encom Wireless website is being updated, please check back shortly.';
									//exit;
								}
								else
									echo 'RSForms: error opening temp file to write CSV';
							}							
							//var_dump($_POST['form']);
							//exit;									
						}
					}
					else
					{
						if(!empty($recipient))
							$RSadapter->mail($userEmail['from'], $userEmail['fromName'], $recipient, $userEmail['subject'], $userEmail['text'], $userEmail['mode'], !empty($userEmail['cc']) ? $userEmail['cc'] : null, !empty($userEmail['bcc']) ? $userEmail['bcc'] : null, $userEmail['files'], !empty($userEmail['replyto']) ? $userEmail['replyto'] : '');
					}
				}
 
			$thankYouMessage = RSprocessField($r['Thankyou'],$SubmissionId);
 
			eval($ScriptProcess2);
			//Trigger - After form process
...
Last Edit: 13 years 10 months ago by Quasi_Mojo.
The administrator has disabled public write access.

Re:Creating CSV from form and attach to Admin email 13 years 10 months ago #10982

Well...

I received the following reply upon submitting a ticket:

"Hello,

Essentially you will have to use the actual part that creates the CSV file and attaches it to the email:
// create the csv string
$csv_string_key_string = "";
$csv_string_val_string = "";
foreach ($_POST['form'] as $key => $val)
{
$val = (is_array($val) ? implode("\n",$val) : $val);
$key = RScleanVar($key);
$val = RScleanVar(RSstripjavaVar($val));
 
if ($key == "terms")
$val = "agreed";
 
if ($key !="formId" && $key !="submit"){
$csv_string_key_string .= $key . ",";
$csv_string_val_string .= $val . ",";
}
}
$csv_string_key_string = substr_replace($csv_string_key_string ,"",-1);
$csv_string_val_string = substr_replace($csv_string_val_string ,"",-1);
 
if ((!empty($csv_string_key_string)) && (!empty($csv_string_val_string)))
{
//Write the csv lines to a file...
//echo $csv_string_key_string . "<br />";
//echo $csv_string_val_string;
 
//get config directory
$config = new JConfig();
$tmp_path = $config->tmp_path;
 
$fp = fopen($tmp_path.'temp_rsForm_file.csv', 'w+');
if ($fp)
{
//echo 'fopen ok';
fputcsv($fp, split(',', $csv_string_key_string));
fputcsv($fp, split(',', $csv_string_val_string));
fclose($fp);

You will actually have to use the attachment option from the User email, and set the path:

/home/encom/encomwireless.com/temp/temp_rsForm_file.csv"

I've tried as suggested and I received no attached file. When I go back into the User Emails tab in RSFormPro, I see the following error:

WARNING! Please verify if the file exists on your server.

It appears that the file is not being attached because the file doesn't exist at the time of the form submission. The file doesn't seem to be created, at all.
The administrator has disabled public write access.
  • 1

Read this first!

We do not monitor these forums. The forum is provided to exchange information and experience with other users ONLY. Forum responses are not guaranteed.

However, please submit a ticket if you have an active subscription and wish to receive support. Our ticketing system is the only way of getting in touch with RSJoomla! and receiving the official RSJoomla! Customer Support.

For more information, the Support Policy is located here.

Thank you!