Posting to WordPress MU by Email Part 2

THIS POST IS OBSOLETE

Warning: This post contains material of a technical nature, if you start reading and feel dizzy or nauseous then stop reading immediately and go and lie down.

This is a follow up to my previous post where I outlined a prototype of an extension for WPMU which allowed posting to multiple blogs from one centralised POP3 account which collected all posts from all users of all blogs. This is very different from the usual way of doing it where you either go out to get the emails from external email addresses or you have multiple local accounts.

Well the prototype has, like Frankenstein’s Monster, come alive and it’s now got configuration screens and lots of rich techy goodness to it.

I’m not going to go over the instructions on how to configure the MTA again, those are documented on my last post, and in the notes in the downloadable archive :

Post by Email-v1.zip – file removed

The Archive also contains instructions on where the TWO files need to be placed for the system to work.

What I’m going to cover here is the new features that I’ve added since the last post. Yes I have way too much time on my hands, but this is keeping my brain sharp so its all good at the end of the day.

These can be broken down into to distinct groups which are reflected in the two new options that will appear on the Settings menu:

Blog Level Options

These are options which are set by the blog administrator or blog owner:

Default Category for posts : This is a drop down of all categories and you select one to be the default category for posts without a category override

Optional Categories: This is a tick box list of all other categories in your blog, Toggle the ones you want users to be able to use in their category override.

Allow Posting By:  This is a tick box list of all the user who are members of your blog. Toggle the ones you want to be able to use the post by email. This will include YOUR address and by default no-one of them are checked (which basically deactivates the system for your specific blog).

User Level Options

These are options which are set on a user by user basis.

Personal Identifier : This is a optional text string which allows users to “secure” their posts. If this has been set then posts from them will be rejected outright. So set this to something good but memorable as you’ll have to provide it for each post.

Alternative Email: This allows you to nominate a secondary email address which the system will accept emails from. If you do not set this then posts will only be accepted from the email address registered to your account.

Additional Posting options

To support the “new” features there are now some additional posting options which you set at the top of your email.

wppbm-categories: A comma separated list of category names for this post. NOTE: This is names not numbers which makes it easier to remember (or are you so sad that you know the underlying IDs of your categories). You’ll find a list of the available categories on your user settings page. So assuming that the Administrator has enabled them you could do something like: wppbm-categories : computing,witterings which would post your post with those categories. If you do not specify this line then the default category set by the Administrator will be applied.

wppbm-pid: Your PID. If you’ve set your PID then you have to provide it here. If you’ve set it and don’t include it (or get it wrong) then your post is toast!

 

Posting to WordPress MU by email

Warning: This post contains material of a technical nature, if you start reading and feel dizzy or nauseous then stop reading immediately and go and lie down.

NOTE: The code and some of the details in this article have been superseded by a later post

There are a lot of clients for the PC/Mac/Linux which allow you to post to your WordPress blog without having to log into the back end, and some might question the need to be able to send posts into your blog via email but there are several situations where you can’t use a fat client or you don’t want to use the web back end for various reasons

WordPress MU (the multi-user/multi-blog version of WordPress) supports a basic post by email feature but it involves each user setting up their account to go out to an external email address and picking up the email. This seems rather “backwards” as it means your WPMU server is having to go out to find your new posts, and if you’ve got a lot of blogs then that’s potentially a lot of outgoing POP3 requests and it means that your have got the email account and passwords for your users, which some might see as not being a very good idea. WPMU suggest using a specific account to support posting by email and keeping the account “secret”.

Surely it makes sense for the posts to be delivered to the WPMU server and processed there. This has several advantages when you think about it.

So I’ve written a new version which uses a local virtual domain and a single pop3 box.

So the user emails theirwpmuloginid@some.domain.here which ends up in a pop3 mail box on a server somewhere (preferably the same server as WPMU). Every email sent to some.domain.here arrives into one single pop3 mail box rather than being in different ones. This “catch all” account means that you don’t have to do anything clever like creating email accounts for each user as they sign up.

My process then opens that pop3 account and reads each email in turn.

