Sending automated emails which don’t get blocked by ISPs

Published:
September 5, 2007 @ 12:38 am
Category:
Server Administration, PHP
Author:
Chris Wheeler

I’ve recently come across a problem where automated emails are getting blocked by a number of ISPs. I’ve tracked this down to the sender verification methods some ISPs use - If an email comes from an address which isn’t an active mailbox on the server, it is sometimes dropped without any notice to the sender or recipient.

A fair few web sites send automated emails from addresses such as no-reply@domain.com do-not-reply@domain.com forum@domain.com etc - if these accounts are not setup the mails will be discarded by some servers.

You should always use a real email account to send emails - even if it silently discards all incoming mail.

Another problem is that if PHP’s mail() function is being used, and php is running as the userĀ ’nobody’, emails by default will be sent from nobody@hostname.isp.com - which in most cases does not exist.

The way around this is to specify the senders email address through the mail function. e.g.

mail($to, $subject, $body, $headers, '-f sales@domain.com')

Note: Even if you have set the ‘From: ‘ field in the email headers, the email envelope address may still be set to a different account, using the ‘-f email@address’ parameter will fix this, and your emails should reach your customers!

Upgrading from phpBB2 to phpBB3 - with attachments!

Published:
July 16, 2007 @ 1:37 am
Category:
Web Development
Author:
Chris Wheeler

I’ve just upgraded a forum for a client to the latest release candidate for phpBB3, and was pleasantly surprised by how easy it was to do, even with a few modifications to the phpBB2 powered forums.

The process is well documented on the phpBB website - but I’ll give a quick run down here.

  1. Install a clean copy of the latest version of phpBB3 (RC3 at the time of writing) to a new directory on the server - I used /phpbb3/ for this, as I planned to rename it back to /forums/ after the upgrade was complete.
  2. Verify everything is working with the new installation.
  3. Use the convert script to import all posts and settings from your existing phpBB2 installation (this does not make any changes to the phpBB2 installation or database, so if things go pear shaped, you can try again). The convert script is accessed via the new ACP (Administration Control Panel) in phpBB3.
  4. Check that everything is working, and remove the phpBB2 installation.

The most impressive part of the process is that all attachments which were added to the phpBB2 powered forums via attach mod were migrated to the new built in attachments system in phpBB3!

Changing font size with JavaScript

Published:
May 25, 2007 @ 3:14 am
Category:
JavaScript
Author:
Chris Wheeler

As you’ve probably noticed, this site has three increasingly large ‘A’ characters in the top right of the design. These enable you to quickly increase the size of the font on the page, to make articles and long sections of text easier to read.

I’ll explain briefly how they work, and how to set them up.

Setting up your pages

To be able to scale font sizes you must make sure all of your CSS ‘font-size’, ‘line-height’ and other font-related measurements are specified in ‘em’ units, and NOT in ‘px’ units.

Internet Explorer will not allow ‘px’ (pixel) based font sizes scaled, which actually makes sense - as pixel measurements are by definition an absolute measurement, set by the users screen resolution. ‘em’ measurements on the other hand, are relative - and can therefore be scaled.

The other feature of ‘em’ measurements which we will make use of, is that they are relative to the size of their parent element. This means that we only have to change the size of the font on the <body> element, and all child elements font sizes will change.

The HTML/JavaScript

<span class="fontsizes">
  <span class="fontsmall">
    <a href="#" onclick="body.style.fontSize='8px'">A</a>
  </span>
  <span class="fontmedium">
    <a href="#" onclick="body.style.fontSize='10px'">A</a>
  </span>
  <span class="fontlarge">
    <a href="#" onclick="body.style.fontSize='14px'">A</a>
  </span>
</span>

The HTML/JavaScript above shows the three ‘A’ characters, and triggers a slightly different onclick event for each, setting the size of the font on the body element to a different size for each. Most browsers use a default size of ‘10px’ - which is the ‘medium’ font in this case. I’ve chosen 8px for the small font, and 14px for the large font, but you can use whatever suits your needs.

The CSS

.fontsizes {
  color: #ffffff;
  line-height: 34px;
  font-size: 10px;
}    

.fontsmall {
  font-size: 12px;
}    

.fontmedium {
  font-size: 20px;
}    

.fontlarge {
  font-size: 28px;
}

The ‘line-height’ is set to 34px, which is equal to that of the large ‘A’.

You may be thinking - ‘Why are the font sizes specified in ‘px’ here?’ - well, we don’t want these characters to change size when the user changes the font size. An alternative would be to use images instead of text characters, to ensure that no browsers scale these characters.

If you want to, you could use JavaScript to write the HTML code to the page. That way, the user will only see the three ‘A’ links if they have JavaScript enabled.

Installing Smarty on Linux

