mail.php 31 KB

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