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