4 * GNU social - a federating social network
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.
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.
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/>.
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/
26 define('INSTALLDIR', realpath(dirname(__DIR__)));
28 $shortoptions = 'i:n:a:';
29 $longoptions = ['id=', 'nickname=', 'subject=', 'all='];
31 $helptext = <<<END_OF_USEREMAIL_HELP
32 sendemail.php [options] < <message body>
33 Sends given email text to user.
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)
40 END_OF_USEREMAIL_HELP;
42 require_once INSTALLDIR.'/scripts/commandline.inc';
44 $all = have_option('a', 'all');
49 } else if (have_option('i', 'id')) {
50 $id = get_option_value('i', 'id');
51 $user = User::getKV('id', $id);
53 print "Can't find user with ID $id\n";
57 } else if (have_option('n', 'nickname')) {
58 $nickname = get_option_value('n', 'nickname');
59 $user = User::getKV('nickname', $nickname);
61 print "Can't find user with nickname '$nickname'.\n";
66 print "You must provide a user by --id, --nickname or just send something to --all\n";
70 if (!have_option('subject')) {
71 echo "You must provide a subject line for the mail in --subject='...' param.\n";
74 $subject = get_option_value('subject');
76 if (posix_isatty(STDIN)) {
77 print "You must provide message input on stdin!\n";
80 $body = file_get_contents('php://stdin');
83 while ($user->fetch()) {
84 _send($user, $subject, $body);
87 _send($user, $subject, $body);
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";
96 print "Sending to $user->email... ";
97 if (mail_to_user($user, $subject, $body)) {