sendemail.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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', realpath(dirname(__DIR__)));
  26. $shortoptions = 'i:n:a:';
  27. $longoptions = ['id=', 'nickname=', 'subject=', 'all='];
  28. $helptext = <<<END_OF_USEREMAIL_HELP
  29. sendemail.php [options] < <message body>
  30. Sends given email text to user.
  31. -i --id id of the user to query
  32. -a --all send to all users
  33. -n --nickname nickname of the user to query
  34. --subject mail subject line (required)
  35. END_OF_USEREMAIL_HELP;
  36. require_once INSTALLDIR.'/scripts/commandline.inc';
  37. $all = have_option('a', 'all');
  38. if ($all) {
  39. $user = new User();
  40. $user->find();
  41. } else if (have_option('i', 'id')) {
  42. $id = get_option_value('i', 'id');
  43. $user = User::getKV('id', $id);
  44. if (empty($user)) {
  45. print "Can't find user with ID $id\n";
  46. exit(1);
  47. }
  48. unset ($id);
  49. } else if (have_option('n', 'nickname')) {
  50. $nickname = get_option_value('n', 'nickname');
  51. $user = User::getKV('nickname', $nickname);
  52. if (empty($user)) {
  53. print "Can't find user with nickname '$nickname'.\n";
  54. exit(1);
  55. }
  56. unset($nickname);
  57. } else {
  58. print "You must provide a user by --id, --nickname or just send something to --all\n";
  59. exit(1);
  60. }
  61. if (!have_option('subject')) {
  62. echo "You must provide a subject line for the mail in --subject='...' param.\n";
  63. exit(1);
  64. }
  65. $subject = get_option_value('subject');
  66. if (posix_isatty(STDIN)) {
  67. print "You must provide message input on stdin!\n";
  68. exit(1);
  69. }
  70. $body = file_get_contents('php://stdin');
  71. if ($all) {
  72. while ($user->fetch()) {
  73. _send($user, $subject, $body);
  74. }
  75. } else {
  76. _send($user, $subject, $body);
  77. }
  78. function _send($user, $subject, $body) {
  79. if (empty($user->email)) {
  80. // @fixme unconfirmed address?
  81. print "No email registered for user '$user->nickname'.\n";
  82. return;
  83. }
  84. print "Sending to $user->email... ";
  85. if (mail_to_user($user, $subject, $body)) {
  86. print "done.\n";
  87. } else {
  88. print "failed.\n";
  89. return;
  90. }
  91. }