SugarCRM is a nice open source customer relationship management tool, with a fancy newsletter campaign system build in from scratch. You would probably think that it is fairly easy to make it possible for your potential customers to subscribe to your newsletter through (for example) your website. Well, it's not. SugarCRM offers a way to create web-to-lead forms out of the box. Web-to-lead forms unfortunately are limited to putting information about a potential lead only to the lead section of the CRM, not registering a potential lead to a newsletter campaign. You have to do some coding to register a new ...
SugarCRM is a nice open source customer relationship management tool, with a fancy newsletter campaign system build in from scratch. You would probably think that it is fairly easy to make it possible for your potential customers to subscribe to your newsletter through (for example) your website. Well, it’s not.
SugarCRM offers a way to create web-to-lead forms out of the box. Web-to-lead forms unfortunately are limited to putting information about a potential lead only to the lead section of the CRM, not registering a potential lead to a newsletter campaign. You have to do some coding to register a new web-lead to a newsletter campaign.
First we create a web-to-lead form in SugarCRM through Campaign -> Create Lead Form (action button on the top)
Drag the fields you want your potential subscribers to fill in (at least Last Name and E-mail address), link the form to your newsletter campaign and generate the form.
SugarCRM will generate a nice form with all the field requested. You can paste the HTML code directly to the place at your website.
For now leads are being registered in SugarCRM leads section, but not subscribed to your newsletter campaign.For this we need to dive into the code. Grab your best PHP editor and go to:
<SugarCRM directory>/modules/Campaigns/WebToLeadCapture.php
Somewhere around line 108 you will find the following:
$lead->load_relationship('campaigns');
$lead->campaigns->add($camplog->id);
In these lines the potential lead is being registered to the campaign. After that we would like to register the lead to the specific prospect list of the newsletter. I coded these fancy lines of code. Paste them just beneath:
// Subscribe to newsletter
if (isset($_POST['prospect_list_id']) &amp;amp;amp;&amp;amp;amp; !empty($_POST['prospect_list_id'])) {
//link prospect list and lead
$lead->load_relationship('prospect_lists');
$lead->prospect_lists->add($_POST['prospect_list_id']);
}</code>
Save and exit. Hmm that’s strange, you check if $_POST['prospect_list_id'] exists, don’t we have to put that in our form also? You’re right! Start up your HTML editor again and search for the Web-to-lead form you just generated.
Search for the hidden field campaign_id. It should look something like this:
<input type="hidden" id="campaign_id" name="campaign_id" value="8c2c2a77-f2a2-d56c-5bde-4caef17195b1">
After this line of code you have to add an extra one:
<input type="hidden" id="prospect_list_id" name="prospect_list_id" value="<somenumberwithdashes>">
To get the prospect_list_id you have to do some browsing in SugarCRM. Go to Campaigns -> My newsletter campaign
Then click the Target list you want to add your subscribers to (most probably just the subscribers list).
In your browser URL field you will then find something like:
/index.php?module=ProspectLists&blablablabla&record=<somenumberwithdashes>&parent_module=
Copy the <somenumberwithdashes> and replace it with the prospect_list_id of the hidden field. Save & Voila, you have a working web-to-lead form with subscription of the lead to your newsletter.
If you want to make your web form more fancy and handle the validation & response message by yourself, you can do the actual subscription of the form by PHP. You can use this piece of code:
function sugarCRM_newsletter_subscribe($firstName, $lastName, $email) {
$url = 'http://<yourCRMurl>/index.php?entryPoint=WebToLeadCapture';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, array('first_name'=>$firstName,
'last_name'=>$lastName,
'webtolead_email1'=>$email,
'campaign_id'=>'<CampaignID>',
'assigned_user_id'=>'<AssignedUserID>',
'prospect_list_id'=>'<ProspectListID>',
'req_id'=>'last_name'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
if ($response) {
return true;
} else {
return false;
}
curl_close($ch);
}
Just replace the <>’s and call the function!
Hello all,
My new blog is on the web! Soon I will start to blog about usability and software things.
See you later!