It will only post if the sender email ID matches the username it is associated with (from the user table). So you have to send email from the email address you signed up with, or the email address you’ve set it to in your account settings and you have to send it to the username you use to login to the site as. This is only very basic security and I will be adding more but for the first pass of the code I felt that this was good enough to prove the principle.

So apart from allowing me to post to my blog via email what else does this “extension” provide? Well users can specify some “options” in the email which affect how their post is handled:

There are currently 4 options that they can provide:

wppbm-tags: comma separated list of tags for this post
wppbm-status: publish [default] | draft | pending
wppbm-type: post [default] | page
wppbm-comments: open [default] | closed

All of these options are optional, you do not have to provide them and if you don’t provide them then the defaults indicated will apply. WPMU developers who have played with the wp_insert_post function will probably recognise them.

Posts accepted by email will be assigned to the default category for new posts.

It currently accepts basic html marked up (bold, italic, underline) messages (either manually coded or from the rich text editor in Google mail). Yahoo marks its html messages up differently so its not working if you try to use their Rich Text Editor, and “advanced” html mark up (like font sizes etc.) don’t work from Googlemail either. Again this will be addressed in a future release.

Things that need adding are:

  • A PID field (so that people can’t just “fake” the sender email address).
  • Ability to support posting to different blogs (rather than just primary).
  • Ability to support posting from more than one email account.

So if people want to try it, and leave feedback then this is how you implement it. If you can’t edit your Postfix configuration (or don’t know how to) then I’m sorry but I can’t help you. I don’t know if other MTAs support virtual domains but if they do and you get it working then we can add those instructions.

I’ve only tested this on Linux so you’ve been warned!!!

In these instructions I’m using posts.my-dummy.domain to represent the domain we’ll be using to send emails to.

Step 1 : DNS Records
Configure your DNS records to support posts.my-dummy.domain. You need to be able to send email to this domain.

Step 2 : Create a REAL email account on your server.
However you do it you need a local email account on the server that you’ll be directing email to. In this example I’m going to call this user wpmuposts

Step 3 : Postfix Configuration
Edit your postfix main.cf file and add the following two lines:

virtual_alias_domains = posts.my-dummy.domain
virtual_alias_maps=hash:/etc/postfix/virtual

NOTE: The virtual_alias_domain MUST NOT be listed in the mydestinations configuration parameter.

Create/edit the /etc/postfix/virtual file and add the following line :

@posts.my-dummy.domain wpmuposts

Save the file, and then you need to create a hash for it using the postmap command. I do this by going to the postfix directory (/etc/postfix on my server) and typing:

postmap virtual

You may then wish to check that everything works by waiting for your DNS to propogate and then going to an external email client and sending an email to anything-here@my-dummy.domain.

You should end up with an email sitting in the POP3 email box wpmuposts. If not then you need to review your settings. There is no point in going beyond this point unless you’ve got the “catch all” email working.

OK so email is now working. That really was the hard part. The rest is pretty easy, and if you’re doing this then going in and editing a php file isn’t the sort of thing that scares you right?

Step 4 : Getting and configuring the extension

Grab a copy of post_by_mail.php and save it to your computer.

Open the file in your favourite text editor and look for the following :

// USER NEEDS TO SET THESE.
// Postfix will need configuring to support virtual domains with a wildcard to deliver to the account given below.
define ("POP3HOST","your host here");
define ("POP3PORT","110");
define ("POP3USER","special pop3 user account here");
define ("POP3PASS","special pop3 user password here");
///

and edit them to match the POP3 settings you need to access your special POP3 email account: So for example:

// USER NEEDS TO SET THESE.
// Postfix will need configuring to support virtual domains with a wildcard to deliver to the account given below.
define ("POP3HOST","localhost");
define ("POP3PORT","110");
define ("POP3USER","wpmuposts");
define ("POP3PASS","notarealpassword");
///

Save the file and then upload it to the ROOT of your WPMU installation. DON’T FORGET TO REMOVE the .TXT extension!

That’s basically it, the extension is there. If you want to give it a different name, for security reasons, then feel free to.

To test it, send an email (and I’d suggest setting the wppbm-status to draft) to yourwpmulogon@posts.my-dummy.domain. Then once its arrived call:

http://my-wp.domain.here/posts_by_mail.php

