sendemail.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #!/usr/bin/env php
  2. <?php
  3. /**
  4. * GNU social - a federating social network
  5. *
  6. * LICENCE: This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. *
  19. * @category Plugin
  20. * @package GNUsocial
  21. * @copyright 2008 Free Software Foundation http://fsf.org
  22. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  23. * @link https://www.gnu.org/software/social/
  24. */
  25. define('INSTALLDIR', dirname(__DIR__));
  26. define('PUBLICDIR', INSTALLDIR);
  27. $shortoptions = 'i:n:a:';
  28. $longoptions = ['id=', 'nickname=', 'subject=', 'all='];
  29. $helptext = <<<END_OF_USEREMAIL_HELP
  30. sendemail.php [options] < <message body>
  31. Sends given email text to user.
  32. -i --id id of the user to query
  33. -a --all send to all users
  34. -n --nickname nickname of the user to query
  35. --subject mail subject line (required)
  36. END_OF_USEREMAIL_HELP;
  37. require_once INSTALLDIR.'/scripts/commandline.inc';
  38. $all = have_option('a', 'all');
  39. if ($all) {
  40. $user = new User();
  41. $user->find();
  42. } else if (have_option('i', 'id')) {
  43. $id = get_option_value('i', 'id');
  44. $user = User::getKV('id', $id);
  45. if (empty($user)) {
  46. print "Can't find user with ID $id\n";
  47. exit(1);
  48. }
  49. unset ($id);
  50. } else if (have_option('n', 'nickname')) {
  51. $nickname = get_option_value('n', 'nickname');
  52. $user = User::getKV('nickname', $nickname);
  53. if (empty($user)) {
  54. print "Can't find user with nickname '$nickname'.\n";
  55. exit(1);
  56. }
  57. unset($nickname);
  58. } else {
  59. print "You must provide a user by --id, --nickname or just send something to --all\n";
  60. exit(1);
  61. }
  62. if (!have_option('subject')) {
  63. echo "You must provide a subject line for the mail in --subject='...' param.\n";
  64. exit(1);
  65. }
  66. $subject = get_option_value('subject');
  67. if (posix_isatty(STDIN)) {
  68. print "You must provide message input on stdin!\n";
  69. exit(1);
  70. }
  71. $body = file_get_contents('php://stdin');
  72. if ($all) {
  73. while ($user->fetch()) {
  74. _send($user, $subject, $body);
  75. }
  76. } else {
  77. _send($user, $subject, $body);
  78. }
  79. function _send($user, $subject, $body) {
  80. if (empty($user->email)) {
  81. // @fixme unconfirmed address?
  82. print "No email registered for user '$user->nickname'.\n";
  83. return;
  84. }
  85. print "Sending to $user->email... ";
  86. if (mail_to_user($user, $subject, $body)) {
  87. print "done.\n";
  88. } else {
  89. print "failed.\n";
  90. return;
  91. }
  92. }