PHP Email Scripts
As a newly introduced feature, the PHP Email Scripts section offers an increased flexibility in the terms of dynamic email content.
The scripts can be applied to all the RSform!Pro generated emails: User, Admin and Additional Emails. The email can be altered via the $userEmail, $adminEmail and $additionalEmail variables.
These are standard PHP Array variables that contain the following fields:
[to] - the address where the email is sent to [recipientName] - the name of the TO address [cc] - CarbonCopy address [bcc] - BlindCarbonCopy address [from] - the address where the email is sent from [fromName] - the name of the FROM address [replyto] - the address the email will be replied to (when clicking REPLY TO from your email client) [text] - email contents/body [subject] - email subject [mode] - plain text or HTML [files] - files attachments
Examples:
1) Let's assume that you wish to change the recipient for the user email based on a drop-down value that has been submitted:
if($_POST['form']['name_of_drop_down'][0] == 'Sample value here') $userEmail['to'] = 'email@domain.com';
2) Let's assume that you wish to remove from the admin email message the placeholders of the fields that were not filled by the user.
/* Retrieve the admin email text */ $modAdminEmailText = $form->AdminEmailText; /* Verify the content of the field and remove the placeholders if no value is submitted */ if($_POST['form']['your_field_name'] == '') $modAdminEmailText = str_replace('{your_field_name:caption}: {your_field_name:value}','',$modAdminEmailText); if($_POST['form']['your_field_name2'] == '') $modAdminEmailText = str_replace('{your_field_name2:caption}: {your_field_name2:value}','',$modAdminEmailText); /*After the placeholders contained in the body of the email message are adjusted according to the user's input, we will need to replace them with the actual submitted values. This is done using */ $adminEmail['text'] = $modAdminEmailText; $adminEmail['text'] = str_replace($placeholders, $values, $adminEmail['text']);
On a similar note, below you can find an example of PHP Script that checks if the field has a value and if so, add it to the email body
$modAdminEmailText = $form->AdminEmailText; /*Checks if the field has a value and if so, add it to the Email Text*/ if($_POST['form']['your_field_name'] != '') $modAdminEmailText .= '{your_field_name:caption}: {your_field_name:value}'; if($_POST['form']['your_field_name2'] != '') $modAdminEmailText .= '{your_field_name2:caption}: {your_field_name2:value}'; /*After the field placeholders are added we will need to replace them with the actual submitted values.*/ $adminEmail['text'] = $modAdminEmailText; $adminEmail['text'] = str_replace($placeholders, $values, $adminEmail['text']);
Note:
This process can be simplified by adding a custom {if} syntax for the text of the User Email, Admin Email, Additional Emails and the Thank you message. This syntax is based on a simple if condition and is explained here!
3) Let's assume you want to automatically add all the fields' placeholders of your form to the generated RSForm!Pro email within a tabular layout (in this case, the User Email). This will require a script that will dynamically add and check your form's placeholders; the script is added in the "Script called before the User Email is sent" area:
//replace below, my-Submit-Button-Name-here with the exact name you've given to your submit button (case sensitive) $nameOfSubmitButton = "my-Submit-Button-Name-here"; $counter = 0; $placeholderArray = array_combine($placeholders, $values); $emailBody = '<table>'; foreach($placeholderArray as $placeholder => $value){ //the following IF will exclude fields with empty values if(strstr($placeholder, 'global:') === false){ if(strstr($placeholder, ':') !== false){ $placeholderName = explode(':',$placeholder)[0]; if($placeholderArray[$placeholderName.':value}'] == ''){ continue; } } } if((strstr($placeholder,":caption") || strstr($placeholder,":value")) && !strstr($placeholder,"{".$nameOfSubmitButton.":")){ if($counter == 0){ $emailBody .= "<tr><td>".$value."</td>"; $counter++; }else{ $emailBody .= "<td><strong>".$value."</strong></td></tr>"; $counter = 0; } } } $emailBody .= "</table>"; $modUserEmailText = $form->UserEmailText; $modUserEmailText .= $emailBody; $userEmail['text'] = $modUserEmailText;
47 persons found this article helpful.