How to use OptInSource "ACTION_BY_CONTACT" to send Welcome Email?


How to use OptInSource "ACTION_BY_CONTACT" to send Welcome Email?

Can someone tell me what I need to do in the below code to set the OptInSource to "ACTION_BY_CONTACT" so that the Welcome Email will get sent when a new user gets added to the mailing list?  I have the welcome email set up already, but from reading other posts, it sounds like the OptInSource setting needs to be added to make it work....




session_start ();
require_once 'ccontact/ConstantContact.php';
require_once 'ccontact/config.php';
$ConstantContact = new ConstantContact ( "oauth2", $apikey, $username, $accessToken );

$ContactLists = $ConstantContact->getLists();

// echo print_r($ContactLists);

        $Contact = new Contact();
        $Contact->emailAddress = $_POST['emailaddress'];
        $Contact->firstName = $_POST['firstname'];
        $Contact->lastName = $_POST['lastname'];
        $Contact->lists = $_POST['lists'];

        $Contact->OptInSource="ACTION_BY_CONTACT";     <---- bad syntax perhaps?

        $NewContact = $ConstantContact->addContact($Contact);
            echo "<script>alert('Congrats!  You are added to the mailing list.');</script>";





I modified my code to send new contacts the welcome email (and verified it does just that), and this is what it looks   like.  Your syntax to set the opt in source looked correct, but to add the contact to lists, lists should be stored as an array.



session_start ();
require_once 'ConstantContact.php';
require_once 'config.php';

$ConstantContact = new ConstantContact("oauth2", $apiKey, $username, $accessToken);  //OR
//$ConstantContact = new ConstantContact("basic", $apiKey, $username, $password);
//Supply your credentials--API Key and other details--in config.php

// Get potential contact lists
$lists = $ConstantContact->getLists();
// Lists are returned in multidimentional arrays 0 being the list, and 1 being the next50

// Email address here is used for testing purposes
$emailAddress = "";

// Search for our new Email address
$search = $ConstantContact->searchContactsByEmail($emailAddress);

// If the search didnt return a contact object
if($search == false)
	// Create a new Contact Object to store data into
	$contactObj = new Contact();
	// Adding multiple lists to this new Contact Object
	$contactObj->lists = array($lists['lists'][0]->id, $lists['lists'][1]->id);
	// Set the email address
	$contactObj->emailAddress = $emailAddress;
	//Set the opt in source
	$contactObj->optInSource = "ACTION_BY_CONTACT";
	// Create the Contact and DONE
	$Contact = $ConstantContact->addContact($contactObj);
	echo ("Contact added.");

} // Otherwise we update our existing contact
	// Gather data from our previous search and store it into a data type
	$contactObj = $ConstantContact->getContactDetails($search[0]);

	// We need to get the old list and add a new list to it as
	// this request requires a PUT and will remove the lists
	// as they are stored in an array
	 array_push($contactObj->lists, $lists['lists'][1]->id );
	 array_push($contactObj->lists, $lists['lists'][2]->id );
	// Update the contact and DONE
	$UpdateContact = $ConstantContact->updateContact($contactObj);
	echo ("Contact updated.");




Best Regards,

Shannon W.

API Support Specialist

Hi there, 


I've followed your instructions to get the welcome email to send using the API and it's still not working. The contacts are saving to my list ok, but they're not recieving the welcome email... Can you please help. Here is my code: 


try {
			// check to see if a contact with the email addess already exists in the account
			$response = $cc->getContactByEmail(ACCESS_TOKEN, $_POST['email']);
			// create a new contact if one does not exist
			if (empty($response->results)) {
				$action = "Creating Contact";
				$contact = new Contact();
				$contact->first_name = $fname;
				$contact->last_name = $lname;
				$contact->custom_fields[] = CustomField::create(
				//Set the opt in source
				$contact->optInSource = "ACTION_BY_CONTACT";
				$returnContact = $cc->addContact(ACCESS_TOKEN, $contact); 
		// update the existing contact if address already existed
			} else {            
				$action = "Updating Contact";
				$contact = $response->results[0];
				$contact->first_name = $fname;
				$contact->last_name = $lname;
				$contact->custom_fields[] = CustomField::create(
				$returnContact = $cc->updateContact(ACCESS_TOKEN, $contact);  
		// catch any exceptions thrown during the process and print the errors to screen
		} catch (CtctException $ex) {
			echo '<span class="label label-important">Error '.$action.'</span>';
			echo '<div class="container alert-error"><pre class="failure-pre">';
			echo '</pre></div>';

Hi Matthew, 


Are you using the v1 XML API, or the v2 JSON REST API?




Rich Marcucella
Sr. Technical Writer, Content Developer
Web Services Team
Developer Portal

View API documentation, code samples, get your API key.

Visit Page