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