You should see some diagnostic message on the screen and hopefully, fingers crossed, you’ll end up with a new post in your blog.

So all you then need to do is set up a cron job. At the moment you can’t use the PHP CLI to run this code but you can set up a cron job to to a wget on the URL. As this isn’t a silent process I don’t recommend putting a call to it into your themes, its much better to keep it controlled by cron (and that way you can route the output to a log file to keep an eye on it).

Debugging
If you think that there are problems and you want to keep checking the same email as you add debug code to the extension then you’ll want to stop it deleting emails / posting messages.

To stop it posting into your blog look for the following line (line 241):

$post_ID = wp_insert_post($post_data);

and put a # in front of it.

To stop it deleting messages on completion look for the following code (line 256 – 262):

if(!$pop3->delete($i)) {
echo ' <p>' . sprintf(__('Oops: %s'), esc_html($pop3->ERROR)) . ' </p>';
$pop3->reset();
exit;
} else {
echo ' <p>' . sprintf(__('Mission complete. Message <strong>%s</strong> deleted.'), $i) . ' </p >';
}

and comment it out.

So there you go. Please remember that is a work in progress and support for complex HTML isn’t there and you install it at your own risk. If you’ve got any suggestions on improvements, or you’ve found a bug then please let me know.

Pub Night

Pub Night is a website that I set up for a group of friends who go out drinking once a week in Cheltenham. We’ve been doing it for years and a few years ago now Nick and I came up with the idea of building a website for it. I went for a Wiki based site but because we wanted “standard” information for each pub I built a rather large extension for it which basically allows us to use Data pages to hold the information on the pub and then for each pub we have a simple page which contains some tags which allow us to display the information in a controlled format.

Pub Night is primarily based in Cheltenham but has details on some pubs outside of the town, primarily in GloucesterLondonUpton upon Severn and Market Drayton.

There is also a sub group of Pub Night called Out Of Town Pub Night Group (or OOTPNG for short) which once a month goes to a good pub within a reasonable driving distance of town. The venues and drivers and approximate dates had been decided and I went to load them into the system so I could put up a page with the 2009 locations. That is where the problems began. The code that drives pub night talks directly to the database behind the wiki which means that information on pubs normally comes out in the order the pubs were added into the database. To support the new page I added two new fields to the data set – nextvisit and driver. But I wanted to sort the information on the nextdate. So I’ve had to go in a recode how the “where” part of the extension works so that rather than just trawling through the pubs in turn and if they match the criteria displaying the information I now go through the pubs once and grab a “sort” field from the data (if specified) and then I sort the list of pubs on that and then process each one in turn to get the information. Sounds easy doesn’t it?

Well it is when you are dealing with text fields because you can simply build the array with the Digraph and the “sort” field and then use the php functions asort and arsort. Date fields are another matter entirely. Pub Night stores its dates in dd/mm/yyyy format which do not sort. PHP has a strtotime function which converts “human” date formats into unix timestamps. Those we can sort easily so problem solved. Afraid not because strtotime doesn’t like dd/mm/yyyy. So I had to us the php mktime function to parse the PN date format into something I could then pass into strtotime to get the timestamp and now its all working.

Now all I have to do is fix the problem with null dates which upset it (silently, they just spew errors into the error log!)

Can there be too many Online Fora?

Over the weekend, when I was moving the boat, I was sitting on the back deck thinking about a lot of things. Camsigh has gone quiet over the past couple of years, and other boards that I’m signed up to such as Ghostly Stay , PhantomFest and Café Phoenixx all seem to have similar cycles – a flurry of postings and then it all goes quiet for a bit, then some more come along and then it goes quiet again.

So why should this be? Is it part of the normal cycle that forums go through or is there something else happening?

I think its the latter – When I set Camsigh up back in January 2004 the only way to get a forum was to either pay for one or run your own server (either from a hosting company or if you were really sad – yourself) and run a forum on there.

Then along came proboards, myfreeforums etc. etc. and the “free” forums phenomena took off. Running costs are recovered through the placing of adverts (over which you have no control) but in return for that you get a good uptime, a responsive server and it would seem some pretty good anti-spam measures.

