Welcome to the LimeSurvey Community Forum

Ask the community, share ideas, and connect with other LimeSurvey users!

Email Response table via API Call

More
7 years 3 weeks ago #188433 by blocka
I'm sharing the following in case anyone needs a method to send an email containing survey responses. My solution is pretty basic. You need to pass the API the survey ID and the email address to send the results to.

If anyone feels inspired to enhance this to send the response data as an Excel attachment, hey, I won't complain!

Code:
<?php // Note you might need to update with the full path require_once 'jsonRPCClient.php'; // update the following with info relevant to your account define( 'LS_BASEURL', 'https://survey.example.com'); define( 'LS_USER', 'admin' ); define( 'LS_PASSWORD', 'password' ); // gets the surveyID form the API call $iSurveyID = $_GET["iSurveyID"]; // gets the To email address from the API call $to = $_GET["SendTo"]; // Instantiate a new RPC client $myJSONRPCClient = new jsonRPCClient( LS_BASEURL.'/index.php/admin/remotecontrol' ); // Get a session key $sSessionKey= $myJSONRPCClient->get_session_key( LS_USER, LS_PASSWORD ); if(is_array($sSessionKey)) { // Invalid session echo $sSessionKey['status']; } else if($sSessionKey) { // Valid session $surveyResponses = $myJSONRPCClient->export_responses($sSessionKey, $iSurveyID, 'json', null, 'completed', 'code', 'short', null, null, null); if(is_array($surveyResponses)) { // Oops, print any errors print_r($surveyResponses); } else { // Decode the retuned base-64 string and convert to an array $decodedString = base64_decode($surveyResponses); $aResponses = json_decode($decodedString, True); // Email Responses: // Find the first response ID $aFirstResponse = reset($aResponses['responses'][0]); $eBody = '<html><body>'; $eBody = '<table style="border-collapse: collapse; text-align: left;">'; $eBody .= '<tr>'; // Insert column headers foreach($aFirstResponse as $key => $value) { $eBody .= '<th style="border: 1px solid #CCC; padding: 2px 7px;">'.$key .'</th>'; } $eBody .= '</tr>'; foreach($aResponses['responses'] as $key => $row) { $eBody .= '<tr>'; // Insert the data foreach(reset($row) as $key => $item) { $eBody .= '<td style="border: 1px solid #CCC; padding: 2px 7px;">'.$item .'</td>'; } $eBody .= '</tr>'; } $eBody .= '</table></body></html>'; // Uncomment if you want to see output in web browser //echo $eBody; // Comment following out if you just want results displayed in browser $subject = 'Response Export'; $headers = "From: <noreply> noreply@example.com" . "\r\n"; $headers .= "Reply-To: ". strip_tags($_POST['req-email']) . "\r\n"; $headers .= "MIME-Version: 1.0\r\n"; $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n"; mail($to, $subject, $eBody, $headers); } } // Release the session key $myJSONRPCClient->release_session_key( $sSessionKey ); ?>
The topic has been locked.
Moderators: tpartner, holch

Lime-years ahead

Online-surveys for every purse and purpose