How to send an email with Mandrill using a SugarOutfitters Webhook for a SugarCRM Add-on Sale
<?php | |
// load up mandrill | |
require_once('vendor/mandrill/src/Mandrill.php'); | |
define(MANDRILL_APIKEY,'MANDRILL_API_KEY_GOES_HERE'); | |
// get the webhook response | |
// decode the json data into a php object | |
$body = @file_get_contents('php://input'); | |
$response = json_decode($body); | |
// the webhook property tells us exactly which webhook event was fired | |
// so let's create a case for a few webhooks | |
switch ($response->webhook) | |
{ | |
case 'sale_new': | |
// Someone purchased your add-on | |
SugarOutfittersHelper::sale_new($response); | |
break; | |
} | |
class SugarOutfittersHelper | |
{ | |
public static function sale_new($response) | |
{ | |
$addon = $response->data->addon; // the addon that was purchased | |
$lineitem = $response->data->lineitem; // we give you the lineitem because you may have multiple purchase plans for an add-on | |
$member = $response->data->member; // your new customer! | |
$licensekey = $response->data->licensekey; // if you're using SugarOutfitters lincense keys, the details of the license key are listed here | |
// let's send them an email | |
try | |
{ | |
$mandrill = new Mandrill(MANDRILL_APIKEY); | |
$message = array( | |
'text' => "Hi there, thanks for giving SugarChimp a try.\n\nDo you have any questions about the integration?\n\nCheers!\nChad", | |
'subject' => 'MailChimp & Sugar Integration', | |
'from_email' => 'chad@sugarchimp.com', | |
'from_name' => 'Chad Hutchins', | |
'to' => array( | |
array( | |
'email' => $member->email, | |
'type' => 'to' | |
) | |
), | |
'headers' => array('Reply-To' => 'chad@sugarchimp.com'), | |
'tags' => array('onboarding-welcome'), | |
); | |
$mandrill->messages->send($message); | |
echo "Email sent.<br>"; | |
} | |
catch(Mandrill_Error $e) | |
{ | |
// Mandrill errors are thrown as exceptions | |
echo 'A mandrill error occurred: ' . get_class($e) . ' - ' . $e->getMessage() . "<br>"; | |
// notify or log this | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment