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:
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:
You can use this script as a base, and modify it to meet your needs:
// Load the AcyMailing library$postData = $_REQUEST['form'];$ds =DIRECTORY_SEPARATOR;include_oncertrim(JPATH_ADMINISTRATOR, $ds).$ds.'components'.$ds.'com_acym'.$ds.'helpers'.$ds.'helper.php';$userClass =newAcyMailing\Classes\UserClass();// Build the user based on your form's fields$myUser =newstdClass(); $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);
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":
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
// The user now exists in AcyMailing, let's add some list subscriptionsif (!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);}
Use a Checkbox field to choose the lists individually
// 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 newsif (in_array('Weekly news', $postData['lists'])) $subscribe[] =5;if (in_array('Daily news', $postData['lists'])) $subscribe[] =6; $userClass->subscribe($userId, $subscribe);}