mail.php 30 KB

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