Forum email not working
Forum email not working
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.
I modified the OpenKore forums to do this. I can send you the code if you like.
So am I.But we're using phpBB.
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)
);
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;
}
Code: Select all
$result = @mail($to, $this->subject, preg_replace("#(?<!\r)\n#s", "\n", $this->msg), $this->extra_headers);
Code: Select all
$result = $this->savemail($db, $to, $this->subject, preg_replace("#(?<!\r)\n#s", "\n", $this->msg), $this->extra_headers);
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;
}
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);
}
Type: mdaemon.pl
Then exit. mdaemon.pl will continue to run in the background.