Published:
May 17, 2007 @ 12:56 pm
Category:
Server Administration
Author:
Chris Wheeler

While its possible to use Smarty by simply uploading it to your user account and then including it in your project, its much cleaner to install it onto your server, and then simply include it in your scripts with

require_once('Smarty.class.php');

Installing Smarty is a simple process, all you have to do is download it, extract it to your servers php libs directory, then make sure its inlcuded in you php_include path. Once you have done this, you will only need to keep one version up to date, as well as saving disk space if you have multiple sites powered by Smarty.

Step 1: Download & Extract Smarty

Smarty 2.6.18 is the latest version at the time of writing, however please check the official Smarty website for the latest version, and ammend the commands below if applicable.

$ cd /usr/local/lib/php/
$ wget "http://smarty.php.net/do_download.php?download_file=Smarty-2.6.18.tar.gz"
$ gtar -zxvf Smarty-2.6.18.tar.gz
$ mkdir Smarty
$ cp -r Smarty-2.6.18/libs/* Smarty
$ rm Smarty-2.6.18.tar.gz

Step 2: Add to php’s include path

Open up your php.ini and edit the include path to include “/usr/local/lib/php/Smarty”. The location of your php.ini file can be found by creating a script with a call to phpinfio() in it, you cal also use this to verify the changes to the include_path once you have made them. Mine include_path looks like the example below.

; UNIX: "/path1:/path2"
include_path = ".:/php/includes:/usr/local/lib/php/Smarty"

While this guide is intended to be generic, your server settings and directory structure may vary, so please be careful!

Integrating Wordpress into Smarty

Published:
May 16, 2007 @ 11:49 pm
Category:
PHP
Author:
Chris Wheeler

While searching for a way to integrate WordPress into an existing web site, all the tutorials I came across were aimed at users who wanted to make WordPress look like their existing site. There were also some tutorials on how to take parts of the WordPress engine and add them to existing PHP pages, however; I couldn’t find anything on integrating WordPress with Smarty, so I thought I’d have a go - and it turned out a lot easier than I thought.

Step 1: Install WordPress

Download WordPress and install to a directory on your web site - this will be the final url which visitors will see. I chose /blog/

Step 2: Create custom PHP includes

Now you need to create two PHP files, which provide ‘The Loop’ (the code which provides the content for all blog pages) and the sidebar links. Credit for this goes to the author of this article.

The two files I created were:

/blog/custom/the-loop.inc.php

View the-loop.inc.php

This is basically just /blog/wp-content/themes/classic/index.php with get_header() and get_footer() removed.

/blog/custom/sidebar.inc.php

View sidebar.inc.php

This is basically just /blog/wp-content/themes/classic/sidebar.php
You could most probably just include the sidebar.php file from the classic theme, but I wanted to remove a few sections

Step 3: Include the WordPress header

The WordPress header file needs to be included on all pages which have blog content on them, I simply placed it into my ‘header.php’ file - as this is included on all pages

require_once './blog/wp-blog-header.php';

Step 4: Create the Smarty PHP files

To show the main WordPress content inside of your sites design, you need to create a PHP file and template. My file looks something like the following:

blog.php

View blog.php

The $page variable is simply an instance of the Smarty class, which is instantiated in the header.php file, the page title is set and then the blog.tpl file is parsed.
The ‘if’ statement checks to make sure the page isn’t a Rss, Trackback or Robot - as in these cases we do not want to send any html output, as output has already been created by the WordPress header.

Step 5: Create the Smarty TPL templates

To pull in the main WordPress content area into your smarty templates, use the following:

blog.tpl

{include file="html_header.tpl"}
{include file="layout_header.tpl"}  

<h1>Blog</h1>  

{include_php file='blog/custom/the-loop.inc.php'}     

{include file="layout_footer.tpl"}
{include file="html_footer.tpl"}

The html_header, layout_header, layout_footer and html_footer template files contain the XHTML which provides the layout for my site.

You can include the sidebar content anywhere on your side, but keep in mind that it can become quite long, and have multiple nested lists depending on how you set up your categories.

sidebar_blog.tpl


{include_php file='blog/custom/sidebar.inc.php'}

Step 6: url re-writing with .htaccess

To make sure your blog.php file is used instead of the default /blog/index.php, add the following .htaccess rules.

RewriteEngine on
Options +FollowSymlinks
RewriteBase /     

RewriteRule ^(.*)blog$ $1blog/ [R=301,L]
RewriteRule ^(.*)blog/$ $1blog.php [L]

If you want to use URL re-writing for permalinks within WordPress, add the following lines to your .htaccess after the above lines

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)blog/(.*)$ $1blog.php [L]

You should with any luck now have WordPress integrated into your Smarty powered site!