AcyMailing Developer Documentation

Create an AcyMailing plugin to insert content in your emails

Here is an example of AcyMailing plugin you can download, it is fully commented:

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: Joomla: administrator / components / com_acym / dynamics / WordPress: wp-content / uploads / acymailing / addons /

Other methods you can use in your plugin

When the user is created

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
}

When the user is modified

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
}

When the user is deleted

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
}

When the user subscribes to a list

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
}

When the user unsubscribes from a list

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
}

Code API for quick integration

Before each of the following examples, please make sure you load the AcyMailing library.

Joomla
WordPress
Joomla
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;
}
WordPress
if(!include_once(WP_PLUGIN_DIR.DS.'acymailing'.DS.'back'.DS.'helpers'.DS.'helper.php')){
echo 'This code can not work without the AcyMailing Component';
return false;
}

How to load the CMS API

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
WordPress
Joomla

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');
WordPress

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');

Get the AcyMailing mailing lists

$listClass = acym_get('class.list');
$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(array(22, 48));

Insert a user in AcyMailing

$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 = acym_get('class.user');
$userId = $userClass->save($myUser, $customFields); // this function will return you the ID of the user inserted in the AcyMailing table

Get a user and / or its subscription

$userClass = acym_get('class.user');
// 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);

Subscribe or remove a user from one or several lists

$subscribe = array(2,4,6); // Id of the lists you want the user to be subscribed to (can be empty)
$unsubscribe = array(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 = acym_get('class.user');
$newSubscription = array();
if(!empty($subscribe)){
$userClass->subscribe($acyUserId, $subscribe);
}
if(!empty($remove)){
$userClass->unsubscribe($acyUserId, $unsubscribe);
}

Create a Campaign

// Lists you want to attach the campaign to
$listIds = array(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 = 'example@test.com' // BCC for your campaign
$mailClass = acym_get('class.mail');
$mailId = $mailClass->save($mail);
$campaign = new stdClass();
$campaign->draft = 1; // Create as a draft
$campaign->mail_id = $mailId;
$campaignClass = acym_get('class.campaign');
$campaignId = $campaignClass->save($campaign);
$campaignClass->manageListsToCampaign($listIds, $mailId);

Insert an e-mail in the queue

// 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)
$queueClass = acym_get('class.queue');
$queueClass->addQueue($acyUserId, $mailId, $senddate);
// If you want to send a campaign to all the users in its attached lists, you can use this:
$queueClass->queue($mailId, $senddate);

Send a Newsletter to a single user

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 Newsletter ID 67 to the user "user@example.com"):

$mailer = acym_get('helper.mailer');
$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, 'user@example.com'); // The first parameter is the ID of the email you want to send

Create a list

$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 = acym_get('class.list');
$listId = $listClass->save($newlist); // This function will create the list and return the ID of the created list

Delete a list

// This code will detach the list from your campaigns, delete the user subscriptions to that list then delete the list
$listClass = acym_get('class.list');
$listClass->delete(43); // You should replace 43 by the ID of the list you want to delete