]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/mail.php
more information in subscription notices
[quix0rs-gnu-social.git] / lib / mail.php
1 <?php
2 /*
3  * Laconica - a distributed open-source microblogging tool
4  * Copyright (C) 2008, Controlez-Vous, Inc.
5  *
6  * 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
20 if (!defined('LACONICA')) { exit(1); }
21
22 require_once('Mail.php');
23
24 function mail_backend() {
25         static $backend = NULL;
26
27         if (!$backend) {
28                 global $config;
29                 $backend = Mail::factory($config['mail']['backend'],
30                                                                  ($config['mail']['params']) ? $config['mail']['params'] : array());
31                 if (PEAR::isError($backend)) {
32                         common_server_error($backend->getMessage(), 500);
33                 }
34         }
35         return $backend;
36 }
37
38 # XXX: use Mail_Queue... maybe
39
40 function mail_send($recipients, $headers, $body) {
41         $backend = mail_backend();
42     if (!isset($headers['Content-Type'])) {
43         $headers['Content-Type'] = 'text/plain; charset=UTF-8';
44     }
45         assert($backend); # throws an error if it's bad
46         $sent = $backend->send($recipients, $headers, $body);
47         if (PEAR::isError($sent)) {
48                 common_log(LOG_ERR, 'Email error: ' . $sent->getMessage());
49                 return false;
50         }
51         return true;
52 }
53
54 function mail_domain() {
55         $maildomain = common_config('mail', 'domain');
56         if (!$maildomain) {
57                 $maildomain = common_config('site', 'server');
58         }
59         return $maildomain;
60 }
61
62 function mail_notify_from() {
63         $notifyfrom = common_config('mail', 'notifyfrom');
64         if (!$notifyfrom) {
65                 $domain = mail_domain();
66                 $notifyfrom = common_config('site', 'name') .' <noreply@'.$domain.'>';
67         }
68         return $notifyfrom;
69 }
70
71 function mail_to_user(&$user, $subject, $body, $address=NULL) {
72         if (!$address) {
73                 $address = $user->email;
74         }
75
76         $recipients = $address;
77         $profile = $user->getProfile();
78
79         $headers['From'] = mail_notify_from();
80         $headers['To'] = $profile->getBestName() . ' <' . $address . '>';
81         $headers['Subject'] = $subject;
82
83         return mail_send($recipients, $headers, $body);
84 }
85
86 # For confirming a Jabber address
87
88 function mail_confirm_address($user, $code, $nickname, $address) {
89
90         $subject = _('Email address confirmation');
91
92     $body = sprintf(_("Hey, %s.\n\nSomeone just entered this email address on %s.\n\n" .
93         "If it was you, and you want to confirm your entry, use the URL below:\n\n\t%s\n\n" .
94         "If not, just ignore this message.\n\nThanks for your time, \n%s\n")
95         , $nickname, common_config('site', 'name')
96         , common_local_url('confirmaddress', array('code' => $code)), common_config('site', 'name'));
97      return mail_to_user($user, $subject, $body, $address);
98 }
99
100 function mail_subscribe_notify($listenee, $listener) {
101         $other = $listener->getProfile();
102         mail_subscribe_notify_profile($listenee, $other);
103 }
104
105 function mail_subscribe_notify_profile($listenee, $other) {
106         if ($listenee->email && $listenee->emailnotifysub) {
107         // use the recipients localization
108         common_init_locale($listenee->language);
109                 $profile = $listenee->getProfile();
110                 $name = $profile->getBestName();
111                 $long_name = ($other->fullname) ? ($other->fullname . ' (' . $other->nickname . ')') : $other->nickname;
112                 $recipients = $listenee->email;
113                 $headers['From'] = mail_notify_from();
114                 $headers['To'] = $name . ' <' . $listenee->email . '>';
115                 $headers['Subject'] = sprintf(_('%1$s is now listening to your notices on %2$s.'),
116                                       $other->getBestName(),
117                                                                           common_config('site', 'name'));
118                 $body  = sprintf(_('%1$s is now listening to your notices on %2$s.'."\n\n".
119                                                    "\t".'%3$s'."\n\n".
120                                                    '%4$s'.
121                            '%5$s'.
122                            '%6$s'.
123                                                    "\n".'Faithfully yours,'."\n".'%7$s.'."\n\n".
124                            "----\n".
125                            "Change your email address or notification options at %8$s"),
126                          $long_name,
127                          common_config('site', 'name'),
128                          $other->profileurl,
129                          ($other->location) ? sprintf(_("Location: %s\n"), $other->location) : '',
130                          ($other->homepage) ? sprintf(_("Homepage: %s\n"), $other->homepage) : '',
131                          ($other->bio) ? sprintf(_("Bio: %s\n\n"), $other->bio) : '',
132                          common_config('site', 'name'),
133                          common_local_url('emailsettings'));
134         // reset localization
135         common_init_locale();
136                 mail_send($recipients, $headers, $body);
137         }
138 }
139
140 function mail_new_incoming_notify($user) {
141
142         $profile = $user->getProfile();
143         $name = $profile->getBestName();
144
145         $headers['From'] = $user->incomingemail;
146         $headers['To'] = $name . ' <' . $user->email . '>';
147         $headers['Subject'] = sprintf(_('New email address for posting to %s'),
148                                                                   common_config('site', 'name'));
149
150         $body  = sprintf(_("You have a new posting address on %1\$s.\n\n".
151                                            "Send email to %2\$s to post new messages.\n\n".
152                                            "More email instructions at %3\$s.\n\n".
153                                            "Faithfully yours,\n%4\$s"),
154                                          common_config('site', 'name'),
155                                          $user->incomingemail,
156                                          common_local_url('doc', array('title' => 'email')),
157                                          common_config('site', 'name'));
158
159         mail_send($user->email, $headers, $body);
160 }
161
162 function mail_new_incoming_address() {
163         $prefix = common_confirmation_code(64);
164         $suffix = mail_domain();
165         return $prefix . '@' . $suffix;
166 }
167
168 function mail_broadcast_notice_sms($notice) {
169
170     # Now, get users subscribed to this profile
171
172         $user = new User();
173
174         $user->query('SELECT nickname, smsemail, incomingemail ' .
175                                  'FROM user JOIN subscription ' .
176                                  'ON user.id = subscription.subscriber ' .
177                                  'WHERE subscription.subscribed = ' . $notice->profile_id . ' ' .
178                                  'AND user.smsemail IS NOT NULL ' .
179                                  'AND user.smsnotify = 1 ' .
180                  'AND subscription.sms = 1 ');
181
182         while ($user->fetch()) {
183                 common_log(LOG_INFO,
184                                    'Sending notice ' . $notice->id . ' to ' . $user->smsemail,
185                                    __FILE__);
186                 $success = mail_send_sms_notice_address($notice, $user->smsemail, $user->incomingemail);
187                 if (!$success) {
188                         # XXX: Not sure, but I think that's the right thing to do
189                         common_log(LOG_WARNING,
190                                            'Sending notice ' . $notice->id . ' to ' . $user->smsemail . ' FAILED, cancelling.',
191                                            __FILE__);
192                         return false;
193                 }
194         }
195
196         $user->free();
197         unset($user);
198
199         return true;
200 }
201
202 function mail_send_sms_notice($notice, $user) {
203         return mail_send_sms_notice_address($notice, $user->smsemail, $user->incomingemail);
204 }
205
206 function mail_send_sms_notice_address($notice, $smsemail, $incomingemail) {
207
208         $to = $nickname . ' <' . $smsemail . '>';
209         $other = $notice->getProfile();
210
211         common_log(LOG_INFO, "Sending notice " . $notice->id . " to " . $smsemail, __FILE__);
212
213         $headers = array();
214         $headers['From'] = (isset($incomingemail)) ? $incomingemail : mail_notify_from();
215         $headers['To'] = $to;
216         $headers['Subject'] = sprintf(_('%s status'),
217                                                                   $other->getBestName());
218         $body = $notice->content;
219
220         return mail_send($smsemail, $headers, $body);
221 }
222
223 function mail_confirm_sms($code, $nickname, $address) {
224
225         $recipients = $address;
226
227         $headers['From'] = mail_notify_from();
228         $headers['To'] = $nickname . ' <' . $address . '>';
229         $headers['Subject'] = _('SMS confirmation');
230
231         $body = "$nickname: confirm you own this phone number with this code:";
232         $body .= "\n\n";
233         $body .= $code;
234         $body .= "\n\n";
235
236         mail_send($recipients, $headers, $body);
237 }
238
239 function mail_notify_nudge($from, $to) {
240     common_init_locale($to->language);
241         $subject = sprintf(_('You\'ve been nudged by %s'), $from->nickname);
242
243         $from_profile = $from->getProfile();
244
245         $body = sprintf(_("%1\$s (%2\$s) is wondering what you are up to these days and is inviting you to post some news.\n\n".
246                                           "So let's hear from you :)\n\n".
247                                           "%3\$s\n\n".
248                                           "Don't reply to this email; it won't get to them.\n\n".
249                                           "With kind regards,\n".
250                                           "%4\$s\n"),
251                                         $from_profile->getBestName(),
252                                         $from->nickname,
253                                         common_local_url('all', array('nickname' => $to->nickname)),
254                                         common_config('site', 'name'));
255     common_init_locale();
256         return mail_to_user($to, $subject, $body);
257 }
258
259 function mail_notify_message($message, $from=NULL, $to=NULL) {
260
261         if (is_null($from)) {
262                 $from = User::staticGet('id', $message->from_profile);
263         }
264
265         if (is_null($to)) {
266                 $to = User::staticGet('id', $message->to_profile);
267         }
268
269         if (is_null($to->email) || !$to->emailnotifymsg) {
270                 return true;
271         }
272
273     common_init_locale($to->language);
274         $subject = sprintf(_('New private message from %s'), $from->nickname);
275
276         $from_profile = $from->getProfile();
277
278         $body = sprintf(_("%1\$s (%2\$s) sent you a private message:\n\n".
279                                           "------------------------------------------------------\n".
280                                           "%3\$s\n".
281                                           "------------------------------------------------------\n\n".
282                                           "You can reply to their message here:\n\n".
283                                           "%4\$s\n\n".
284                                           "Don't reply to this email; it won't get to them.\n\n".
285                                           "With kind regards,\n".
286                                           "%5\$s\n"),
287                                         $from_profile->getBestName(),
288                                         $from->nickname,
289                                         $message->content,
290                                         common_local_url('newmessage', array('to' => $from->id)),
291                                         common_config('site', 'name'));
292
293     common_init_locale();
294         return mail_to_user($to, $subject, $body);
295 }
296
297 function mail_notify_fave($other, $user, $notice) {
298
299         $profile = $user->getProfile();
300         $bestname = $profile->getBestName();
301     common_init_locale($other->language);
302         $subject = sprintf(_('%s added your notice as a favorite'), $bestname);
303         $body = sprintf(_("%1\$s just added your notice from %2\$s as one of their favorites.\n\n" .
304                                           "In case you forgot, you can see the text of your notice here:\n\n" .
305                                           "%3\$s\n\n" .
306                                           "You can see the list of %1\$s's favorites here:\n\n" .
307                                           "%4\$s\n\n" .
308                                           "Faithfully yours,\n" .
309                                           "%5\$s\n"),
310                                         $bestname,
311                                         common_exact_date($notice->created),
312                                         common_local_url('shownotice', array('notice' => $notice->id)),
313                                         common_local_url('showfavorites', array('nickname' => $user->nickname)),
314                                         common_config('site', 'name'));
315
316     common_init_locale();
317         mail_to_user($other, $subject, $body);
318 }