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]
      [cc]
      [bcc]
      [from]
      [replyto]
      [fromName]
      [text]
      [subject]
      [mode]
      [files]
 

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 field's placeholders that have a submitted value to the generated RSForm!Pro email(in this case, the Admin Email). This will require a script that will dynamically add and check your form components, the script is added in the "Script called before the Admin Email is sent":

 
    $modAdminEmailText = $form->AdminEmailText;
    foreach($_POST['form'] as $component=>$value){
      if(is_array($value)) $value = implode(',',$value);
        if($value != '' && $component != 'formId'){
        $modAdminEmailText .= '{'.$component.':caption}: {'.$component.':value}'."<br/>";
        }
    }
    $adminEmail['text'] = $modAdminEmailText;
    $adminEmail['text'] = str_replace($placeholders, $values, $adminEmail['text']);
 

22 persons found this article helpful.


Was this article helpful?

Yes No
Sorry about that

You Should Also Read

Component Emails HOT

Plugin - RSMail! (Create custom newsletter subscription forms) HOT

Additional emails

Plugin - External Mailer (Use different email settings and add DKIM support)

How to avoid email validation when using RSForm!Pro - RSMail! integration plugin