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