So now there is a forum for everything – a forum for bunion sufferers, a forum for people who collect pictures of buses, a forum for every conspiracy you could ever imagine (and then a few more). The list goes on and on, it is endless. The Internet is full of forums.

But something is going on with these free forums. Running this site and doing admin on a few others there is a rising level of spam advertising forums (hosted on various free forum sites). These forums aren’t real forums – they contain nothing but a URL redirect to another site which will then try to sell you dodgy drugs, iffy insurance, or manky mortgages. These sites bring no money into the coffers of the companies hosting the forums and give the scam merchants yet another marketing vector. Sooner or later the free forum hosters will have to clamp down on the processes needed to create a forum and put more stringent controls in to stop their services from being abused.

Of course the spammers are well ahead of the game – they are now spamming free Blog services which they using in a similar way to the way they abuse forums.

So who knows what will happen – maybe a couple of free forum providers will go down the tubes, maybe users of the forums will get sick and fed up of the continual stream of spam being forced down their throats.

The Web should be a great place for people to learn, to communicate, to share. But the problem is that the spammers just don’t give a shit : they don’t care who they annoy, who they abuse, who they peddle their shit to. They have no morals at all – I’m sure they spam cancer support forums and such like with their odious filth.

Of course the spammers are just doing what they have been paid to do. At the end of the day its people like Visa and MasterCard who could put a stop to a lot of it. If they refused to do business with companies who spam or who deal in products like “Generic” counterfeit drugs then the money flow would stop. Mind you it would stop if people out there actually stopped believing the crap they are being spammed with.

Playing around with PHP and PHPBB

I’d been looking round for quite a bit for a nice acronym explainer for PHPBB… I found quite a few but none of them did what I wanted.

So I took a couple of existing mods and hacked them round a bit and have produced just what I wanted:

1) Centrally maintained definitions
2) Proper formatting of definitions (i,e, full phpbb code)
3) Nice sized pop up when you move over the word/phrase
4) Word/Phrase is also a link to a post containing the definition.

OK maybe other people will think its rubbish.. but I like it 😉

Google Ads

Well I’m quite pleased with myself. The mod to support google ads in the phpbb forums only handles text ads. I wanted to put in some of the referrer ads so I wrote a little bit of php code which sets up an array with the different ads in and then picks one at Random.

I also dropped the code to put random Google “Pack” adverts into this blog mod which works quite well 😉

Phew!

Well the e-commerce site is up and running at www.ladykat.co.uk

I’ve been working on a rather large silly project to “recover” some old phpbb forum posts from html file dumps. It was hard work but fun.. the fruits of that effort can be found at www.tty.homelinux.org/board

I’m also working on a two stack parser in PHP – so far I’ve got all the logic worked out and I’ve written the extression parser which breaks the inputted line into chunks. Not sure when its going to go live but when it does pubnight ( www.pubnight.org.uk ) is going to get a lot more dynamic content.

Gone away!!

Well after following the instructions here http://www.gotroot.com/tiki-view_blog_post.php?blogId=2&postId=34

I’ve managed to get it so that spammers on my boards get a 403 message.

The only thing I found was that the directives had to go into the apache2 config file rather than the .htaccess file which complained “order not allowed here”. I also adjusted it so that only POST is blocked..

I’m going to run with it for a few days then I’m going to add a couple more RBLs to the list and then I can hopefully put the trackback link back in.

And for those who dont want to down load the whole of the apache source files just to compile this one little bit here is the recompiled mod_access for Apache 2.0.48

http://www.tty.homelinux.org/board/mod_access.zip

Things

Its lunch time – I’m sitting here watching the horses canter round the field and I can see a small group of tourists walking along the top of the Malvern Hills.

Its so much quieter here than in the Birmingham Business Park, the scenery is much better too, and there are several good pubs within a short commute.

So apart from work what have I been doing? Not doing the kitchen renovations if you must know – the disaster that is the window (Thanks a bunch Cheltenham Council for using jerry builders to build the Priors Farm estate) really is a big disincentive. I’ve also been doing some coding on an extension to CanalPlan which should implement social networking, blogging and “tracking”. OK its not going to do all of that to start off with but…

Of course this means me learning how to handle sessions in PHP and interfacing PHP with a Database. Still its all good fun (!).