Forum email not working

Web discussions, including project website, forums, wiki, and others.
Post Reply
VCL
Peon
Peon
Posts: 4
Joined: 25 Sep 2004, 18:04

Forum email not working

Post by VCL » 01 Mar 2005, 22:39

As you may have noticed, forum email stopped working a month ago. SourceForge made webserver applications unable to send mail to prevent spam abuse. Projects must now queue email in the database, and run an application on the shell server which periodically downloads email from the queue, and then send it.

I modified the OpenKore forums to do this. I can send you the code if you like.
User avatar
Bjørn
TMW Adviser
TMW Adviser
Posts: 1430
Joined: 09 Dec 2004, 19:50
Location: North Rhine-Westphalia, Germany
Contact:

Post by Bjørn » 02 Mar 2005, 11:24

But we're using phpBB. If you think your modifications apply to us as well then feel free to send it, and we'll look into it.
User avatar
maci
Warrior
Warrior
Posts: 507
Joined: 05 Dec 2004, 21:01
Location: Germany
Contact:

Post by maci » 02 Mar 2005, 12:40

shouldnt be that hard to implent this
ElvenProgrammer wrote:Maci: don't be rude, we're here to help people ;)
VCL
Peon
Peon
Posts: 4
Joined: 25 Sep 2004, 18:04

Post by VCL » 03 Mar 2005, 18:04

But we're using phpBB.
So am I. :roll:

phpBB sucks at posting code because it destroys tabs. Anyway...

You will need the following MySQL table:

Code: Select all

CREATE TABLE mailer (
   id INT UNSIGNED AUTO_INCREMENT,
   mailto VARCHAR(255) NOT NULL,
   subject VARCHAR(255) NOT NULL,
   body TEXT NOT NULL,
   extra_headers TEXT NOT NULL,
   PRIMARY KEY (id)
);
In includes/emailer.php, add the following function to the class:

Code: Select all

function savemail($db, $to, $subject, $body, $extra_headers)
	{
		$sql = sprintf("INSERT INTO mailer VALUES(NULL, '%s', '%s', '%s', '%s');",
			mysql_escape_string($to),
			mysql_escape_string($subject),
			mysql_escape_string($body),
			mysql_escape_string($extra_headers)
			);
		$db->sql_query($sql);
		return 1;
	}
In send(), look for:

Code: Select all

$result = @mail($to, $this->subject, preg_replace("#(?<!\r)\n#s", "\n", $this->msg), $this->extra_headers);
Replace with:

Code: Select all

$result = $this->savemail($db, $to, $this->subject, preg_replace("#(?<!\r)\n#s", "\n", $this->msg), $this->extra_headers);
In your shell server's home folder, save the following script as mailer.pl:

Code: Select all

#!/usr/bin/perl -w
use strict;
use DBI;
use Net::SMTP;

my $smtp = 'mail.sourceforge.net';
my $mailfrom = 'themanaworld@themanaworld.sourceforge.net';

my $database = 'themanaworld';
my $user = 'themanaworld';
my $pass = 'your password';
my $host = 'mysql.sourceforge.net';
my $table = 'mailer';

my $dbh=DBI->connect("DBI:mysql:$database:$host", $user, $pass);

my $sql = "SELECT * FROM $table;";
my $sth = $dbh->prepare($sql);
my $rc = $sth->execute;

my @mails;
while (my @r = $sth->fetchrow_array) {
	push @mails, \@r;
}
$dbh->do("TRUNCATE TABLE $table");

my $i = 1;
foreach my $r (@mails) {
	print "Sending mail $i of " . @mails . "...\n";
	my %mail = (
		'To' => $r->[1],
 		'From' => $mailfrom,
		'Subject' => $r->[2],
		'Message' => $r->[3],
		'X-Mailer' => "Openkore Forum Mailer v0.1"
	);
	sendmail(\%mail);
	$i++;	
}

sub sendmail {
	my $mail = shift;

	my $smtp = Net::SMTP->new($smtp);
	$smtp->mail($mail->{From});
	$smtp->to($mail->{To});
	$smtp->data();
	$smtp->datasend("Subject: $mail->{Subject}\n");
	$smtp->datasend("X-Mailer: $mail->{'X-Mailer'}\n");
	$smtp->datasend("From: $mail->{From}\n");
	$smtp->datasend("To: $mail->{To}\n");
	$smtp->datasend("\n");
	$smtp->datasend("$mail->{Message}\n");
	$smtp->dataend();
	$smtp->quit;
}
Remember to set the password!


Although you're supposed to send the email in a cron job, cron doesn't work because crond is not running! So I wrote a little script that runs mailer.pl every 30 minutes.
Save as mdaemon.pl:

Code: Select all

#!/usr/bin/env perl
# cron doesn't work, bah
use strict;
use POSIX;

if (fork() == 0) {
	POSIX::setsid();
	open(STDIN, "< /dev/null");
	open(STDOUT, "> /dev/null");
	open(STDERR, "> /dev/null");
	while (1) {
		system('perl', '/full/path/to/mailer.pl');
		sleep(30 * 60);
	}
	POSIX::_exit(0);
}
Remember to set the full path to mailer.pl!
Type: mdaemon.pl
Then exit. mdaemon.pl will continue to run in the background.
User avatar
Bjørn
TMW Adviser
TMW Adviser
Posts: 1430
Joined: 09 Dec 2004, 19:50
Location: North Rhine-Westphalia, Germany
Contact:

Post by Bjørn » 01 Jun 2005, 13:08

Well, I finally made these modifications and ran the script. I hope it'll work.
User avatar
Bjørn
TMW Adviser
TMW Adviser
Posts: 1430
Joined: 09 Dec 2004, 19:50
Location: North Rhine-Westphalia, Germany
Contact:

Post by Bjørn » 02 Oct 2005, 22:07

Well this script had to be disabled again after we moved to the new server of course. Maybe now the emails will start rolling finally. Did we really miss them? :-)
User avatar
maci
Warrior
Warrior
Posts: 507
Joined: 05 Dec 2004, 21:01
Location: Germany
Contact:

Post by maci » 02 Oct 2005, 22:13

i missed them :P
ElvenProgrammer wrote:Maci: don't be rude, we're here to help people ;)
xand
Peon
Peon
Posts: 95
Joined: 19 Jul 2005, 19:08

Post by xand » 02 Oct 2005, 23:33

me too :evil:
Post Reply