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