1

Email Automation – Laravel+Sendy+JVzoo In under an hour

TLDR; Skip to the lower source block for the apache and htacess configs for laravel in a subdirectory using apache aliases.

I mentioned a few days ago that I’m moving over to using Sendy as my mailing list system. One of the things I needed to get sorted pretty sharpish was handling the customer email automation process for my Book website ‘Beyond Bedroom Guitar”.

Although the book is available in printed form from Amazon and that sales process is out of my control, the ebook version can be brought from the site. In the past when someone subscribed, I’d get a notification from the sales system ‘JVZoo’ and they had integration to add the new customer to a special email list. The idea being that they would then get an autoresponder sequence helping them get more from the book.

Note to self: Improve the customer email sequence.

However JVZoo don’t have a Sendy integration out of the box. However they do have their own notification system called JVZIPN.

Details here http://support.jvzoo.com/knowledgebase/article/View/17/0/jvzipn

So essentially they send messages via a POST request to an endpoint url that you define for the product with the order status (sale, refund, chargeback etc.)

The email automation actions I need to take are:

  • If the customer has successfully purchased the book then add them to my customer list
  • If the customer has requested a refund (which isn’t often but it does happen) then remove them from the link

I also need to validate the  ‘posted’ message content using some jigging about with a secret key that I define and combining the posted values together to compare them with a verfication string. Sounds complicated but the page above gives some boilerplate code for the checking the verfication code.

Do I use a framework with a lot of the heavy lifting done for me?

So first decision, do I use a simple one file bit of code and hand craft everything I need (checking for post variables, doing the verfication, grabbing the values I need (email and name) and then setting up curl to the sendy api. OR Do I use a framework with a lot of the heavy lifting done for me.

As an experiment I decided to try out a framework – but which one? PHP of course because my Sendy server is configured to handle that. But such a choice. Zend1 or 2, Codeigniter, CakePHP, Symphony… even possibly Yii. However my current ‘shiny thing’ framework is Laravel – and not without good reason. (Typically I’m b*lls deep in Zend1 every day for clients but that’s the day job).

So what do I get from laravel out of the box for this mini side project?

  • Rest API
  • Routing
  • CSRF (but disabled as this isn’t a form post but an api)
  • Fast (and I mean very very very fast) creation. Artisan anyone.
  • Easy to maintain and extend.
  • Probably a half dozen more reasons

Getting started was a cinch.

 

Now down to development.

I’ll not bore you with a complete walkthrough of the code here. Here’s the shortened version.

  • Configure a  quick POST route in app/Http/routes.php to the endpoint I’ve defined.
  • Add the route to JVZoo
  • Switch off the laravel middleware VerifyCRSFToken for the route (this took a couple of google searches to find, and is the only thing that pushed me over 30 minutes of dev time from start to finish – opinionated frameworks are fine as long as its clear what its opinions are and how you can override them. It isn’t clear Laravel devs!)
  • Create the controller using “ php artisan make:controller JvZController “
  • install the ‘hocza/sendy’  library with composer and tweak the providers and aliases in the app.config. Brilliant – works a charm. This is the heavy lifting bit of the email automation. One use statement, one command to subscrine, one to unsubscribe and one config file (which could be overridden).
  • Change the .env file to production values
  • Generate new secret keys and put in the right places (again opinionated!)

A bit of testing (without going all TDD with phpunit – this is a very simple use case and I can test it manually for the moment)

Then upload to my sendy host into a new directory.

Sweeeeet.

That’s where the fun began.

I wanted to put the endpoint in a subdirectory but not pollute the sendy codebase, so the code would live in another directory and I’d configure apache to use an Alias to point to the laravel public directory.

It didn’t work! Not with the first few methods that I found with google searches either. It’s a faff because of the many different apache configs that can exist so one size does not fit all.

The solution’s didn’t include proper apache configs and .htaccess files working in tandem. So I’ve pasted them in below for anyone else who finds this post and has the same problem

  1. httpd.conf (or in my case in a file under /etc/apache2/sites-enabled)
The .htaccess file in the laravel root directory
A second (yes second) .htaccess file in the public directory
 

I could possibly have made things a bit simpler by using AliasMatch  or a bit more mucking about with the actual config. However its a live system and I didn’t want to ‘fettle ‘er  about for long. but I was trying to keep the dev time to under an hour and had already spent 15 mins fiddling with this step.

So there you go – a little side project that took under an hour even with some bumps in the road.

Further work could include additional email lists. Handling multiple products and lists. With a single php file this would have been a pain but with this setup, adding a database, admin crud controller etc would take about another hour.

Laravel. Nice.

Spencer Westwood
 

Author, musician, developer and generally a bit of a self confessed computer geek.