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.
- Open
bb-includes/registration-functions.phpin your favourite editor. - Add
include_once('sf-functions.php');immediately after<?php. - Next use Find and Replace to replace all occurances of
mail(withsfmail(in this file.
Now create another file called sf-functions.php in bb-includes directory with the following code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | <?php /* Additional functions for enabling mail on sourceforge * by Pravin Paratey * http://pravin.insanitybegins.com/articles/running-bbpress-on-sourceforge/ * * * Note: I use the same database as bbpress. I create an additional table * called sfMailTable * */ // This function pushes data into the table function sfmail ($email, $subject, $message) { global $bbdb; $table = 'sfMailTable'; // Create table if it does not exist if($bbdb->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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | #!/usr/bin/perl # This script is responsible for pulling values out of the database # and emailing. # by Pravin Paratey # http://pravin.insanitybegins.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,
- Registering a user in bbpress
- Running sendmail.pl
- 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 crontabwould be your best guide.


Pravin, thank you very much indeed for taking the time to update this to match the current codebase!
A couple of clarifications:
* don’t forget to put a semicolon at the end of the intial
include_once('sf-functions.php');else you’ll get a parse error several lines further down. (yes a silly newbie mistake, maybe everyone else is smart enough to know that already but it tripped me up so I figured I might as well say something* don’t search & replace simply “mail” but “mail(“, otherwise embedded mail strings will get changed also, e.g. $email –> $esfmail, which we don’t want
* the password reset function will cause a mysql error because of an embedded single quote in
registration-functions.phpnear line 50, “If you don’t want to reset your password,”. A simple fix is to change that to “do not”. There is probably a cleaner/safer (from a security viewpoint) way to do this though.Note: password resets can take more than 2 hours to complete: up to 1hr for the initial email, and then up to an additional hour after visiting the “yes I really want to reset my password” confirmation link. So while changing the “don’t” contraction above you might as well add a note to this effect for your users.
thanks again Pravin!
-matt
Addendum:
There are two occurences of
mail(to replace withsfmail((as of v0.8.1).The best place to add the note about how long it takes to receive the confirmation emails is actually in the templates. I found 2 places:
bb-templates/kakumei/register-success.php
bb-templates/kakumei/password-reset.php
Matt: Thank you for your feedback. I have incorporated your corrections in this article. I don’t know how to solve the single quote issue. I suppose for now we’ll just have to change don’t to do not.
pravin: many many thanks for your efforts. It is much appreciated.
Unfortunately it appears this might all be for naught. I just learned Sourceforge has disabled cron indefinately: “As of 2007-06-12 the cron service provided under the project shell service has been disabled. Ongoing systems problems have forced us to pull it offline until a suitable replacement can be developed. Accordingly, no estimate for when we’ll restore service is being provided.” – https://sourceforge.net/docs/A04
https://sourceforge.net/tracker/?func=detail&atid=200001&aid=1736741&group_id=1
sigh.
Still, at least I can periodically ssh in and run the trigger script from the command line once or twice a day, so it’s not completely irrelevant.
And a refinement for the templates: save the changed files into
./my-templates/so they don’t get clobbered on upgrades.I’m not a developer or something like that, I’m just a trial and error curious guy.
The first step (Preparing bbPress): Ok, i can do that (but I didn’t, yet).
Step two (Email Script); I really don’t know where create the folder to put the sendmail.pl file. If I’m not sure I do not do, to keep the code safe.
Step three (Setting up cron): It’s like greek to me. What’s **** is “cron”? The God from Conan?
Sorry, I’m noob but i really want to correct that problem. Can you help, me please?
Rodolfo Castrezana
castrezana@gmail.com
http://forum.omedi.net
Mr.Pravin,
There is no file called registration-functions.php in bb-includes folder in BbPress Version 1.0.2.
Please help.