Hello, I am an Engineering Manager at Facebook with 13+ years in Ad Technology, Natural Language Processing and Data mining. (Learn More)
by Pravin Paratey

Running bbpress on sourceforge

Changelog

  1. June 14, 2007 - Updated for bbPress 0.8.1
  2. October 23, 2006 - First version of this document

Background

Sourceforge does not allow php mailers1. Software like bbPress which use the mailer to send newly registered users their password fails. This article will describe how you can solve this problem.

Preparing bbPress

The following steps are for bbPress v0.8.1. Adapt these for other (future) versions.

  1. Open bb-includes/registration-functions.php in your favourite editor.
  2. Add include_once('sf-functions.php'); immediately after <?php.
  3. Next use Find and Replace to replace all occurances of mail( with sfmail( in this file.

Now create another file called sf-functions.php in bb-includes directory with the following code:

get_var("SHOW TABLES LIKE '$table'") != $table) {
		$sql = "CREATE TABLE $table (
			id	bigint	not null auto_increment,
			email	text	not null,
			subject	text	not null,
			message	text	not null,
			unique key id(id)
			);";
		$results = $bbdb->query($sql);
	}

	// Push email data into the table
	$results = $bbdb->query("INSERT INTO `$table` (email, subject, message)" .
		"VALUES ('$email', '$subject', '$message');");
}
?>

This function pushes to-email messages into the database.

Email Script

Next, we will write a perl script which will pull out values from the database and email them. This script will be called by the cron daemon every hour.

For the sake of this demonstration, the project name is assumed to be pravin. Replace instances of this with your own project name and path.

Create a file called sendmail.pl at /home/groups/p/pr/pravin/bin/ (you’ll have to create the bin directory) with the following code:

#!/usr/bin/perl
# This script is responsible for pulling values out of the database
# and emailing.
# by Pravin Paratey
# http://pravin.paratey.com/articles/running-bbpress-on-sourceforge/

use DBI;

#---------------------------------------------
# Edit this and replace with your sf db details
my $dsn = 'DBI:mysql:p133996_pravin:mysql4-p'; # Change to database-name:hostname
my $db_username = 'p133996admin'; # Change this to your db username
my $db_password = 'password'; # Change this to your password

#--------------------------------------------------------------
# Do not Edit below this line unless you know what you are doing

my $sfMailTable = 'sfMailTable';

my $db = DBI->connect($dsn, $db_username, $db_password) or
	die "Cannot connect to database";

my $query = $db->prepare("select * from $sfMailTable");

$query->execute();

while(my ($id, $email, $subject, $message) = $query->fetchrow_array()) {
	open(SENDMAIL, "|/bin/mail -s '$subject' $email") or
		die ("Cannot open mail");
	print SENDMAIL $message;
	close(SENDMAIL);

	# delete row
	$query2 = $db->prepare("delete from `$sfMailTable` where id=$id");
	$query2->execute();
}

$db->disconnect();

Mark this file as executable by:

$ chmod +x sendmail.pl

Test if everything works by,

  1. Registering a user in bbpress
  2. Running sendmail.pl
  3. Check if you receive the registration successful mail

Setting up cron

Now that the script works, we need to get cron to call it every hour. Type crontab -e and add the lines:

# Send email every hour
6 * * * * /home/groups/p/pr/pravin/bin/sendmail.pl

That’s it. You’re done!

Troubleshooting

The perl script gives a "Table not found" error.
The table is created (if it doesn't exist) via `sf-functions.php`. This means the table is created when a user registers. Just register an arbitrary user to make this error go away.
I want the script to execute every x minutes.
I do not know the specifics of cron. `man 5 crontab` would be your best guide.