send-email.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #!/usr/bin/env php5
  2. <?php
  3. if (PHP_SAPI != 'cli') {
  4. die("This script must be run from the commandline.\n");
  5. }
  6. require_once('adodb/adodb-exceptions.inc.php');
  7. require_once('adodb/adodb.inc.php');
  8. $connect_string = # FILL IN YOUR CONNECT STRING HERE BEFORE USE
  9. $delay = 1;
  10. try {
  11. $adodb =& NewADOConnection($connect_string);
  12. } catch (Exception $e) {
  13. var_dump($e);
  14. adodb_backtrace($e->gettrace());
  15. }
  16. if(count($argv) < 2) {
  17. die("Usage: " . $argv[0] . " <filename>\n\nWhere <filename> is a file containing a template of the e-mail you'd like to send.\n");
  18. }
  19. $tfile = fopen($argv[1], 'r');
  20. $header = "From: " . fgets($tfile);
  21. $subject = fgets($tfile);
  22. $template = str_replace('"', '\"', fread($tfile, filesize($argv[1])));
  23. fclose($tfile);
  24. if(empty($template)) {
  25. die("The template appears to be empty, giving up.");
  26. }
  27. $res = $adodb->GetAll("SELECT username, md5(md5(email) || md5(password)) as unsubid, email FROM Users WHERE receive_emails = 1 AND active = 1 AND email <> '' ORDER BY uniqueid ASC");
  28. $total = count($res);
  29. echo "\nYou're about to send " . $total . " e-mails, with a " . $delay . " second delay
  30. between each. This is estimated to take roughly " . round(($total * $delay) / 3600, 2) . " hours.
  31. The text of the message will look as follows:\n\n\n";
  32. // Show an example
  33. $username = "Elleo";
  34. $unsubscribe = "http://alpha.libre.fm/unsubscribe.php?id=dummy";
  35. $emailtext = eval('return "' . $template . '";');
  36. echo $header;
  37. echo "Subject: " . $subject . "\n";
  38. echo $emailtext;
  39. system("stty -icanon");
  40. $c = '';
  41. do {
  42. if ($c == 'n') {
  43. die("\nAborting!\n");
  44. }
  45. echo "\n\nAre you sure you want to send this message to " . $total . " people? (y/n): ";
  46. } while(($c = fread(STDIN, 1)) != 'y');
  47. echo "\n";
  48. $i = 1;
  49. foreach($res as &$row) {
  50. $username = $row['username'];
  51. $email = $row['email'];
  52. $unsubscribe = "http://alpha.libre.fm/unsubscribe.php?id=" . $row['unsubid'];
  53. $emailtext = eval('return "' . $template . '";');
  54. printf("[%" . strlen($total) . "s/%s]", $i, $total);
  55. echo " Sending to " . $username . " (" . $email . ")... ";
  56. mail($email, $subject, $emailtext, $header);
  57. echo "Sent!\n";
  58. sleep($delay);
  59. $i++;
  60. }
  61. ?>