3 * StatusNet, the distributed open-source microblogging tool
5 * utilities for sending email
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.
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.
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/>.
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/
33 if (!defined('STATUSNET') && !defined('LACONICA')) {
37 require_once 'Mail.php';
40 * return the configured mail backend
42 * Uses the $config array to make a mail backend. Cached so it is safe to call
45 * @return Mail backend
48 function mail_backend()
50 static $backend = null;
53 $backend = Mail::factory(common_config('mail', 'backend'),
54 (common_config('mail', 'params')) ?
55 common_config('mail', 'params') :
57 if (PEAR::isError($backend)) {
58 common_server_error($backend->getMessage(), 500);
65 * send an email to one or more recipients
67 * @param array $recipients array of strings with email addresses of recipients
68 * @param array $headers array mapping strings to strings for email headers
69 * @param string $body body of the email
71 * @return boolean success flag
74 function mail_send($recipients, $headers, $body)
76 // XXX: use Mail_Queue... maybe
77 $backend = mail_backend();
78 if (!isset($headers['Content-Type'])) {
79 $headers['Content-Type'] = 'text/plain; charset=UTF-8';
81 assert($backend); // throws an error if it's bad
82 $sent = $backend->send($recipients, $headers, $body);
83 if (PEAR::isError($sent)) {
84 common_log(LOG_ERR, 'Email error: ' . $sent->getMessage());
91 * returns the configured mail domain
93 * Defaults to the server name.
95 * @return string mail domain, suitable for making email addresses.
98 function mail_domain()
100 $maildomain = common_config('mail', 'domain');
102 $maildomain = common_config('site', 'server');
108 * returns a good address for sending email from this server
110 * Uses either the configured value or a faked-up value made
111 * from the mail domain.
113 * @return string notify from address
116 function mail_notify_from()
118 $notifyfrom = common_config('mail', 'notifyfrom');
122 $domain = mail_domain();
124 $notifyfrom = '"'.common_config('site', 'name') .'" <noreply@'.$domain.'>';
131 * sends email to a user
133 * @param User &$user user to send email to
134 * @param string $subject subject of the email
135 * @param string $body body of the email
136 * @param string $address optional specification of email address
138 * @return boolean success flag
141 function mail_to_user(&$user, $subject, $body, $address=null)
144 $address = $user->email;
147 $recipients = $address;
148 $profile = $user->getProfile();
150 $headers['From'] = mail_notify_from();
151 $headers['To'] = $profile->getBestName() . ' <' . $address . '>';
152 $headers['Subject'] = $subject;
154 return mail_send($recipients, $headers, $body);
158 * Send an email to confirm a user's control of an email address
160 * @param User $user User claiming the email address
161 * @param string $code Confirmation code
162 * @param string $nickname Nickname of user
163 * @param string $address email address to confirm
165 * @see common_confirmation_code()
167 * @return success flag
170 function mail_confirm_address($user, $code, $nickname, $address)
172 $subject = _('Email address confirmation');
174 $body = sprintf(_("Hey, %s.\n\n".
175 "Someone just entered this email address on %s.\n\n" .
176 "If it was you, and you want to confirm your entry, ".
177 "use the URL below:\n\n\t%s\n\n" .
178 "If not, just ignore this message.\n\n".
179 "Thanks for your time, \n%s\n"),
180 $nickname, common_config('site', 'name'),
181 common_local_url('confirmaddress', array('code' => $code)),
182 common_config('site', 'name'));
183 return mail_to_user($user, $subject, $body, $address);
187 * notify a user of subscription by another user
189 * This is just a wrapper around the profile-based version.
191 * @param User $listenee user who is being subscribed to
192 * @param User $listener user who is subscribing
194 * @see mail_subscribe_notify_profile()
199 function mail_subscribe_notify($listenee, $listener)
201 $other = $listener->getProfile();
202 mail_subscribe_notify_profile($listenee, $other);
206 * notify a user of subscription by a profile (remote or local)
208 * This function checks to see if the listenee has an email
209 * address and wants subscription notices.
211 * @param User $listenee user who's being subscribed to
212 * @param Profile $other profile of person who's listening
217 function mail_subscribe_notify_profile($listenee, $other)
219 if ($other->hasRight(Right::EMAILONSUBSCRIBE) &&
220 $listenee->email && $listenee->emailnotifysub) {
222 // use the recipient's localization
223 common_init_locale($listenee->language);
225 $profile = $listenee->getProfile();
227 $name = $profile->getBestName();
229 $long_name = ($other->fullname) ?
230 ($other->fullname . ' (' . $other->nickname . ')') : $other->nickname;
232 $recipients = $listenee->email;
234 $headers['From'] = mail_notify_from();
235 $headers['To'] = $name . ' <' . $listenee->email . '>';
236 $headers['Subject'] = sprintf(_('%1$s is now listening to '.
237 'your notices on %2$s.'),
238 $other->getBestName(),
239 common_config('site', 'name'));
241 $body = sprintf(_('%1$s is now listening to your notices on %2$s.'."\n\n".
246 "\n".'Faithfully yours,'."\n".'%7$s.'."\n\n".
248 "Change your email address or ".
249 "notification options at ".'%8$s' ."\n"),
251 common_config('site', 'name'),
254 sprintf(_("Location: %s\n"), $other->location) : '',
256 sprintf(_("Homepage: %s\n"), $other->homepage) : '',
258 sprintf(_("Bio: %s\n\n"), $other->bio) : '',
259 common_config('site', 'name'),
260 common_local_url('emailsettings'));
262 // reset localization
263 common_init_locale();
264 mail_send($recipients, $headers, $body);
269 * notify a user of their new incoming email address
271 * User's email and incoming fields should already be updated.
273 * @param User $user user with the new address
278 function mail_new_incoming_notify($user)
280 $profile = $user->getProfile();
282 $name = $profile->getBestName();
284 $headers['From'] = $user->incomingemail;
285 $headers['To'] = $name . ' <' . $user->email . '>';
286 $headers['Subject'] = sprintf(_('New email address for posting to %s'),
287 common_config('site', 'name'));
289 $body = sprintf(_("You have a new posting address on %1\$s.\n\n".
290 "Send email to %2\$s to post new messages.\n\n".
291 "More email instructions at %3\$s.\n\n".
292 "Faithfully yours,\n%4\$s"),
293 common_config('site', 'name'),
294 $user->incomingemail,
295 common_local_url('doc', array('title' => 'email')),
296 common_config('site', 'name'));
298 mail_send($user->email, $headers, $body);
302 * generate a new address for incoming messages
304 * @todo check the database for uniqueness
306 * @return string new email address for incoming messages
309 function mail_new_incoming_address()
311 $prefix = common_confirmation_code(64);
312 $suffix = mail_domain();
313 return $prefix . '@' . $suffix;
317 * broadcast a notice to all subscribers with SMS notification on
319 * This function sends SMS messages to all users who have sms addresses;
320 * have sms notification on; and have sms enabled for this particular
323 * @param Notice $notice The notice to broadcast
325 * @return success flag
328 function mail_broadcast_notice_sms($notice)
330 // Now, get users subscribed to this profile
334 $UT = common_config('db','type')=='pgsql'?'"user"':'user';
335 $user->query('SELECT nickname, smsemail, incomingemail ' .
336 "FROM $UT JOIN subscription " .
337 "ON $UT.id = subscription.subscriber " .
338 'WHERE subscription.subscribed = ' . $notice->profile_id . ' ' .
339 'AND subscription.subscribed != subscription.subscriber ' .
340 "AND $UT.smsemail IS NOT null " .
341 "AND $UT.smsnotify = 1 " .
342 'AND subscription.sms = 1 ');
344 while ($user->fetch()) {
346 'Sending notice ' . $notice->id . ' to ' . $user->smsemail,
348 $success = mail_send_sms_notice_address($notice,
350 $user->incomingemail);
352 // XXX: Not sure, but I think that's the right thing to do
353 common_log(LOG_WARNING,
354 'Sending notice ' . $notice->id . ' to ' .
355 $user->smsemail . ' FAILED, cancelling.',
368 * send a notice to a user via SMS
370 * A convenience wrapper around mail_send_sms_notice_address()
372 * @param Notice $notice notice to send
373 * @param User $user user to receive notice
375 * @see mail_send_sms_notice_address()
377 * @return boolean success flag
380 function mail_send_sms_notice($notice, $user)
382 return mail_send_sms_notice_address($notice,
384 $user->incomingemail);
388 * send a notice to an SMS email address from a given address
390 * We use the user's incoming email address as the "From" address to make
391 * replying to notices easier.
393 * @param Notice $notice notice to send
394 * @param string $smsemail email address to send to
395 * @param string $incomingemail email address to set as 'from'
397 * @return boolean success flag
400 function mail_send_sms_notice_address($notice, $smsemail, $incomingemail)
402 $to = $nickname . ' <' . $smsemail . '>';
404 $other = $notice->getProfile();
406 common_log(LOG_INFO, 'Sending notice ' . $notice->id .
407 ' to ' . $smsemail, __FILE__);
411 $headers['From'] = ($incomingemail) ? $incomingemail : mail_notify_from();
412 $headers['To'] = $to;
413 $headers['Subject'] = sprintf(_('%s status'),
414 $other->getBestName());
416 $body = $notice->content;
418 return mail_send($smsemail, $headers, $body);
422 * send a message to confirm a claim for an SMS number
424 * @param string $code confirmation code
425 * @param string $nickname nickname of user claiming number
426 * @param string $address email address to send the confirmation to
428 * @see common_confirmation_code()
433 function mail_confirm_sms($code, $nickname, $address)
435 $recipients = $address;
437 $headers['From'] = mail_notify_from();
438 $headers['To'] = $nickname . ' <' . $address . '>';
439 $headers['Subject'] = _('SMS confirmation');
443 $body = "$nickname: confirm you own this phone number with this code:";
448 mail_send($recipients, $headers, $body);
452 * send a mail message to notify a user of a 'nudge'
454 * @param User $from user nudging
455 * @param User $to user being nudged
457 * @return boolean success flag
460 function mail_notify_nudge($from, $to)
462 common_init_locale($to->language);
463 $subject = sprintf(_('You\'ve been nudged by %s'), $from->nickname);
465 $from_profile = $from->getProfile();
467 $body = sprintf(_("%1\$s (%2\$s) is wondering what you are up to ".
468 "these days and is inviting you to post some news.\n\n".
469 "So let's hear from you :)\n\n".
471 "Don't reply to this email; it won't get to them.\n\n".
472 "With kind regards,\n".
474 $from_profile->getBestName(),
476 common_local_url('all', array('nickname' => $to->nickname)),
477 common_config('site', 'name'));
478 common_init_locale();
479 return mail_to_user($to, $subject, $body);
483 * send a message to notify a user of a direct message (DM)
485 * This function checks to see if the recipient wants notification
486 * of DMs and has a configured email address.
488 * @param Message $message message to notify about
489 * @param User $from user sending message; default to sender
490 * @param User $to user receiving message; default to recipient
492 * @return boolean success code
495 function mail_notify_message($message, $from=null, $to=null)
497 if (is_null($from)) {
498 $from = User::staticGet('id', $message->from_profile);
502 $to = User::staticGet('id', $message->to_profile);
505 if (is_null($to->email) || !$to->emailnotifymsg) {
509 common_init_locale($to->language);
510 $subject = sprintf(_('New private message from %s'), $from->nickname);
512 $from_profile = $from->getProfile();
514 $body = sprintf(_("%1\$s (%2\$s) sent you a private message:\n\n".
515 "------------------------------------------------------\n".
517 "------------------------------------------------------\n\n".
518 "You can reply to their message here:\n\n".
520 "Don't reply to this email; it won't get to them.\n\n".
521 "With kind regards,\n".
523 $from_profile->getBestName(),
526 common_local_url('newmessage', array('to' => $from->id)),
527 common_config('site', 'name'));
529 common_init_locale();
530 return mail_to_user($to, $subject, $body);
534 * notify a user that one of their notices has been chosen as a 'fave'
536 * Doesn't check that the user has an email address nor if they
537 * want to receive notification of faves. Maybe this happens higher
540 * @param User $other The user whose notice was faved
541 * @param User $user The user who faved the notice
542 * @param Notice $notice The notice that was faved
547 function mail_notify_fave($other, $user, $notice)
549 if (!$user->hasRight(Right::EMAILONFAVE)) {
553 $profile = $user->getProfile();
555 $bestname = $profile->getBestName();
557 common_init_locale($other->language);
559 $subject = sprintf(_('%s (@%s) added your notice as a favorite'), $bestname, $user->nickname);
561 $body = sprintf(_("%1\$s (@%7\$s) just added your notice from %2\$s".
562 " as one of their favorites.\n\n" .
563 "The URL of your notice is:\n\n" .
565 "The text of your notice is:\n\n" .
567 "You can see the list of %1\$s's favorites here:\n\n" .
569 "Faithfully yours,\n" .
572 common_exact_date($notice->created),
573 common_local_url('shownotice',
574 array('notice' => $notice->id)),
576 common_local_url('showfavorites',
577 array('nickname' => $user->nickname)),
578 common_config('site', 'name'),
581 common_init_locale();
582 mail_to_user($other, $subject, $body);
586 * notify a user that they have received an "attn:" message AKA "@-reply"
588 * @param User $user The user who recevied the notice
589 * @param Notice $notice The notice that was sent
594 function mail_notify_attn($user, $notice)
596 if (!$user->email || !$user->emailnotifyattn) {
600 $sender = $notice->getProfile();
602 if (!$sender->hasRight(Right::EMAILONREPLY)) {
606 $bestname = $sender->getBestName();
608 common_init_locale($user->language);
610 if ($notice->conversation != $notice->id) {
611 $conversationEmailText = "The full conversation can be read here:\n\n".
613 $conversationUrl = common_local_url('conversation',
614 array('id' => $notice->conversation)).'#notice-'.$notice->id;
616 $conversationEmailText = "%5\$s";
617 $conversationUrl = null;
620 $subject = sprintf(_('%s (@%s) sent a notice to your attention'), $bestname, $sender->nickname);
622 $body = sprintf(_("%1\$s (@%9\$s) just sent a notice to your attention (an '@-reply') on %2\$s.\n\n".
623 "The notice is here:\n\n".
627 $conversationEmailText .
628 "You can reply back here:\n\n".
630 "The list of all @-replies for you here:\n\n" .
632 "Faithfully yours,\n" .
634 "P.S. You can turn off these email notifications here: %8\$s\n"),
636 common_config('site', 'name'),//%2
637 common_local_url('shownotice',
638 array('notice' => $notice->id)),//%3
639 $notice->content,//%4
640 $conversationUrl,//%5
641 common_local_url('newnotice',
642 array('replyto' => $sender->nickname, 'inreplyto' => $notice->id)),//%6
643 common_local_url('replies',
644 array('nickname' => $user->nickname)),//%7
645 common_local_url('emailsettings'), //%8
646 $sender->nickname); //%9
648 common_init_locale();
649 mail_to_user($user, $subject, $body);