« Back to Documentation OverviewlistSubscribe – v1.1
listSubscribe(string apikey, string id, string email_address, array merge_vars, string email_type, boolean double_optin)
Subscribe the provided email to a list. By default this sends a confirmation email - you will not see new members until the link contained in it is clicked!
| Parameters | | apikey | a valid API Key for your user account. Get by calling Get by visiting your API dashboard |
| id | the list id to connect to. Get by calling lists() |
| email_address | the email address to subscribe |
| merge_vars | array of merges for the email (FNAME, LNAME, etc.) (see examples below for handling "blank" arrays). Note that a merge field can only hold up to 255 characters. Also, there are 2 "special" keys:| string | INTERESTS | Set Interest Groups by passing a field named "INTERESTS" that contains a comma delimited list of Interest Groups to add. Commas in Interest Group names should be escaped with a backslash. ie, "," => "\," | | array | GROUPINGS | Set Interest Groups by Grouping. Each element in this array should be an array containing the "groups" parameter (same restrictions as INTERESTS above) and either an "id" or "name" parameter to specify the Grouping - get from listInterestGroupings() | | string | OPTINIP | Set the Opt-in IP fields. Abusing this may cause your account to be suspended. |
|
| email_type | optional - email type preference for the email (html or text, defaults to html) |
| double_optin | optional - flag to control whether a double opt-in confirmation message is sent, defaults to true. Abusing this may cause your account to be suspended. |
| Returns | | boolean |
true on success, false on failure. When using MCAPI.class.php, the value can be tested and error messages pulled from the MCAPI object (see below) |
Examples (2)
download example code
[1] mcapi_listSubscribe.php
<?php
/**
This Example shows how to Subscribe a New Member to a List using the MCAPI.php
class and do some basic error checking.
**/
require_once 'inc/MCAPI.class.php';
require_once 'inc/config.inc.php'; //contains apikey
$api = new MCAPI($apikey);
/**
Note that if you are not passing merge_vars, you will still need to
pass a "blank" array. That should be either:
$merge_vars = array('');
- or -
$merge_vars = '';
Specifically, this will fail:
$merge_vars = array();
Or pass the proper data as below...
*/
$merge_vars = array('FNAME'=>'Test', 'LNAME'=>'Account', 'INTERESTS'=>'');
// By default this sends a confirmation email - you will not see new members
// until the link contained in it is clicked!
$retval = $api->listSubscribe( $listId, $my_email, $merge_vars );
if ($api->errorCode){
echo "\tCode=".$api->errorCode."\n"; echo "\tMsg=".$api->errorMessage."\n"; } else {
echo "Returned: ".$retval."\n"; }
[2] xml-rpc_listSubscribe.php
<?php
/**
This Example shows how to Subscribe a New Member to a List using XML-RPC.
Note that we are using the PEAR XML-RPC client and recommend others do as well.
**/
require_once 'XML/RPC2/Client.php';
require_once 'inc/config.inc.php';
try {
// Note that the same blank array() restriction for the merge_vars
// does not apply here as XML-RPC will handle it unless you use a null value
$merges = array('FNAME'=>'Test', 'LNAME'=>'Account', 'INTERESTS'=>'Dogs,Horses');
// By default this sends a confirmation email - you will not see new members
// until the link contained in it is clicked!
$result = $client->listSubscribe($apikey, $listId, $my_email, $merges, 'html', false);
echo "Returned: ".$result."\n"; } catch (XML_RPC2_FaultException $e){
echo $e->getFaultCode()." : ".$e->getFaultString()."\n"; }
?>