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