rss
logo

I provide consulting and custom development for Natural Language Processing, Information Extraction and Search solutions.Self Picture


 learn more   get in touch 

Logo - I Build Search
Oct 23
2006

Running bbpress on sourceforge digg

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 &lt?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
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,

  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.

6 Responses (rss) (trackback)

#1

matt wilkie

June 14th, 2007 at 10:21 pm

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.php near 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

#2

matt wilkie

June 14th, 2007 at 10:34 pm

Addendum:

There are two occurences of mail( to replace with sfmail( (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

#3

Pravin

June 14th, 2007 at 11:34 pm

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.

#4

matt wilkie

June 19th, 2007 at 11:13 pm

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.

#5

Rodolfo Castrezana

July 27th, 2007 at 1:09 pm

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

#6

Suresh

January 7th, 2010 at 10:00 am

Mr.Pravin,
There is no file called registration-functions.php in bb-includes folder in BbPress Version 1.0.2.

Please help.

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Latest Articles

Feb
19

Join a list of integers in Python

How do you run a string join on a list of integers in Python? After googling for about 10 mins, I gave up and did this. I am sure there is a better way of doing it! [Read More]
Jan
21

Writing a spider in 10 mins using Scrapy

I came across Scrapy a few days back and have grown to really love it. This tutorial will illustrate how you can write a simple spider using Scrapy to scrape data off Paul Smith. All this in 10 minutes. [Read More]

Featured Projects

Document Tagger

Document Tagger

DocTagger lets you automatically classify text documents. Use this as a starting point to write apps that can sort through volumes of unorganized data.

[Read More]

NLP classes for PHP

NLP classes for PHP

This is an ongoing project to develop a set of classes for Natural Language Processing. Some code would be ported from the NLTK project.

[Read More]

This page and its contents are copyright © 2010, Pravin Paratey.