]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/mail.php
Merge branch '1.0.x' of gitorious.org:statusnet/mainline into 1.0.x
[quix0rs-gnu-social.git] / lib / mail.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * utilities for sending email
6  *
7  * PHP version 5
8  *
9  * LICENCE: This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU Affero General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU Affero General Public License for more details.
18  *
19  * You should have received a copy of the GNU Affero General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  *
22  * @category  Mail
23  * @package   StatusNet
24  * @author    Evan Prodromou <evan@status.net>
25  * @author    Zach Copley <zach@status.net>
26  * @author    Robin Millette <millette@status.net>
27  * @author    Sarven Capadisli <csarven@status.net>
28  * @copyright 2008 StatusNet, Inc.
29  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
30  * @link      http://status.net/
31  */
32
33 if (!defined('STATUSNET') && !defined('LACONICA')) {
34     exit(1);
35 }
36
37 require_once 'Mail.php';
38
39 /**
40  * return the configured mail backend
41  *
42  * Uses the $config array to make a mail backend. Cached so it is safe to call
43  * more than once.
44  *
45  * @return Mail backend
46  */
47 function mail_backend()
48 {
49     static $backend = null;
50
51     if (!$backend) {
52         $backend = Mail::factory(common_config('mail', 'backend'),
53                                  (common_config('mail', 'params')) ?
54                                  common_config('mail', 'params') :
55                                  array());
56         if (PEAR::isError($backend)) {
57             common_server_error($backend->getMessage(), 500);
58         }
59     }
60     return $backend;
61 }
62
63 /**
64  * send an email to one or more recipients
65  *
66  * @param array  $recipients array of strings with email addresses of recipients
67  * @param array  $headers    array mapping strings to strings for email headers
68  * @param string $body       body of the email
69  *
70  * @return boolean success flag
71  */
72 function mail_send($recipients, $headers, $body)
73 {
74     // XXX: use Mail_Queue... maybe
75     $backend = mail_backend();
76     if (!isset($headers['Content-Type'])) {
77         $headers['Content-Type'] = 'text/plain; charset=UTF-8';
78     }
79     assert($backend); // throws an error if it's bad
80     $sent = $backend->send($recipients, $headers, $body);
81     if (PEAR::isError($sent)) {
82         common_log(LOG_ERR, 'Email error: ' . $sent->getMessage());
83         return false;
84     }
85     return true;
86 }
87
88 /**
89  * returns the configured mail domain
90  *
91  * Defaults to the server name.
92  *
93  * @return string mail domain, suitable for making email addresses.
94  */
95 function mail_domain()
96 {
97     $maildomain = common_config('mail', 'domain');
98     if (!$maildomain) {
99         $maildomain = common_config('site', 'server');
100     }
101     return $maildomain;
102 }
103
104 /**
105  * returns a good address for sending email from this server
106  *
107  * Uses either the configured value or a faked-up value made
108  * from the mail domain.
109  *
110  * @return string notify from address
111  */
112 function mail_notify_from()
113 {
114     $notifyfrom = common_config('mail', 'notifyfrom');
115
116     if (!$notifyfrom) {
117
118         $domain = mail_domain();
119
120         $notifyfrom = '"'. str_replace('"', '\\"', common_config('site', 'name')) .'" <noreply@'.$domain.'>';
121     }
122
123     return $notifyfrom;
124 }
125
126 /**
127  * sends email to a user
128  *
129  * @param User   &$user   user to send email to
130  * @param string $subject subject of the email
131  * @param string $body    body of the email
132  * @param array  $headers optional list of email headers
133  * @param string $address optional specification of email address
134  *
135  * @return boolean success flag
136  */
137 function mail_to_user(&$user, $subject, $body, $headers=array(), $address=null)
138 {
139     if (!$address) {
140         $address = $user->email;
141     }
142
143     $recipients = $address;
144     $profile    = $user->getProfile();
145
146     $headers['From']    = mail_notify_from();
147     $headers['To']      = $profile->getBestName() . ' <' . $address . '>';
148     $headers['Subject'] = $subject;
149
150     return mail_send($recipients, $headers, $body);
151 }
152
153 /**
154  * Send an email to confirm a user's control of an email address
155  *
156  * @param User   $user     User claiming the email address
157  * @param string $code     Confirmation code
158  * @param string $nickname Nickname of user
159  * @param string $address  email address to confirm
160  *
161  * @see common_confirmation_code()
162  *
163  * @return success flag
164  */
165 function mail_confirm_address($user, $code, $nickname, $address, $url=null)
166 {
167     if (empty($url)) {
168         $url = common_local_url('confirmaddress', array('code' => $code));
169     }
170
171     // TRANS: Subject for address confirmation email.
172     $subject = _('Email address confirmation');
173
174     // TRANS: Body for address confirmation email.
175     // TRANS: %1$s is the addressed user's nickname, %2$s is the StatusNet sitename,
176     // TRANS: %3$s is the URL to confirm at.
177     $body = sprintf(_("Hey, %1\$s.\n\n".
178                       "Someone just entered this email address on %2\$s.\n\n" .
179                       "If it was you, and you want to confirm your entry, ".
180                       "use the URL below:\n\n\t%3\$s\n\n" .
181                       "If not, just ignore this message.\n\n".
182                       "Thanks for your time, \n%2\$s\n"),
183                     $nickname,
184                     common_config('site', 'name'),
185                     $url);
186
187     $headers = array();
188
189     return mail_to_user($user, $subject, $body, $headers, $address);
190 }
191
192 /**
193  * notify a user of subscription by another user
194  *
195  * This is just a wrapper around the profile-based version.
196  *
197  * @param User $listenee user who is being subscribed to
198  * @param User $listener user who is subscribing
199  *
200  * @see mail_subscribe_notify_profile()
201  *
202  * @return void
203  */
204 function mail_subscribe_notify($listenee, $listener)
205 {
206     $other = $listener->getProfile();
207     mail_subscribe_notify_profile($listenee, $other);
208 }
209
210 /**
211  * notify a user of subscription by a profile (remote or local)
212  *
213  * This function checks to see if the listenee has an email
214  * address and wants subscription notices.
215  *
216  * @param User    $listenee user who's being subscribed to
217  * @param Profile $other    profile of person who's listening
218  *
219  * @return void
220  */
221 function mail_subscribe_notify_profile($listenee, $other)
222 {
223     if ($other->hasRight(Right::EMAILONSUBSCRIBE) &&
224         $listenee->email && $listenee->emailnotifysub) {
225
226         $profile = $listenee->getProfile();
227
228         $name = $profile->getBestName();
229
230         $long_name = ($other->fullname) ?
231           ($other->fullname . ' (' . $other->nickname . ')') : $other->nickname;
232
233         $recipients = $listenee->email;
234
235         // use the recipient's localization
236         common_switch_locale($listenee->language);
237
238         $headers = _mail_prepare_headers('subscribe', $listenee->nickname, $other->nickname);
239         $headers['From']    = mail_notify_from();
240         $headers['To']      = $name . ' <' . $listenee->email . '>';
241         // TRANS: Subject of new-subscriber notification e-mail.
242         // TRANS: %1$s is the subscribing user's nickname, %2$s is the StatusNet sitename.
243         $headers['Subject'] = sprintf(_('%1$s is now following you on %2$s.'),
244                                       $other->getBestName(),
245                                       common_config('site', 'name'));
246
247         // TRANS: Main body of new-subscriber notification e-mail.
248         // TRANS: %1$s is the subscriber's long name, %2$s is the StatusNet sitename.
249         $body = sprintf(_('%1$s is now following you on %2$s.'),
250                         $long_name,
251                         common_config('site', 'name')) .
252                 mail_profile_block($other) .
253                 mail_footer_block();
254
255         // reset localization
256         common_switch_locale();
257         mail_send($recipients, $headers, $body);
258     }
259 }
260
261 function mail_subscribe_pending_notify_profile($listenee, $other)
262 {
263     if ($other->hasRight(Right::EMAILONSUBSCRIBE) &&
264         $listenee->email && $listenee->emailnotifysub) {
265
266         $profile = $listenee->getProfile();
267
268         $name = $profile->getBestName();
269
270         $long_name = ($other->fullname) ?
271           ($other->fullname . ' (' . $other->nickname . ')') : $other->nickname;
272
273         $recipients = $listenee->email;
274
275         // use the recipient's localization
276         common_switch_locale($listenee->language);
277
278         $headers = _mail_prepare_headers('subscribe', $listenee->nickname, $other->nickname);
279         $headers['From']    = mail_notify_from();
280         $headers['To']      = $name . ' <' . $listenee->email . '>';
281         // TRANS: Subject of pending new-subscriber notification e-mail.
282         // TRANS: %1$s is the subscribing user's nickname, %2$s is the StatusNet sitename.
283         $headers['Subject'] = sprintf(_('%1$s would like to listen to '.
284                                         'your notices on %2$s.'),
285                                       $other->getBestName(),
286                                       common_config('site', 'name'));
287
288         // TRANS: Main body of pending new-subscriber notification e-mail.
289         // TRANS: %1$s is the subscriber's long name, %2$s is the StatusNet sitename.
290         $body = sprintf(_('%1$s would like to listen to your notices on %2$s. ' .
291                           'You may approve or reject their subscription at %3$s'),
292                         $long_name,
293                         common_config('site', 'name'),
294                         common_local_url('subqueue', array('nickname' => $listenee->nickname))) .
295                 mail_profile_block($other) .
296                 mail_footer_block();
297
298         // reset localization
299         common_switch_locale();
300         mail_send($recipients, $headers, $body);
301     }
302 }
303
304 function mail_footer_block()
305 {
306     // TRANS: Common footer block for StatusNet notification emails.
307     // TRANS: %1$s is the StatusNet sitename,
308     // TRANS: %2$s is a link to the addressed user's e-mail settings.
309     return "\n\n" . sprintf(_('Faithfully yours,'.
310                               "\n".'%1$s.'."\n\n".
311                               "----\n".
312                               "Change your email address or ".
313                               "notification options at ".'%2$s'),
314                             common_config('site', 'name'),
315                             common_local_url('emailsettings')) . "\n";
316 }
317
318 /**
319  * Format a block of profile info for a plaintext notification email.
320  *
321  * @param Profile $profile
322  * @return string
323  */
324 function mail_profile_block($profile)
325 {
326     // TRANS: Layout for
327     // TRANS: %1$s is the subscriber's profile URL, %2$s is the subscriber's location (or empty)
328     // TRANS: %3$s is the subscriber's homepage URL (or empty), %4%s is the subscriber's bio (or empty)
329     $out = array();
330     $out[] = "";
331     $out[] = "";
332     // TRANS: Profile info line in notification e-mail.
333     // TRANS: %s is a URL.
334     $out[] = sprintf(_("Profile: %s"), $profile->profileurl);
335     if ($profile->location) {
336         // TRANS: Profile info line in notification e-mail.
337         // TRANS: %s is a location.
338         $out[] = sprintf(_("Location: %s"), $profile->location);
339     }
340     if ($profile->homepage) {
341         // TRANS: Profile info line in notification e-mail.
342         // TRANS: %s is a homepage.
343         $out[] = sprintf(_("Homepage: %s"), $profile->homepage);
344     }
345     if ($profile->bio) {
346         // TRANS: Profile info line in notification e-mail.
347         // TRANS: %s is biographical information.
348         $out[] = sprintf(_("Bio: %s"), $profile->bio);
349     }
350
351     $blocklink = common_local_url('block', array('profileid' => $profile->id));
352     // This'll let ModPlus add the remote profile info so it's possible
353     // to block remote users directly...
354     Event::handle('MailProfileInfoBlockLink', array($profile, &$blocklink));
355
356     // TRANS: This is a paragraph in a new-subscriber e-mail.
357     // TRANS: %s is a URL where the subscriber can be reported as abusive.
358     $out[] = sprintf(_('If you believe this account is being used abusively, ' .
359                        'you can block them from your subscribers list and ' .
360                        'report as spam to site administrators at %s.'),
361                      $blocklink);
362     $out[] = "";
363
364     return implode("\n", $out);
365 }
366
367 /**
368  * notify a user of their new incoming email address
369  *
370  * User's email and incoming fields should already be updated.
371  *
372  * @param User $user user with the new address
373  *
374  * @return void
375  */
376 function mail_new_incoming_notify($user)
377 {
378     $profile = $user->getProfile();
379
380     $name = $profile->getBestName();
381
382     $headers['From']    = $user->incomingemail;
383     $headers['To']      = $name . ' <' . $user->email . '>';
384     // TRANS: Subject of notification mail for new posting email address.
385     // TRANS: %s is the StatusNet sitename.
386     $headers['Subject'] = sprintf(_('New email address for posting to %s'),
387                                   common_config('site', 'name'));
388
389     // TRANS: Body of notification mail for new posting email address.
390     // TRANS: %1$s is the StatusNet sitename, %2$s is the e-mail address to send
391     // TRANS: to to post by e-mail, %3$s is a URL to more instructions.
392     $body = sprintf(_("You have a new posting address on %1\$s.\n\n".
393                       "Send email to %2\$s to post new messages.\n\n".
394                       "More email instructions at %3\$s."),
395                     common_config('site', 'name'),
396                     $user->incomingemail,
397                     common_local_url('doc', array('title' => 'email'))) .
398             mail_footer_block();
399
400     mail_send($user->email, $headers, $body);
401 }
402
403 /**
404  * generate a new address for incoming messages
405  *
406  * @todo check the database for uniqueness
407  *
408  * @return string new email address for incoming messages
409  */
410 function mail_new_incoming_address()
411 {
412     $prefix = common_confirmation_code(64);
413     $suffix = mail_domain();
414     return $prefix . '@' . $suffix;
415 }
416
417 /**
418  * broadcast a notice to all subscribers with SMS notification on
419  *
420  * This function sends SMS messages to all users who have sms addresses;
421  * have sms notification on; and have sms enabled for this particular
422  * subscription.
423  *
424  * @param Notice $notice The notice to broadcast
425  *
426  * @return success flag
427  */
428 function mail_broadcast_notice_sms($notice)
429 {
430     // Now, get users subscribed to this profile
431
432     $user = new User();
433
434     $UT = common_config('db','type')=='pgsql'?'"user"':'user';
435     $user->query('SELECT nickname, smsemail, incomingemail ' .
436                  "FROM $UT JOIN subscription " .
437                  "ON $UT.id = subscription.subscriber " .
438                  'WHERE subscription.subscribed = ' . $notice->profile_id . ' ' .
439                  'AND subscription.subscribed != subscription.subscriber ' .
440                  "AND $UT.smsemail IS NOT null " .
441                  "AND $UT.smsnotify = 1 " .
442                  'AND subscription.sms = 1 ');
443
444     while ($user->fetch()) {
445         common_log(LOG_INFO,
446                    'Sending notice ' . $notice->id . ' to ' . $user->smsemail,
447                    __FILE__);
448         $success = mail_send_sms_notice_address($notice,
449                                                 $user->smsemail,
450                                                 $user->incomingemail);
451         if (!$success) {
452             // XXX: Not sure, but I think that's the right thing to do
453             common_log(LOG_WARNING,
454                        'Sending notice ' . $notice->id . ' to ' .
455                        $user->smsemail . ' FAILED, cancelling.',
456                        __FILE__);
457             return false;
458         }
459     }
460
461     $user->free();
462     unset($user);
463
464     return true;
465 }
466
467 /**
468  * send a notice to a user via SMS
469  *
470  * A convenience wrapper around mail_send_sms_notice_address()
471  *
472  * @param Notice $notice notice to send
473  * @param User   $user   user to receive notice
474  *
475  * @see mail_send_sms_notice_address()
476  *
477  * @return boolean success flag
478  */
479 function mail_send_sms_notice($notice, $user)
480 {
481     return mail_send_sms_notice_address($notice,
482                                         $user->smsemail,
483                                         $user->incomingemail);
484 }
485
486 /**
487  * send a notice to an SMS email address from a given address
488  *
489  * We use the user's incoming email address as the "From" address to make
490  * replying to notices easier.
491  *
492  * @param Notice $notice        notice to send
493  * @param string $smsemail      email address to send to
494  * @param string $incomingemail email address to set as 'from'
495  *
496  * @return boolean success flag
497  */
498 function mail_send_sms_notice_address($notice, $smsemail, $incomingemail)
499 {
500     $to = $nickname . ' <' . $smsemail . '>';
501
502     $other = $notice->getProfile();
503
504     common_log(LOG_INFO, 'Sending notice ' . $notice->id .
505                ' to ' . $smsemail, __FILE__);
506
507     $headers = array();
508
509     $headers['From']    = ($incomingemail) ? $incomingemail : mail_notify_from();
510     $headers['To']      = $to;
511     // TRANS: Subject line for SMS-by-email notification messages.
512     // TRANS: %s is the posting user's nickname.
513     $headers['Subject'] = sprintf(_('%s status'),
514                                   $other->getBestName());
515
516     $body = $notice->content;
517
518     return mail_send($smsemail, $headers, $body);
519 }
520
521 /**
522  * send a message to confirm a claim for an SMS number
523  *
524  * @param string $code     confirmation code
525  * @param string $nickname nickname of user claiming number
526  * @param string $address  email address to send the confirmation to
527  *
528  * @see common_confirmation_code()
529  *
530  * @return void
531  */
532 function mail_confirm_sms($code, $nickname, $address)
533 {
534     $recipients = $address;
535
536     $headers['From']    = mail_notify_from();
537     $headers['To']      = $nickname . ' <' . $address . '>';
538     // TRANS: Subject line for SMS-by-email address confirmation message.
539     $headers['Subject'] = _('SMS confirmation');
540
541     // TRANS: Main body heading for SMS-by-email address confirmation message.
542     // TRANS: %s is the addressed user's nickname.
543     $body  = sprintf(_('%s: confirm you own this phone number with this code:'), $nickname);
544     $body .= "\n\n";
545     $body .= $code;
546     $body .= "\n\n";
547
548     mail_send($recipients, $headers, $body);
549 }
550
551 /**
552  * send a mail message to notify a user of a 'nudge'
553  *
554  * @param User $from user nudging
555  * @param User $to   user being nudged
556  *
557  * @return boolean success flag
558  */
559 function mail_notify_nudge($from, $to)
560 {
561     common_switch_locale($to->language);
562     // TRANS: Subject for 'nudge' notification email.
563     // TRANS: %s is the nudging user.
564     $subject = sprintf(_('You have been nudged by %s'), $from->nickname);
565
566     $from_profile = $from->getProfile();
567
568     // TRANS: Body for 'nudge' notification email.
569     // TRANS: %1$s is the nuding user's long name, $2$s is the nudging user's nickname,
570     // TRANS: %3$s is a URL to post notices at.
571     $body = sprintf(_("%1\$s (%2\$s) is wondering what you are up to ".
572                       "these days and is inviting you to post some news.\n\n".
573                       "So let's hear from you :)\n\n".
574                       "%3\$s\n\n".
575                       "Don't reply to this email; it won't get to them."),
576                     $from_profile->getBestName(),
577                     $from->nickname,
578                     common_local_url('all', array('nickname' => $to->nickname))) .
579             mail_footer_block();
580     common_switch_locale();
581
582     $headers = _mail_prepare_headers('nudge', $to->nickname, $from->nickname);
583
584     return mail_to_user($to, $subject, $body, $headers);
585 }
586
587 /**
588  * send a message to notify a user of a direct message (DM)
589  *
590  * This function checks to see if the recipient wants notification
591  * of DMs and has a configured email address.
592  *
593  * @param Message $message message to notify about
594  * @param User    $from    user sending message; default to sender
595  * @param User    $to      user receiving message; default to recipient
596  *
597  * @return boolean success code
598  */
599 function mail_notify_message($message, $from=null, $to=null)
600 {
601     if (is_null($from)) {
602         $from = User::staticGet('id', $message->from_profile);
603     }
604
605     if (is_null($to)) {
606         $to = User::staticGet('id', $message->to_profile);
607     }
608
609     if (is_null($to->email) || !$to->emailnotifymsg) {
610         return true;
611     }
612
613     common_switch_locale($to->language);
614     // TRANS: Subject for direct-message notification email.
615     // TRANS: %s is the sending user's nickname.
616     $subject = sprintf(_('New private message from %s'), $from->nickname);
617
618     $from_profile = $from->getProfile();
619
620     // TRANS: Body for direct-message notification email.
621     // TRANS: %1$s is the sending user's long name, %2$s is the sending user's nickname,
622     // TRANS: %3$s is the message content, %4$s a URL to the message,
623     $body = sprintf(_("%1\$s (%2\$s) sent you a private message:\n\n".
624                       "------------------------------------------------------\n".
625                       "%3\$s\n".
626                       "------------------------------------------------------\n\n".
627                       "You can reply to their message here:\n\n".
628                       "%4\$s\n\n".
629                       "Don't reply to this email; it won't get to them."),
630                     $from_profile->getBestName(),
631                     $from->nickname,
632                     $message->content,
633                     common_local_url('newmessage', array('to' => $from->id))) .
634             mail_footer_block();
635
636     $headers = _mail_prepare_headers('message', $to->nickname, $from->nickname);
637
638     common_switch_locale();
639     return mail_to_user($to, $subject, $body, $headers);
640 }
641
642 /**
643  * Notify a user that one of their notices has been chosen as a 'fave'
644  *
645  * Doesn't check that the user has an email address nor if they
646  * want to receive notification of faves. Maybe this happens higher
647  * up the stack...?
648  *
649  * @param User   $other  The user whose notice was faved
650  * @param User   $user   The user who faved the notice
651  * @param Notice $notice The notice that was faved
652  *
653  * @return void
654  */
655 function mail_notify_fave($other, $user, $notice)
656 {
657     if (!$user->hasRight(Right::EMAILONFAVE)) {
658         return;
659     }
660
661     $profile = $user->getProfile();
662     if ($other->hasBlocked($profile)) {
663         // If the author has blocked us, don't spam them with a notification.
664         return;
665     }
666
667     $bestname = $profile->getBestName();
668
669     common_switch_locale($other->language);
670
671     // TRANS: Subject for favorite notification e-mail.
672     // TRANS: %1$s is the adding user's long name, %2$s is the adding user's nickname.
673     $subject = sprintf(_('%1$s (@%2$s) added your notice as a favorite'), $bestname, $user->nickname);
674
675     // TRANS: Body for favorite notification e-mail.
676     // TRANS: %1$s is the adding user's long name, $2$s is the date the notice was created,
677     // TRANS: %3$s is a URL to the faved notice, %4$s is the faved notice text,
678     // TRANS: %5$s is a URL to all faves of the adding user, %6$s is the StatusNet sitename,
679     // TRANS: %7$s is the adding user's nickname.
680     $body = sprintf(_("%1\$s (@%7\$s) just added your notice from %2\$s".
681                       " as one of their favorites.\n\n" .
682                       "The URL of your notice is:\n\n" .
683                       "%3\$s\n\n" .
684                       "The text of your notice is:\n\n" .
685                       "%4\$s\n\n" .
686                       "You can see the list of %1\$s's favorites here:\n\n" .
687                       "%5\$s"),
688                     $bestname,
689                     common_exact_date($notice->created),
690                     common_local_url('shownotice',
691                                      array('notice' => $notice->id)),
692                     $notice->content,
693                     common_local_url('showfavorites',
694                                      array('nickname' => $user->nickname)),
695                     common_config('site', 'name'),
696                     $user->nickname) .
697             mail_footer_block();
698
699     $headers = _mail_prepare_headers('fave', $other->nickname, $user->nickname);
700
701     common_switch_locale();
702     mail_to_user($other, $subject, $body, $headers);
703 }
704
705 /**
706  * Notify a user that they have received an "attn:" message AKA "@-reply"
707  *
708  * @param User   $user   The user who recevied the notice
709  * @param Notice $notice The notice that was sent
710  *
711  * @return void
712  */
713 function mail_notify_attn($user, $notice)
714 {
715     if (!$user->email || !$user->emailnotifyattn) {
716         return;
717     }
718
719     $sender = $notice->getProfile();
720
721     if ($sender->id == $user->id) {
722         return;
723     }
724
725     if (!$sender->hasRight(Right::EMAILONREPLY)) {
726         return;
727     }
728
729     $bestname = $sender->getBestName();
730
731     common_switch_locale($user->language);
732
733     if ($notice->hasConversation()) {
734         $conversationUrl = common_local_url('conversation',
735                          array('id' => $notice->conversation)).'#notice-'.$notice->id;
736         // TRANS: Line in @-reply notification e-mail. %s is conversation URL.
737         $conversationEmailText = sprintf(_("The full conversation can be read here:\n\n".
738                                            "\t%s"), $conversationUrl) . "\n\n";
739     } else {
740         $conversationEmailText = '';
741     }
742
743     // TRANS: E-mail subject for notice notification.
744     // TRANS: %1$s is the sending user's long name, %2$s is the adding user's nickname.
745     $subject = sprintf(_('%1$s (@%2$s) sent a notice to your attention'), $bestname, $sender->nickname);
746
747         // TRANS: Body of @-reply notification e-mail.
748         // TRANS: %1$s is the sending user's name, $2$s is the StatusNet sitename,
749         // TRANS: %3$s is a URL to the notice, %4$s is the notice text,
750         // TRANS: %5$s is a URL to the full conversion if it exists (otherwise empty),
751         // TRANS: %6$s is a URL to reply to the notice, %7$s is a URL to all @-replies for the addressed user,
752         $body = sprintf(_("%1\$s just sent a notice to your attention (an '@-reply') on %2\$s.\n\n".
753                       "The notice is here:\n\n".
754                       "\t%3\$s\n\n" .
755                       "It reads:\n\n".
756                       "\t%4\$s\n\n" .
757                       "%5\$s" .
758                       "You can reply back here:\n\n".
759                       "\t%6\$s\n\n" .
760                       "The list of all @-replies for you here:\n\n" .
761                       "%7\$s"),
762                     $sender->getFancyName(),//%1
763                     common_config('site', 'name'),//%2
764                     common_local_url('shownotice',
765                                      array('notice' => $notice->id)),//%3
766                     $notice->content,//%4
767                     $conversationEmailText,//%5
768                     common_local_url('newnotice',
769                                      array('replyto' => $sender->nickname, 'inreplyto' => $notice->id)),//%6
770                     common_local_url('replies',
771                                      array('nickname' => $user->nickname))) . //%7
772                 mail_footer_block();
773     $headers = _mail_prepare_headers('mention', $user->nickname, $sender->nickname);
774
775     common_switch_locale();
776     mail_to_user($user, $subject, $body, $headers);
777 }
778
779 /**
780  * Prepare the common mail headers used in notification emails
781  *
782  * @param string $msg_type type of message being sent to the user
783  * @param string $to       nickname of the receipient
784  * @param string $from     nickname of the user triggering the notification
785  *
786  * @return array list of mail headers to include in the message
787  */
788 function _mail_prepare_headers($msg_type, $to, $from)
789 {
790     $headers = array(
791         'X-StatusNet-MessageType' => $msg_type,
792         'X-StatusNet-TargetUser'  => $to,
793         'X-StatusNet-SourceUser'  => $from,
794         'X-StatusNet-Domain'      => common_config('site', 'server')
795     );
796
797     return $headers;
798 }
799
800 /**
801  * Send notification emails to group administrator.
802  *
803  * @param User_group $group
804  * @param Profile $joiner
805  */
806 function mail_notify_group_join($group, $joiner)
807 {
808     // This returns a Profile query...
809     $admin = $group->getAdmins();
810     while ($admin->fetch()) {
811         // We need a local user for email notifications...
812         $adminUser = User::staticGet('id', $admin->id);
813         // @fixme check for email preference?
814         if ($adminUser && $adminUser->email) {
815             // use the recipient's localization
816             common_switch_locale($adminUser->language);
817
818             $headers = _mail_prepare_headers('join', $admin->nickname, $joiner->nickname);
819             $headers['From']    = mail_notify_from();
820             $headers['To']      = $admin->getBestName() . ' <' . $adminUser->email . '>';
821             // TRANS: Subject of group join notification e-mail.
822             // TRANS: %1$s is the joining user's nickname, %2$s is the group name, and %3$s is the StatusNet sitename.
823             $headers['Subject'] = sprintf(_('%1$s has joined '.
824                                             'your group %2$s on %3$s.'),
825                                           $joiner->getBestName(),
826                                           $group->getBestName(),
827                                           common_config('site', 'name'));
828
829             // TRANS: Main body of group join notification e-mail.
830             // TRANS: %1$s is the subscriber's long name, %2$s is the group name, and %3$s is the StatusNet sitename,
831             // TRANS: %4$s is a block of profile info about the subscriber.
832             // TRANS: %5$s is a link to the addressed user's e-mail settings.
833             $body = sprintf(_('%1$s has joined your group %2$s on %3$s.'),
834                             $joiner->getFancyName(),
835                             $group->getFancyName(),
836                             common_config('site', 'name')) .
837                     mail_profile_block($joiner) .
838                     mail_footer_block();
839
840             // reset localization
841             common_switch_locale();
842             mail_send($adminUser->email, $headers, $body);
843         }
844     }
845 }
846
847
848 /**
849  * Send notification emails to group administrator.
850  *
851  * @param User_group $group
852  * @param Profile $joiner
853  */
854 function mail_notify_group_join_pending($group, $joiner)
855 {
856     $admin = $group->getAdmins();
857     while ($admin->fetch()) {
858         // We need a local user for email notifications...
859         $adminUser = User::staticGet('id', $admin->id);
860         // @fixme check for email preference?
861         if ($adminUser && $adminUser->email) {
862             // use the recipient's localization
863             common_switch_locale($adminUser->language);
864
865             $headers = _mail_prepare_headers('join', $admin->nickname, $joiner->nickname);
866             $headers['From']    = mail_notify_from();
867             $headers['To']      = $admin->getBestName() . ' <' . $adminUser->email . '>';
868             // TRANS: Subject of pending group join request notification e-mail.
869             // TRANS: %1$s is the joining user's nickname, %2$s is the group name, and %3$s is the StatusNet sitename.
870             $headers['Subject'] = sprintf(_('%1$s wants to join your group %2$s on %3$s.'),
871                                           $joiner->getBestName(),
872                                           $group->getBestName(),
873                                           common_config('site', 'name'));
874
875             // TRANS: Main body of pending group join request notification e-mail.
876             // TRANS: %1$s is the subscriber's long name, %2$s is the group name, and %3$s is the StatusNet sitename,
877             // TRANS: %4$s is the URL to the moderation queue page.
878             $body = sprintf(_('%1$s would like to join your group %2$s on %3$s. ' .
879                               'You may approve or reject their group membership at %4$s'),
880                             $joiner->getFancyName(),
881                             $group->getFancyName(),
882                             common_config('site', 'name'),
883                             common_local_url('groupqueue', array('nickname' => $group->nickname))) .
884                     mail_profile_block($joiner) .
885                     mail_footer_block();
886
887             // reset localization
888             common_switch_locale();
889             mail_send($adminUser->email, $headers, $body);
890         }
891     }
892 }