LessFussDesign blog

PHP mail() & Fasthosts

After three days, 14 Wordpress contact form plugins and a lengthy support email dialogue, here’s a post on what you can do – other than repeatedly punching a wall  – if you want to use Wordpress with a simple contact form on a Fasthosts server (or any other provider with strict mail server settings).

Fasthosts mail server security

Fasthosts [external link] route all outbound email from web scripts through their SMTP servers, ‘to prevent spam from being sent through our web servers’. If you use a web script that will attempt to fire an email, either the from or to address must be a Fasthosts email account:

Email must be sent to, or from, an email address hosted by Fasthosts. An email address hosted by Fasthosts is not the same as a domain name hosted by Fasthosts. If your domain’s MX record points to another email provider, it will not count as being hosted by Fasthosts.
Fasthosts Knowledgebase

Now I have no problem with this as such – anything that can be done (over and above the validation within the web script itself) to stop my web space and scripts being used to send spam is a good thing.

The problem is that you won’t always want emails from your web scripts to be sent from one single (Fasthosts) address. You may for example have a script that sends the email from the address supplied by the user filling it in, which is perfectly fine. And anyway, the majority of the Wordpress plugins I tried would still not send email, even when I supplied Fasthosts address for both from and to email fields in the configuration settings.

PHP mail() parameters

After the fifth Wordpress contact form plugin failed to send an email, I contacted Fasthosts support, who – after a lengthy email dialogue – helpfully informed me that ‘there’s nothing wrong with the mail server’. So I checked Fasthosts’ support website, the Knowledgebase, to discover that a fifth parameter is required when using the PHP mail() function.

A simple PHP mail function looks like this:

mail($email_to, $email_subject, $email_message, $headers);

On a Fasthosts server, you need to add a fifth parameter:

mail($email_to, $email_subject, $email_message, $headers, '-f'.$email_from);

According to PHP.net [external link], this parameter essentially forces the mail server to trust the email address supplied and continue sending the email:

The user that the webserver runs as should be added as a trusted user to the sendmail configuration to prevent a ‘X-Warning’ header from being added to the message when the envelope sender (-f) is set using this method.
php.net

Fine and dandy. You can use this parameter to essentially ‘force’ the mail server to accept the address you’ve supplied, Fasthosts email account or not.

Fasthosts and Wordpress contact form plugins

Knowing the fix is only a quarter of the battle – you still have to add it to your script. If it’s your own script then no problem, but if you’re using a plugin then it’s a different matter entirely.

None of the Wordpress plugins I tried came with any option for adding this extra parameter in the configuration settings (suggesting it’s not a huge requirement from users out there), which meant – gulp – diving into the PHP files and finding the mail() function myself.

This is never a great idea for a number of reasons (not least if the plugin is upgraded, you’ll have to make all your modifications again) – especially if, like me, PHP is a teeny bit out of your comfort zone. But it may prove the only way to resolve the problem.

It’s also not always immediately obvious where the mail() function is – some plugins have multiple PHP files, and finding a simple mail() function call that you can quickly edit may not be a simpe thing to do.

What I would say though is don’t use a plugin simply because you’ve found editing the mail() function a doddle. On my journey through the Wordpress plugins directory, I’ve seen some pretty depressing things – plugins that have little or no validation or basic spam prevention, forms that are woefully inaccessible and even some that generate multiple nested tables to control the layout. It’s not worth using trash just because you got it to send a mail…

Summary

If you’re on a Fasthosts server – or with any host that has strict security on it’s outgoing mail – and you’re having trouble getting your scripts to send email, then the solution above may fix it.

If you’ve written your own script then adding this fix will be painless. If you’re beholden to a plugin then it’s less straghtforward, but still achieveable. It just so happens that one of the few plugins I found that sent email first time without any problem, is also by far the most accessible – Mike Cherim’s Wordpress-ready Contact Form plugin [external link] – and I would recommend this to anyone on the Wordpress platform looking for a contact form, email server problems or not.

There is of course a third option, and that’s to switch hosting providers. What with this and other things Fasthosts have done to put my back up lately, it’s certainly something I’m considering.

Written by: Andy Bryant

Published on: 21 Oct 2009

Tags: , , , ,

Responses to this article

11 responses to ‘PHP mail() & Fasthosts’

Follow responses to this article through the Comments RSS 2.0 feed.

  1. Or, you know, just drop your friendly PHP techie a line, and get it done in five mins.

    (Once you knew what needed to go where, obv.)

  2. Cheers, I may just do that – I really like the Contact Form 7 plugin, which is one of the ones I couldn’t find the mail() function on, so I might send that through to you…

  3. Excellent post, keep at it!

  4. Dude, you are a life saver! I’ve been trawling the net for days trying to find out why i could get my form to send to a fasthosts account. I’m have hardly any knowledge of php and when I looks at the fasthosts ‘knowledge base’, it all went straight over my head.

    So thank you, for putting it in plain english!

  5. @Aaron Glad it was helpful Aaron. I always dread using the Fasthosts Knowledgebase, as it’s never particularly intuitive and doesn’t always have the answer you’re after anyway. But this time around I was lucky – shame the Support guys couldn’t have told me up front when i contacted them, would have saved me some time and head scratching…

  6. Well isn’t that the wierdest thing i’m also on fasthosts and having the same problems and they’re support have yet again been less than helpful this morning. However, I can get contact form 7 working straight away but not the one you suggest – that’s the one I really want to use.

    I’ve also had problems with fastshosts and wordpress custom 404 error pages i.e. they just turn up a page of random code – again no useful help from FH themselves you’ll no doubt be surprised to know!

  7. @Lyz I’ve had issues with 404 pages too – still have them. I found others on the wordpress forum with similar issues, and from what I can gather it’s to do with the htaccess file and Apache, though exactly what I’m not sure. It’s another of those things that, with Fasthosts, you end up learning to live with. Which is all wrong, really.

  8. OK I’ve fixed the contact form problem and here’s what you need to do:

    1. get the plug-in WP-MAIL-SMTP http://www.callum-macdonald.com/code/wp-mail-smtp/

    2. change the plug-in settings so ‘from email’ is an email on your blog’s domain

    3. check the radio button ‘Send all WordPress emails via SMTP’

    4. type smtp.fasthosts.co.uk into the smtp host field

    It’s worked for me when nothing else has so hope it prevents someone else tearing their hair out :D

  9. Hi,

    Thanks for posting this info. I have just started web development.

    I have just completed a site for a client and am having such problems with a form.

    I am determoined to make it work without having to buy in a formbuilder (maybe it is less hassle though?).

    Can you help me with my form if I send you the page?

    Sounds like u r an expert.

    Ant

  10. @anthony I’ve been called many things, but rarely an expert! Dude, you flatter me, so yes go ahead and send it – email address is on the contact page.

    Only send it if you’re having grief on a Fasthosts server, if Fasthosts isn’t your host and the problem’s to do with something else with PHP then I’ll tell you now, I’m not your man…

    Best of luck.

  11. Has anyone ever added this into the MODx emailer script and got it working? Would love to know how too!

Leave a Reply

Got something to say? Whether you agree or disagree, if you can contribute on the subject then please go ahead - just keep it clean.

Your details
Your comments