Skip to content

Pravin Paratey

Natural Language Processing, Data mining and Information Extraction consultant based in London.

Oct 23 2006
Oct 23 2006

Running bbpress on sourceforge

Changelog

[June 14, 2007]
Updated for bbPress 0.8.1
[October 23, 2006]
First version of this document

Background

Sourceforge does not allow php mailers[1]. 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:

 1 &lt;?php
 2 /* Additional functions for enabling mail on sourceforge
 3  * by Pravin Paratey
 4  * http://pravin.insanitybegins.com/articles/running-bbpress-on-sourceforge/
 5  *
 6  *
 7  * Note: I use the same database as bbpress. I create an additional table
 8  * called sfMailTable
 9  *
10  */
11 
12 
13 // This function pushes data into the table
14 function sfmail ($email, $subject, $message) {
15 	global $bbdb;
16 	$table = 'sfMailTable';
17 
18 	// Create table if it does not exist
19 	if($bbdb->get_var("SHOW TABLES LIKE '$table'") != $table) {
20 		$sql = "CREATE TABLE $table (
21 			id	bigint	not null auto_increment,
22 			email	text	not null,
23 			subject	text	not null,
24 			message	text	not null,
25 			unique key id(id)
26 			);";
27 		$results = $bbdb->query($sql);
28 	}
29 
30 	// Push email data into the table
31 	$results = $bbdb->query("INSERT INTO `$table` (email, subject, message)" .
32 		"VALUES ('$email', '$subject', '$message');");
33 }
34 ?&gt;

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:

 1 #!/usr/bin/perl
 2 # This script is responsible for pulling values out of the database
 3 # and emailing.
 4 # by Pravin Paratey
 5 # http://pravin.insanitybegins.com/articles/running-bbpress-on-sourceforge/
 6 
 7 use DBI;
 8 
 9 #---------------------------------------------
10 #Edit this and replace with your sf db details
11 my $dsn = 'DBI:mysql:p133996_pravin:mysql4-p'; # Change to database-name:hostname
12 my $db_username = 'p133996admin'; # Change this to your db username
13 my $db_password = 'password'; # Change this to your password
14 
15 #--------------------------------------------------------------
16 #Do not Edit below this line unless you know what you are doing
17 
18 my $sfMailTable = 'sfMailTable';
19 
20 my $db = DBI->connect($dsn, $db_username, $db_password) or
21 	die "Cannot connect to database";
22 
23 my $query = $db->prepare("select * from $sfMailTable");
24 
25 $query->execute();
26 
27 while(my ($id, $email, $subject, $message) = $query->fetchrow_array()) {
28 	open(SENDMAIL, "|/bin/mail -s '$subject' $email") or
29 		die ("Cannot open mail");
30 	print SENDMAIL $message;
31 	close(SENDMAIL);
32 
33 	# delete row
34 	$query2 = $db->prepare("delete from `$sfMailTable` where id=$id");
35 	$query2->execute();
36 }
37 
38 $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.

Latest Articles