mail.php 29 KB

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