# RSForm!Pro integration

### Description

If you use RSForm!Pro on your website, you can integrate AcyMailing with it so that when a user submits a form, this user is also subscribed to one or several AcyMailing Lists.

### Integrate using PHP code

You can configure RSForm!Pro to execute a php script to subscribe the user in AcyMailing.\
First of all, edit your form and go in the PHP Scripts section:

![](https://963725040-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-L_D4iy53WFAtKVs3BPA%2F-MRP6PR2DSp2_1xHiT80%2F-MRP72BiYsHvjyPbittf%2FRSForm!%20Pro%20script.png?alt=media\&token=016df31c-8ea9-48c7-a696-81ec8579f940)

The script should go in the **Script called after form has been processed** area to ensure the user isn't subscribed if an error occurs on the form:

![](https://963725040-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-L_D4iy53WFAtKVs3BPA%2F-MRP6PR2DSp2_1xHiT80%2F-MRP7IRhkYYjN4KxXgog%2FRSForm!%20Pro%20script%202.png?alt=media\&token=8c181fa0-579e-420d-880a-2876049de089)

You can use this script as a base, and modify it to meet your needs:

{% code lineNumbers="true" %}

```php
// Load the AcyMailing library
$postData = $_REQUEST['form'];
$ds = DIRECTORY_SEPARATOR;
include_once rtrim(JPATH_ADMINISTRATOR, $ds).$ds.'components'.$ds.'com_acym'.$ds.'Core'.$ds.'init.php';
$userClass = new AcyMailing\Classes\UserClass();

// Build the user based on your form's fields
$myUser = new stdClass(); 
$myUser->email = strip_tags($postData['EMAIL_FIELD']);
$myUser->name = strip_tags($postData['NAME_FIELD']);

// If the user already exists update it
$existingUser = $userClass->getOneByEmail($postData['EMAIL_FIELD']);
if (!empty($existingUser)) $myUser->id = $existingUser->id;

// You can add as many extra fields as you want if you already created them in AcyMailing
$customFields = [];
$customFields[CUSTOM_FIELD_ID] = $postData['MY_FIELD']; // the custom field id can be found in the Custom fields list of the AcyMailing admin page


$userClass->sendConf = true; // Or false if you don't want a confirmation email to be sent
$userId = $userClass->save($myUser, $customFields);


// The user now exists in AcyMailing, let's add some list subscriptions
$subscribe = [2,5,6]; // Ids of the lists you want the user to be subscribed to need to be added in this array
$userClass->subscribe($userId, $subscribe);
```

{% endcode %}

In this code, `CUSTOM_FIELD_ID` is the ID that can be seen in the Acy custom fields listing.

EMAIL\_FIELD, NAME\_FIELD and MY\_FIELD must be replaced by your form's field names, so in this example it would be "Email", "the user name" and "Our newsletters field name":

![](https://963725040-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-L_D4iy53WFAtKVs3BPA%2F-MRP6PR2DSp2_1xHiT80%2F-MRPDthSuaKWFC0q_iHa%2FRSForm!%20Pro%20script%203.png?alt=media\&token=25b04417-5706-4544-baaa-6bd76f0dbbfb)

### Let users choose their subscription

You can add a Radio Group field or a Checkbox Group field to let users choose their subscription. Here are some examples:

### Use a Radio Group field as a Yes/No subscription field

![](https://963725040-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-L_D4iy53WFAtKVs3BPA%2F-MRPEOHCuPdaZUo7If2j%2F-MRPF5pt3AIu1x81QGpi%2FRSForm!%20Pro%20newsletters.png?alt=media\&token=800e48fd-afee-447a-b80d-2e71ffadc065)

{% code lineNumbers="true" %}

```php
// The user now exists in AcyMailing, let's add some list subscriptions

if (!empty($postData['newsletters']) && $postData['newsletters'] === 'Yes') {
    $subscribe = [2,5,6]; // Ids of the lists you want the user to be subscribed to need to be added in this array
    $userClass->subscribe($userId, $subscribe);
}
```

{% endcode %}

### Use a Checkbox field to choose the lists individually

![](https://963725040-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-L_D4iy53WFAtKVs3BPA%2F-MRPEOHCuPdaZUo7If2j%2F-MRPG1G6zev_17OF-yaZ%2FRSForm!%20Pro%20newsletters%20list.png?alt=media\&token=92150488-4ffb-439a-a8a9-9dd0393804e0)

{% code lineNumbers="true" %}

```php
// The user now exists in AcyMailing, let's add some list subscriptions

$subscribe = [];

if (!empty($postData['lists'])) {
    if (in_array('Monthly news', $postData['lists'])) $subscribe[] = 2; // 2 being the list ID corresponding to the monthly news
    if (in_array('Weekly news', $postData['lists'])) $subscribe[] = 5;
    if (in_array('Daily news', $postData['lists'])) $subscribe[] = 6;
    
    $userClass->subscribe($userId, $subscribe);
}
```

{% endcode %}
