# I couldn't sleep last night and spent about half an hour planning an email subscription system for the blog daily feed. I drew a quick diagram and a list to include:
- register/unregister (add and remove address to/from list in database)
- send a welcome mail on registering
- auto-send each day
I made some notes about how I would achieve each element ready for when I woke up before finally dropping off.
# The daily email sign-up is now live on the /Subscribe page and seems to work okay in my (admittedly limited) testing.
Adding and removing your address is fine and the welcome mails go out correctly while redirecting you to a separate welcome page. Removing your address provides a prompt rather than redirecting to a separate page.
Each address in the list is added as a BCC to preserve subscribers privacy. Using wp_mail
they are each added as a separate header item so I have to loop through them individually. As an additional privacy measure I might look at encrypting the addresses in the database but that's for another day.
It's a bit rough and ready, and could probably be made a little (okay, a lot) prettier, but it seems to do the trick.
# As with the other automated processes (generating the RSS feeds) the daily email will be triggered by a cron job at 5 past midnight UK time.
# One thing I need to do is move the email generation code to its own template. It's currently on the test page where I try different things before making them live. I don't want to trigger multiple emails each time I forget and go to work on something else.
As with the custom RSS feeds, it's not something I anticipate anyone actually signing up for. I just thought it was worth a shot at creating as much for the intellectual exercise than the functionality.
Maybe, in future, I could hook into it to send additional mails or a newsletter and have it all self-hosted rather than relying on a third party service like Mail Chimp.
Comments
# The mail generation stuff has now been moved to its own template and the cron job updated so there shouldn't be any accidents.
I was thinking more last night about how I would implement a self-hosted newsletter to tie in with the daily email list subscription list. To this end I've created a custom post type to hold it. It will work by using a combination of my custom posting form and the mail generation code. I create the newsletter as though it was a normal post, hit submit on the form and it then takes the content, transposes it for the mail and automatically sends it to the list. It's actually a lot simpler than I imagined and I've already done all the hard work. I just need to build the form (which will be on a separate page template) and repurpose the mail generation code. If this became a thing I would probably need to stop the daily post emails going to the same list, as this would be overkill, or provide opt in choices.