]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/mail.php
Refactoring on notification mail generation: common profile & footer chunks pulled...
[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 listening to '.
244                                         'your notices on %2$s.'),
245                                       $other->getBestName(),
246                                       common_config('site', 'name'));
247
248         // TRANS: Main body of new-subscriber notification e-mail.
249         // TRANS: %1$s is the subscriber's long name, %2$s is the StatusNet sitename.
250         $body = sprintf(_('%1$s is now listening to your notices on %2$s.'),
251                         $long_name,
252                         common_config('site', 'name')) .
253                 mail_profile_block($other) .
254                 mail_footer_block();
255
256         // reset localization
257         common_switch_locale();
258         mail_send($recipients, $headers, $body);
259     }
260 }
261
262 function mail_footer_block()
263 {
264     // TRANS: Common footer block for StatusNet notification emails.
265     // TRANS: %1$s is the StatusNet sitename,
266     // TRANS: %2$s is a link to the addressed user's e-mail settings.
267     return "\n\n" . sprintf(_('Faithfully yours,'.
268                               "\n".'%1$s.'."\n\n".
269                               "----\n".
270                               "Change your email address or ".
271                               "notification options at ".'%2$s'),
272                             common_config('site', 'name'),
273                             common_local_url('emailsettings')) . "\n";
274 }
275
276 /**
277  * Format a block of profile info for a plaintext notification email.
278  *
279  * @param Profile $profile
280  * @return string 
281  */
282 function mail_profile_block($profile)
283 {
284     // TRANS: Layout for
285     // TRANS: %1$s is the subscriber's profile URL, %2$s is the subscriber's location (or empty)
286     // TRANS: %3$s is the subscriber's homepage URL (or empty), %4%s is the subscriber's bio (or empty)
287     $out = array();
288     $out[] = "";
289     $out[] = "";
290     // TRANS: Profile info line in notification e-mail.
291     // TRANS: %s is a URL.
292     $out[] = sprintf(_("Profile: %s"), $profile->profileurl);
293     if ($profile->location) {
294         // TRANS: Profile info line in notification e-mail.
295         // TRANS: %s is a location.
296         $out[] = sprintf(_("Location: %s"), $profile->location);
297     }
298     if ($profile->homepage) {
299         // TRANS: Profile info line in notification e-mail.
300         // TRANS: %s is a homepage.
301         $out[] = sprintf(_("Homepage: %s"), $profile->homepage);
302     }
303     if ($profile->bio) {
304         // TRANS: Profile info line in notification e-mail.
305         // TRANS: %s is biographical information.
306         $out[] = sprintf(_("Bio: %s"), $profile->bio);
307     }
308
309     $blocklink = common_local_url('block', array('profileid' => $profile->id));
310     // This'll let ModPlus add the remote profile info so it's possible
311     // to block remote users directly...
312     Event::handle('MailProfileInfoBlockLink', array($profile, &$blocklink));
313
314     // TRANS: This is a paragraph in a new-subscriber e-mail.
315     // TRANS: %s is a URL where the subscriber can be reported as abusive.
316     $out[] = sprintf(_("If you believe this account is being used abusively, " .
317                        "you can block them from your subscribers list and " .
318                        "report as spam to site administrators at %s"),
319                      $blocklink);
320     $out[] = "";
321
322     return implode("\n", $out);
323 }
324
325 /**
326  * notify a user of their new incoming email address
327  *
328  * User's email and incoming fields should already be updated.
329  *
330  * @param User $user user with the new address
331  *
332  * @return void
333  */
334 function mail_new_incoming_notify($user)
335 {
336     $profile = $user->getProfile();
337
338     $name = $profile->getBestName();
339
340     $headers['From']    = $user->incomingemail;
341     $headers['To']      = $name . ' <' . $user->email . '>';
342     // TRANS: Subject of notification mail for new posting email address.
343     // TRANS: %s is the StatusNet sitename.
344     $headers['Subject'] = sprintf(_('New email address for posting to %s'),
345                                   common_config('site', 'name'));
346
347     // TRANS: Body of notification mail for new posting email address.
348     // TRANS: %1$s is the StatusNet sitename, %2$s is the e-mail address to send
349     // TRANS: to to post by e-mail, %3$s is a URL to more instructions.
350     $body = sprintf(_("You have a new posting address on %1\$s.\n\n".
351                       "Send email to %2\$s to post new messages.\n\n".
352                       "More email instructions at %3\$s."),
353                     common_config('site', 'name'),
354                     $user->incomingemail,
355                     common_local_url('doc', array('title' => 'email'))) .
356             mail_footer_block();
357
358     mail_send($user->email, $headers, $body);
359 }
360
361 /**
362  * generate a new address for incoming messages
363  *
364  * @todo check the database for uniqueness
365  *
366  * @return string new email address for incoming messages
367  */
368 function mail_new_incoming_address()
369 {
370     $prefix = common_confirmation_code(64);
371     $suffix = mail_domain();
372     return $prefix . '@' . $suffix;
373 }
374
375 /**
376  * broadcast a notice to all subscribers with SMS notification on
377  *
378  * This function sends SMS messages to all users who have sms addresses;
379  * have sms notification on; and have sms enabled for this particular
380  * subscription.
381  *
382  * @param Notice $notice The notice to broadcast
383  *
384  * @return success flag
385  */
386 function mail_broadcast_notice_sms($notice)
387 {
388     // Now, get users subscribed to this profile
389
390     $user = new User();
391
392     $UT = common_config('db','type')=='pgsql'?'"user"':'user';
393     $user->query('SELECT nickname, smsemail, incomingemail ' .
394                  "FROM $UT JOIN subscription " .
395                  "ON $UT.id = subscription.subscriber " .
396                  'WHERE subscription.subscribed = ' . $notice->profile_id . ' ' .
397                  'AND subscription.subscribed != subscription.subscriber ' .
398                  "AND $UT.smsemail IS NOT null " .
399                  "AND $UT.smsnotify = 1 " .
400                  'AND subscription.sms = 1 ');
401
402     while ($user->fetch()) {
403         common_log(LOG_INFO,
404                    'Sending notice ' . $notice->id . ' to ' . $user->smsemail,
405                    __FILE__);
406         $success = mail_send_sms_notice_address($notice,
407                                                 $user->smsemail,
408                                                 $user->incomingemail);
409         if (!$success) {
410             // XXX: Not sure, but I think that's the right thing to do
411             common_log(LOG_WARNING,
412                        'Sending notice ' . $notice->id . ' to ' .
413                        $user->smsemail . ' FAILED, cancelling.',
414                        __FILE__);
415             return false;
416         }
417     }
418
419     $user->free();
420     unset($user);
421
422     return true;
423 }
424
425 /**
426  * send a notice to a user via SMS
427  *
428  * A convenience wrapper around mail_send_sms_notice_address()
429  *
430  * @param Notice $notice notice to send
431  * @param User   $user   user to receive notice
432  *
433  * @see mail_send_sms_notice_address()
434  *
435  * @return boolean success flag
436  */
437 function mail_send_sms_notice($notice, $user)
438 {
439     return mail_send_sms_notice_address($notice,
440                                         $user->smsemail,
441                                         $user->incomingemail);
442 }
443
444 /**
445  * send a notice to an SMS email address from a given address
446  *
447  * We use the user's incoming email address as the "From" address to make
448  * replying to notices easier.
449  *
450  * @param Notice $notice        notice to send
451  * @param string $smsemail      email address to send to
452  * @param string $incomingemail email address to set as 'from'
453  *
454  * @return boolean success flag
455  */
456 function mail_send_sms_notice_address($notice, $smsemail, $incomingemail)
457 {
458     $to = $nickname . ' <' . $smsemail . '>';
459
460     $other = $notice->getProfile();
461
462     common_log(LOG_INFO, 'Sending notice ' . $notice->id .
463                ' to ' . $smsemail, __FILE__);
464
465     $headers = array();
466
467     $headers['From']    = ($incomingemail) ? $incomingemail : mail_notify_from();
468     $headers['To']      = $to;
469     // TRANS: Subject line for SMS-by-email notification messages.
470     // TRANS: %s is the posting user's nickname.
471     $headers['Subject'] = sprintf(_('%s status'),
472                                   $other->getBestName());
473
474     $body = $notice->content;
475
476     return mail_send($smsemail, $headers, $body);
477 }
478
479 /**
480  * send a message to confirm a claim for an SMS number
481  *
482  * @param string $code     confirmation code
483  * @param string $nickname nickname of user claiming number
484  * @param string $address  email address to send the confirmation to
485  *
486  * @see common_confirmation_code()
487  *
488  * @return void
489  */
490 function mail_confirm_sms($code, $nickname, $address)
491 {
492     $recipients = $address;
493
494     $headers['From']    = mail_notify_from();
495     $headers['To']      = $nickname . ' <' . $address . '>';
496     // TRANS: Subject line for SMS-by-email address confirmation message.
497     $headers['Subject'] = _('SMS confirmation');
498
499     // TRANS: Main body heading for SMS-by-email address confirmation message.
500     // TRANS: %s is the addressed user's nickname.
501     $body  = sprintf(_("%s: confirm you own this phone number with this code:"), $nickname);
502     $body .= "\n\n";
503     $body .= $code;
504     $body .= "\n\n";
505
506     mail_send($recipients, $headers, $body);
507 }
508
509 /**
510  * send a mail message to notify a user of a 'nudge'
511  *
512  * @param User $from user nudging
513  * @param User $to   user being nudged
514  *
515  * @return boolean success flag
516  */
517 function mail_notify_nudge($from, $to)
518 {
519     common_switch_locale($to->language);
520     // TRANS: Subject for 'nudge' notification email.
521     // TRANS: %s is the nudging user.
522     $subject = sprintf(_('You have been nudged by %s'), $from->nickname);
523
524     $from_profile = $from->getProfile();
525
526     // TRANS: Body for 'nudge' notification email.
527     // TRANS: %1$s is the nuding user's long name, $2$s is the nudging user's nickname,
528     // TRANS: %3$s is a URL to post notices at.
529     $body = sprintf(_("%1\$s (%2\$s) is wondering what you are up to ".
530                       "these days and is inviting you to post some news.\n\n".
531                       "So let's hear from you :)\n\n".
532                       "%3\$s\n\n".
533                       "Don't reply to this email; it won't get to them."),
534                     $from_profile->getBestName(),
535                     $from->nickname,
536                     common_local_url('all', array('nickname' => $to->nickname))) .
537             mail_footer_block();
538     common_switch_locale();
539
540     $headers = _mail_prepare_headers('nudge', $to->nickname, $from->nickname);
541
542     return mail_to_user($to, $subject, $body, $headers);
543 }
544
545 /**
546  * send a message to notify a user of a direct message (DM)
547  *
548  * This function checks to see if the recipient wants notification
549  * of DMs and has a configured email address.
550  *
551  * @param Message $message message to notify about
552  * @param User    $from    user sending message; default to sender
553  * @param User    $to      user receiving message; default to recipient
554  *
555  * @return boolean success code
556  */
557 function mail_notify_message($message, $from=null, $to=null)
558 {
559     if (is_null($from)) {
560         $from = User::staticGet('id', $message->from_profile);
561     }
562
563     if (is_null($to)) {
564         $to = User::staticGet('id', $message->to_profile);
565     }
566
567     if (is_null($to->email) || !$to->emailnotifymsg) {
568         return true;
569     }
570
571     common_switch_locale($to->language);
572     // TRANS: Subject for direct-message notification email.
573     // TRANS: %s is the sending user's nickname.
574     $subject = sprintf(_('New private message from %s'), $from->nickname);
575
576     $from_profile = $from->getProfile();
577
578     // TRANS: Body for direct-message notification email.
579     // TRANS: %1$s is the sending user's long name, %2$s is the sending user's nickname,
580     // TRANS: %3$s is the message content, %4$s a URL to the message,
581     $body = sprintf(_("%1\$s (%2\$s) sent you a private message:\n\n".
582                       "------------------------------------------------------\n".
583                       "%3\$s\n".
584                       "------------------------------------------------------\n\n".
585                       "You can reply to their message here:\n\n".
586                       "%4\$s\n\n".
587                       "Don't reply to this email; it won't get to them."),
588                     $from_profile->getBestName(),
589                     $from->nickname,
590                     $message->content,
591                     common_local_url('newmessage', array('to' => $from->id))) .
592             mail_footer_block();
593
594     $headers = _mail_prepare_headers('message', $to->nickname, $from->nickname);
595
596     common_switch_locale();
597     return mail_to_user($to, $subject, $body, $headers);
598 }
599
600 /**
601  * notify a user that one of their notices has been chosen as a 'fave'
602  *
603  * Doesn't check that the user has an email address nor if they
604  * want to receive notification of faves. Maybe this happens higher
605  * up the stack...?
606  *
607  * @param User   $other  The user whose notice was faved
608  * @param User   $user   The user who faved the notice
609  * @param Notice $notice The notice that was faved
610  *
611  * @return void
612  */
613 function mail_notify_fave($other, $user, $notice)
614 {
615     if (!$user->hasRight(Right::EMAILONFAVE)) {
616         return;
617     }
618
619     $profile = $user->getProfile();
620     if ($other->hasBlocked($profile)) {
621         // If the author has blocked us, don't spam them with a notification.
622         return;
623     }
624
625     $bestname = $profile->getBestName();
626
627     common_switch_locale($other->language);
628
629     // TRANS: Subject for favorite notification e-mail.
630     // TRANS: %1$s is the adding user's long name, %2$s is the adding user's nickname.
631     $subject = sprintf(_('%1$s (@%2$s) added your notice as a favorite'), $bestname, $user->nickname);
632
633     // TRANS: Body for favorite notification e-mail.
634     // TRANS: %1$s is the adding user's long name, $2$s is the date the notice was created,
635     // TRANS: %3$s is a URL to the faved notice, %4$s is the faved notice text,
636     // TRANS: %5$s is a URL to all faves of the adding user, %6$s is the StatusNet sitename,
637     // TRANS: %7$s is the adding user's nickname.
638     $body = sprintf(_("%1\$s (@%7\$s) just added your notice from %2\$s".
639                       " as one of their favorites.\n\n" .
640                       "The URL of your notice is:\n\n" .
641                       "%3\$s\n\n" .
642                       "The text of your notice is:\n\n" .
643                       "%4\$s\n\n" .
644                       "You can see the list of %1\$s's favorites here:\n\n" .
645                       "%5\$s"),
646                     $bestname,
647                     common_exact_date($notice->created),
648                     common_local_url('shownotice',
649                                      array('notice' => $notice->id)),
650                     $notice->content,
651                     common_local_url('showfavorites',
652                                      array('nickname' => $user->nickname)),
653                     common_config('site', 'name'),
654                     $user->nickname) .
655             mail_footer_block();
656
657     $headers = _mail_prepare_headers('fave', $other->nickname, $user->nickname);
658
659     common_switch_locale();
660     mail_to_user($other, $subject, $body, $headers);
661 }
662
663 /**
664  * notify a user that they have received an "attn:" message AKA "@-reply"
665  *
666  * @param User   $user   The user who recevied the notice
667  * @param Notice $notice The notice that was sent
668  *
669  * @return void
670  */
671 function mail_notify_attn($user, $notice)
672 {
673     if (!$user->email || !$user->emailnotifyattn) {
674         return;
675     }
676
677     $sender = $notice->getProfile();
678
679     if ($sender->id == $user->id) {
680         return;
681     }
682
683     if (!$sender->hasRight(Right::EMAILONREPLY)) {
684         return;
685     }
686
687     $bestname = $sender->getBestName();
688
689     common_switch_locale($user->language);
690
691     if ($notice->hasConversation()) {
692         $conversationUrl = common_local_url('conversation',
693                          array('id' => $notice->conversation)).'#notice-'.$notice->id;
694         // TRANS: Line in @-reply notification e-mail. %s is conversation URL.
695         $conversationEmailText = sprintf(_("The full conversation can be read here:\n\n".
696                                            "\t%s"), $conversationUrl) . "\n\n";
697     } else {
698         $conversationEmailText = '';
699     }
700
701     // TRANS: E-mail subject for notice notification.
702     // TRANS: %1$s is the sending user's long name, %2$s is the adding user's nickname.
703     $subject = sprintf(_('%1$s (@%2$s) sent a notice to your attention'), $bestname, $sender->nickname);
704
705         // TRANS: Body of @-reply notification e-mail.
706         // TRANS: %1$s is the sending user's name, $2$s is the StatusNet sitename,
707         // TRANS: %3$s is a URL to the notice, %4$s is the notice text,
708         // TRANS: %5$s is a URL to the full conversion if it exists (otherwise empty),
709         // TRANS: %6$s is a URL to reply to the notice, %7$s is a URL to all @-replies for the addressed user,
710         $body = sprintf(_("%1\$s just sent a notice to your attention (an '@-reply') on %2\$s.\n\n".
711                       "The notice is here:\n\n".
712                       "\t%3\$s\n\n" .
713                       "It reads:\n\n".
714                       "\t%4\$s\n\n" .
715                       "%5\$s" .
716                       "You can reply back here:\n\n".
717                       "\t%6\$s\n\n" .
718                       "The list of all @-replies for you here:\n\n" .
719                       "%7\$s"),
720                     $sender->getFancyName(),//%1
721                     common_config('site', 'name'),//%2
722                     common_local_url('shownotice',
723                                      array('notice' => $notice->id)),//%3
724                     $notice->content,//%4
725                     $conversationEmailText,//%5
726                     common_local_url('newnotice',
727                                      array('replyto' => $sender->nickname, 'inreplyto' => $notice->id)),//%6
728                     common_local_url('replies',
729                                      array('nickname' => $user->nickname))) . //%7
730                 mail_footer_block();
731     $headers = _mail_prepare_headers('mention', $user->nickname, $sender->nickname);
732
733     common_switch_locale();
734     mail_to_user($user, $subject, $body, $headers);
735 }
736
737 /**
738  * Prepare the common mail headers used in notification emails
739  *
740  * @param string $msg_type type of message being sent to the user
741  * @param string $to       nickname of the receipient
742  * @param string $from     nickname of the user triggering the notification
743  *
744  * @return array list of mail headers to include in the message
745  */
746 function _mail_prepare_headers($msg_type, $to, $from)
747 {
748     $headers = array(
749         'X-StatusNet-MessageType' => $msg_type,
750         'X-StatusNet-TargetUser'  => $to,
751         'X-StatusNet-SourceUser'  => $from,
752         'X-StatusNet-Domain'      => common_config('site', 'server')
753     );
754
755     return $headers;
756 }
757
758 /**
759  * Send notification emails to group administrator.
760  *
761  * @param User_group $group
762  * @param Profile $joiner
763  */
764 function mail_notify_group_join($group, $joiner)
765 {
766     // This returns a Profile query...
767     $admin = $group->getAdmins();
768     while ($admin->fetch()) {
769         // We need a local user for email notifications...
770         $adminUser = User::staticGet('id', $admin->id);
771         // @fixme check for email preference?
772         if ($adminUser && $adminUser->email) {
773             // use the recipient's localization
774             common_switch_locale($adminUser->language);
775
776             $headers = _mail_prepare_headers('join', $admin->nickname, $joiner->nickname);
777             $headers['From']    = mail_notify_from();
778             $headers['To']      = $admin->getBestName() . ' <' . $adminUser->email . '>';
779             // TRANS: Subject of group join notification e-mail.
780             // TRANS: %1$s is the joining user's nickname, %2$s is the group name, and %3$s is the StatusNet sitename.
781             $headers['Subject'] = sprintf(_('%1$s has joined '.
782                                             'your group %2$s on %3$s.'),
783                                           $joiner->getBestName(),
784                                           $group->getBestName(),
785                                           common_config('site', 'name'));
786
787             // TRANS: Main body of group join notification e-mail.
788             // TRANS: %1$s is the subscriber's long name, %2$s is the group name, and %3$s is the StatusNet sitename,
789             // TRANS: %4$s is a block of profile info about the subscriber.
790             // TRANS: %5$s is a link to the addressed user's e-mail settings.
791             $body = sprintf(_('%1$s has joined your group %2$s on %3$s.'),
792                             $joiner->getFancyName(),
793                             $group->getFancyName(),
794                             common_config('site', 'name')) .
795                     mail_profile_block($joiner) .
796                     mail_footer_block();
797
798             // reset localization
799             common_switch_locale();
800             mail_send($adminUser->email, $headers, $body);
801         }
802     }
803 }
804
805
806 /**
807  * Send notification emails to group administrator.
808  *
809  * @param User_group $group
810  * @param Profile $joiner
811  */
812 function mail_notify_group_join_pending($group, $joiner)
813 {
814     $admin = $group->getAdmins();
815     while ($admin->fetch()) {
816         // We need a local user for email notifications...
817         $adminUser = User::staticGet('id', $admin->id);
818         // @fixme check for email preference?
819         if ($adminUser && $adminUser->email) {
820             // use the recipient's localization
821             common_switch_locale($adminUser->language);
822
823             $headers = _mail_prepare_headers('join', $admin->nickname, $joiner->nickname);
824             $headers['From']    = mail_notify_from();
825             $headers['To']      = $admin->getBestName() . ' <' . $adminUser->email . '>';
826             // TRANS: Subject of pending group join request notification e-mail.
827             // TRANS: %1$s is the joining user's nickname, %2$s is the group name, and %3$s is the StatusNet sitename.
828             $headers['Subject'] = sprintf(_('%1$s wants to join your group %2$s on %3$s.'),
829                                           $joiner->getBestName(),
830                                           $group->getBestName(),
831                                           common_config('site', 'name'));
832
833             // TRANS: Main body of pending group join request notification e-mail.
834             // TRANS: %1$s is the subscriber's long name, %2$s is the group name, and %3$s is the StatusNet sitename,
835             // TRANS: %3$s is the URL to the moderation queue page.
836             $body = sprintf(_('%1$s would like to join your group %2$s on %3$s. ' .
837                               'You may approve or reject their group membership at %4$s'),
838                             $joiner->getFancyName(),
839                             $group->getFancyName(),
840                             common_config('site', 'name'),
841                             common_local_url('groupqueue', array('nickname' => $group->nickname))) .
842                     mail_profile_block($joiner) .
843                     mail_footer_block();
844
845             // reset localization
846             common_switch_locale();
847             mail_send($adminUser->email, $headers, $body);
848         }
849     }
850 }