Developer Documentation
This page is meant for developers only. It shows how to develop an add-on for AcyMailing and how to use the AcyMailing code for general needs.
Here is an example of AcyMailing plugin you can download, it is fully commented and meant to explain every part of the code. When developing a new add-on, you could also copy an existing one and just change the SQL queries/names in it if you find it easier.
Joomla
WordPress
example.zip
19KB
Binary
example.zip
Don't forget to replace any reference of "example" by your plugin's name, for example "myplugin".
You can upload the folder of your plugin in: administrator / components / com_acym / dynamics /
acymailing-example.zip
25KB
Binary
acymailing-example.zip
Don't forget to replace any reference of "example" by your plugin's name, for example "myplugin".
You can install the plugin in the WordPress plugins section of your site.
Once added, you will see it in the add-ons page of AcyMailing.
Once your custom add-on is installed in the correct folder, insert a new line in the table
xxxx_acym_plugin
with folder_name = the add-on's folder name, active = 1, type = ADDON.You can use these methods to execute specific actions before/after a user is created
public function onAcymBeforeUserCreate(&$user) {// Your code here}public function onAcymAfterUserCreate(&$user) {// Your code here}
You can use these methods to execute specific actions before/after a user is modified
public function onAcymBeforeUserModify(&$user) {// Your code here}public function onAcymAfterUserModify(&$user) {// Your code here}
You can use these methods to execute specific actions before/after one or several users are deleted
public function onAcymBeforeUserDelete(&$users) {// Your code here, $users is always an array of IDs}public function onAcymAfterUserDelete(&$users) {// Your code here, $users is always an array of IDs}
This method is called after the user subscribes to one or several lists
public function onAcymAfterUserSubscribe(&$user, $lists) {// Your code here, $lists is always an array of list IDs}
This method is called after the user unsubscribes from one or several lists
public function onAcymAfterUserUnsubscribe(&$userID, $lists) {// Your code here, $lists is always an array of list IDs}
Before each of the following examples, please make sure you load the AcyMailing library.
Joomla
WordPress
if(!include_once(rtrim(JPATH_ADMINISTRATOR, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.'components'.DIRECTORY_SEPARATOR.'com_acym'.DIRECTORY_SEPARATOR.'helpers'.DIRECTORY_SEPARATOR.'helper.php')){echo 'This code can not work without the AcyMailing Component';return false;}
if(!include_once(WP_PLUGIN_DIR.DIRECTORY_SEPARATOR.'acymailing'.DIRECTORY_SEPARATOR.'back'.DIRECTORY_SEPARATOR.'helpers'.DIRECTORY_SEPARATOR.'helper.php')){echo 'This code can not work without the AcyMailing Component';return false;}
If you want to use the AcyMailing code outside of your site, you will need to load your site's library then the AcyMailing library.
Joomla 3
Joomla 4
WordPress
Add these lines first in your script and don't forget to replace PATH_TO_YOUR_JOOMLA_SITE_ROOT_FOLDER by the real value
define('_JEXEC', 1);
define('DS', DIRECTORY_SEPARATOR);
if (file_exists(dirname(__FILE__) . '/defines.php')) {
include_once dirname(__FILE__) . '/defines.php';
}
if (!defined('_JDEFINES')) {
define('JPATH_BASE', 'PATH_TO_YOUR_JOOMLA_SITE_ROOT_FOLDER');
require_once JPATH_BASE.'/includes/defines.php';
}
require_once JPATH_BASE.'/includes/framework.php';
$app = JFactory::getApplication('site');
Add these lines first in your script and don't forget to replace PATH_TO_YOUR_JOOMLA_SITE_ROOT_FOLDER by the real value
define('_JEXEC', 1);
define('DS', DIRECTORY_SEPARATOR);
if (file_exists(dirname(__FILE__) . '/defines.php')) {
include_once dirname(__FILE__) . '/defines.php';
}
if (!defined('_JDEFINES')) {
define('JPATH_BASE', 'PATH_TO_YOUR_JOOMLA_SITE_ROOT_FOLDER');
require_once JPATH_BASE.'/includes/defines.php';
}
require_once JPATH_BASE.'/includes/framework.php';
$container = \Joomla\CMS\Factory::getContainer();
$container->alias(\Joomla\Session\SessionInterface::class, 'session.web.site');
$app = $container->get(\Joomla\CMS\Application\SiteApplication::class);
Add these lines first in your script and don't forget to replace PATH_TO_YOUR_WORDPRESS_SITE_ROOT_FOLDER by the real value
define('WP_USE_THEMES', false);
require('PATH_TO_YOUR_WORDPRESS_SITE_ROOT_FOLDER/wp-load.php');
use AcyMailing\Classes\ListClass;
$listClass = new ListClass;
$allLists = $listClass->getAll();
// You can then do a foreach on this variable (which contains an array of objects) and use ->name
// You can get a specific list like this
$myList = $listClass->getOneById(34);
// Or multiple lists like this
$myLists = $listClass->getListsByIds([22, 48]);
use AcyMailing\Classes\UserClass;
$myUser = new stdClass();
$myUser->email = 'emailaddressoftheuser';
$myUser->name = 'myname'; // this information is optional
// If you require a confirmation but don't want the user to have to confirm his subscription, you can set the confirmed field to 1:
// $myUser->confirmed = 1;
// You can add as many extra fields as you want if you already created them in AcyMailing
$customFields = [];
$customFields[custom_field_id] = 'france'; // the custom field id can be found in the Custom fields list of the AcyMailing admin page
//...
$userClass = new UserClass();
$userClass->sendConf = true; // Or false if you don't want a confirmation email to be sent
$userId = $userClass->save($myUser, $customFields); // this function will return you the ID of the user inserted in the AcyMailing table
use AcyMailing\Classes\UserClass;
$userClass = new UserClass();
// Get the AcyMailing user from his email address
$user = $userClass->getOneByEmail($email);
// Or you can get the AcyMailing user from his site user ID
$user = $userClass->getOneByCMSId($userID);
// Get subscription
$userSubscriptions = $userClass->getUserSubscriptionById($user->id);
// If you need a simpler result, you can use this method
$userSubscriptions = $userClass->getSubscriptionStatus($user->id);
use AcyMailing\Classes\UserClass;
$subscribe = [2,4,6]; // Id of the lists you want the user to be subscribed to (can be empty)
$unsubscribe = [1,3]; // Id of the lists you want the user to be unsubscribe from (can be empty)
$acyUserId = '23'; // you can use the previous example code to get the user by its site user id or its email address
$userClass = new UserClass();
if(!empty($subscribe)){
// The last two parameters are are to make sure to send the welcome email
$userClass->subscribe($acyUserId, $subscribe, true, true);
}
if(!empty($remove)){
$userClass->unsubscribe($acyUserId, $unsubscribe);
}
use AcyMailing\Classes\MailClass;
use AcyMailing\Classes\CampaignClass;
// Lists you want to attach the campaign to
$listIds = [3,5,8];
// We create the Campaign element that we will save in the database.
// You can use all fields from the database the same way we do it with subject and body.
$mail = new stdClass();
$mail->subject = 'your subject'; // Subject of your email
$mail->name = 'your campaign name'; // Name of the campaign
$mail->body = 'Hi folks, we have a big announcement...'; // Body of your email
$mail->bcc = '[email protected]'; // BCC for your campaign
$mailClass = new MailClass();
$mailId = $mailClass->save($mail);
$campaign = new stdClass();
$campaign->draft = 1; // Create as a draft
$campaign->mail_id = $mailId;
$campaign->sending_type = CampaignClass::SENDING_TYPE_NOW; // Create a standard campaign
$campaignClass = new CampaignClass();
$campaignId = $campaignClass->save($campaign);
$campaignClass->manageListsToCampaign($listIds, $mailId);
use AcyMailing\Classes\QueueClass;
use AcyMailing\Classes\MailClass;
// The purpose of this code is to let AcyMailing send an e-mail to a specific user at a specific time.
// You just have to add an entry in the queue and AcyMailing will take care of the rest.
$acyUserId = '23';
$mailId = '45'; // ID of the email you want to add in the queue
$sendDate = time(); //When do you want the e-mail to be sent to this user? you should specify a timestamp here (time() is the current time)
// If you want to add the email in the queue only for one specific user
$queueClass = new QueueClass();
$queueClass->addQueue($acyUserId, $mailId, acym_date($sendDate, 'Y-m-d H:i', false));
// If you want to send the email to all the users subscribed to the list the email is attached to, you can use this:
$mailClass = new MailClass();
$mailToSend = $mailClass->getOneById($mailId);
$mailToSend->sending_date = acym_date($sendDate, 'Y-m-d H:i', false);
$mailToSend->parent_id = $mailId;
$mailToSend->sending_params = [''];
$queueClass->queue($mailToSend);
Sometimes you want to send a pre-saved email to a single user only...
In that case you should not bother with the queue system and use this code (it will send the email ID 67 to the user "[email protected]"):
The email ID can be found in the xxxx_acym_mail table, this is NOT the campaign ID displayed on the campaigns listing
use AcyMailing\Helpers\MailerHelper;
$mailer = new MailerHelper();
$mailer->report = true; // set it to true or false if you want Acy to display a confirmation message or not (message successfully sent to...)
$mailer->trackEmail = true; // set it to true or false if you want Acy to track the message or not (it will be inserted in the statistics table)
$mailer->autoAddUser = false; // set it to true if you want Acy to automatically create the user if it does not exist in AcyMailing
$mailer->addParam('var1', 'Value of the variable 1'); // Acy will automatically replace the tag {var1} by the value specified in the second parameter... you can use this function several times to replace tags in your email
$mailer->sendOne(67, '[email protected]'); // The first parameter is the ID of the email you want to send
use AcyMailing\Classes\ListClass;
$newList = new stdClass();
$newList->name = 'Your list name';
$newList->active = 1;
$newList->welcome_id = 43; // Optional. This is the Id of the mail you want to send as welcome email
$newList->unsubscribe_id = 52; // Optional. This is the Id of the mail you want to send when a user unsubscribes
$newList->color = '#3366ff'; // Optional. This is the color of the list
$listClass = new ListClass();
$listId = $listClass->save($newList); // This function will create the list and return the ID of the created list
use AcyMailing\Classes\ListClass;
// This code will detach the list from your campaigns, delete the user subscriptions to that list then delete the list
$listClass = new ListClass();
$listClass->delete(43); // You should replace 43 by the ID of the list you want to delete
Last modified 3mo ago