Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100755 94 lines (74 sloc) 2.716 kb
94d362b Sent first newsletter
frabcus authored
1 #!/usr/bin/perl
2 use strict;
6e458fc Renamed scraper as loader
frabcus authored
3 use lib "loader/";
94d362b Sent first newsletter
frabcus authored
4
fa403c9 Move to ukparse
theyworkforyou authored
5 my $text = "website/newsletters/issue8.txt";
9d89398 Removed date.
theyworkforyou authored
6 my $type = "all";
a49ab09 Sent mailCVS: ----------------------------------------------------------...
theyworkforyou authored
7 my $test_name = "";
8
9 #my $type = "all";
512ac99 Loadsa stuff
theyworkforyou authored
10 #my $test_name = "Jo Kibble";
a49ab09 Sent mailCVS: ----------------------------------------------------------...
theyworkforyou authored
11 #my $test_name = "Francis Irving";
11cac06 Updateroo
frabcus authored
12 #my $test_name = "Julian Todd";
94d362b Sent first newsletter
frabcus authored
13
43383fe Newsletter stuff
theyworkforyou authored
14 my $amount = 1000000;
a40ae03 Newsletter
frabcus authored
15
55812cc New extra newsletter for Euro elections
frabcus authored
16 use PublicWhip::Error;
17 use PublicWhip::DB;
18 my $dbh = PublicWhip::DB::connect();
94d362b Sent first newsletter
frabcus authored
19
a40ae03 Newsletter
frabcus authored
20 # Extra where clause
21 my $where = "";
22 if ($test_name ne "") {
23 $where = "and real_name = '$test_name'";
24 }
25 my $where_newsletter = "and is_confirmed = 1 and is_newsletter=1 ";
26 my $already_clause = "left join pw_dyn_newsletters_sent on
27 pw_dyn_newsletters_sent.user_id = pw_dyn_user.user_id and
28 newsletter_name = ? where newsletter_name is null";
29
30 # Create query string
31 my $query;
32 if ($type eq "all") {
c5bea6a Last tweaks for newsletter 7
frabcus authored
33 $query = "select real_name, email, user_name, pw_dyn_user.user_id from pw_dyn_user
a40ae03 Newsletter
frabcus authored
34 $already_clause $where_newsletter $where";
35
36 } elsif ($type eq "dream") {
5eff51f Rename rollie to dream
frabcus authored
37 $query = "select real_name, email, user_name, pw_dyn_user.user_id, count(pw_dyn_dreamvote.vote) as count
38 from pw_dyn_dreammp, pw_dyn_user, pw_dyn_dreamvote
a40ae03 Newsletter
frabcus authored
39 $already_clause $where_newsletter and
5eff51f Rename rollie to dream
frabcus authored
40 pw_dyn_dreammp.user_id = pw_dyn_user.user_id and
41 pw_dyn_dreamvote.rolliemp_id = rollie_id
a40ae03 Newsletter
frabcus authored
42 $where
43 group by pw_dyn_user.user_id
44 order by count desc";
45 } else {
46 die "Choose type"
94d362b Sent first newsletter
frabcus authored
47 }
a40ae03 Newsletter
frabcus authored
48 $query .= " limit $amount";
49
50 # Send mailshot
51 my $sth = PublicWhip::DB::query($dbh, $query, $text);
52 my $all = $sth->fetchall_hashref('user_id');
d2ab440 New newsletter
frabcus authored
53 print "Sending to " . $sth->rows . " people\n";
a40ae03 Newsletter
frabcus authored
54 foreach my $k (keys %$all)
94d362b Sent first newsletter
frabcus authored
55 {
a40ae03 Newsletter
frabcus authored
56 my $data = $all->{$k};
57
58 my $email = $data->{'email'};
59 my $username = $data->{'user_name'};
60 my $realname = $data->{'real_name'};
61 my $userid = $data->{'user_id'};
62 my $dreamcount = $data->{'count'};
63
94d362b Sent first newsletter
frabcus authored
64 $realname =~ s/@/(at)/;
65 my $to = $realname . " <" . $email . ">";
66
c5bea6a Last tweaks for newsletter 7
frabcus authored
67 print "Sending to $to who is $username";
68 print " (dream count $dreamcount)" if ($type eq "dream");
69 print "...";
94d362b Sent first newsletter
frabcus authored
70
71 open(SENDMAIL, "|/usr/lib/sendmail -oi -t") or die "Can't fork for sendmail: $!\n";
72 print SENDMAIL <<"EOF";
e49b765 End protest
frabcus authored
73 From: Public Whip Team <team\@publicwhip.org.uk>
94d362b Sent first newsletter
frabcus authored
74 To: $to
75 EOF
76
77 open (TEXT, $text) || die "Can't open newsletter $text : $!";
78 while (<TEXT>) {
79 print SENDMAIL $_;
80 }
81
82 print SENDMAIL "\nYou are subscribed as user $username with email $email\n";
83
512ac99 Loadsa stuff
theyworkforyou authored
84 close(SENDMAIL) or die "sendmail didn't close nicely";
94d362b Sent first newsletter
frabcus authored
85
a40ae03 Newsletter
frabcus authored
86 PublicWhip::DB::query($dbh, "insert into pw_dyn_newsletters_sent (user_id, newsletter_name)
87 values (?, ?)", $userid, $text);
88
94d362b Sent first newsletter
frabcus authored
89 print "done\n";
c167087 Notes
frabcus authored
90
43383fe Newsletter stuff
theyworkforyou authored
91 sleep 2; # One second probably enough
94d362b Sent first newsletter
frabcus authored
92 }
93
Something went wrong with that request. Please try again.