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@controlyourself.ca>
25 * @author Zach Copley <zach@controlyourself.ca>
26 * @author Robin Millette <millette@controlyourself.ca>
27 * @author Sarven Capadisli <csarven@controlyourself.ca>
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('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 ($listenee->email && $listenee->emailnotifysub) {
221 // use the recipient's localization
222 common_init_locale($listenee->language);
224 $profile = $listenee->getProfile();
226 $name = $profile->getBestName();
228 $long_name = ($other->fullname) ?
229 ($other->fullname . ' (' . $other->nickname . ')') : $other->nickname;
231 $recipients = $listenee->email;
233 $headers['From'] = mail_notify_from();
234 $headers['To'] = $name . ' <' . $listenee->email . '>';
235 $headers['Subject'] = sprintf(_('%1$s is now listening to '.
236 'your notices on %2$s.'),
237 $other->getBestName(),
238 common_config('site', 'name'));
240 $body = sprintf(_('%1$s is now listening to your notices on %2$s.'."\n\n".
245 "\n".'Faithfully yours,'."\n".'%7$s.'."\n\n".
247 "Change your email address or ".
248 "notification options at ".'%8$s' ."\n"),
250 common_config('site', 'name'),
253 sprintf(_("Location: %s\n"), $other->location) : '',
255 sprintf(_("Homepage: %s\n"), $other->homepage) : '',
257 sprintf(_("Bio: %s\n\n"), $other->bio) : '',
258 common_config('site', 'name'),
259 common_local_url('emailsettings'));
261 // reset localization
262 common_init_locale();
263 mail_send($recipients, $headers, $body);
268 * notify a user of their new incoming email address
270 * User's email and incoming fields should already be updated.
272 * @param User $user user with the new address
277 function mail_new_incoming_notify($user)
279 $profile = $user->getProfile();
281 $name = $profile->getBestName();
283 $headers['From'] = $user->incomingemail;
284 $headers['To'] = $name . ' <' . $user->email . '>';
285 $headers['Subject'] = sprintf(_('New email address for posting to %s'),
286 common_config('site', 'name'));
288 $body = sprintf(_("You have a new posting address on %1\$s.\n\n".
289 "Send email to %2\$s to post new messages.\n\n".
290 "More email instructions at %3\$s.\n\n".
291 "Faithfully yours,\n%4\$s"),
292 common_config('site', 'name'),
293 $user->incomingemail,
294 common_local_url('doc', array('title' => 'email')),
295 common_config('site', 'name'));
297 mail_send($user->email, $headers, $body);
301 * generate a new address for incoming messages
303 * @todo check the database for uniqueness
305 * @return string new email address for incoming messages
308 function mail_new_incoming_address()
310 $prefix = common_confirmation_code(64);
311 $suffix = mail_domain();
312 return $prefix . '@' . $suffix;
316 * broadcast a notice to all subscribers with SMS notification on
318 * This function sends SMS messages to all users who have sms addresses;
319 * have sms notification on; and have sms enabled for this particular
322 * @param Notice $notice The notice to broadcast
324 * @return success flag
327 function mail_broadcast_notice_sms($notice)
329 // Now, get users subscribed to this profile
333 $UT = common_config('db','type')=='pgsql'?'"user"':'user';
334 $user->query('SELECT nickname, smsemail, incomingemail ' .
335 "FROM $UT JOIN subscription " .
336 "ON $UT.id = subscription.subscriber " .
337 'WHERE subscription.subscribed = ' . $notice->profile_id . ' ' .
338 'AND subscription.subscribed != subscription.subscriber ' .
339 "AND $UT.smsemail IS NOT null " .
340 "AND $UT.smsnotify = 1 " .
341 'AND subscription.sms = 1 ');
343 while ($user->fetch()) {
345 'Sending notice ' . $notice->id . ' to ' . $user->smsemail,
347 $success = mail_send_sms_notice_address($notice,
349 $user->incomingemail);
351 // XXX: Not sure, but I think that's the right thing to do
352 common_log(LOG_WARNING,
353 'Sending notice ' . $notice->id . ' to ' .
354 $user->smsemail . ' FAILED, cancelling.',
367 * send a notice to a user via SMS
369 * A convenience wrapper around mail_send_sms_notice_address()
371 * @param Notice $notice notice to send
372 * @param User $user user to receive notice
374 * @see mail_send_sms_notice_address()
376 * @return boolean success flag
379 function mail_send_sms_notice($notice, $user)
381 return mail_send_sms_notice_address($notice,
383 $user->incomingemail);
387 * send a notice to an SMS email address from a given address
389 * We use the user's incoming email address as the "From" address to make
390 * replying to notices easier.
392 * @param Notice $notice notice to send
393 * @param string $smsemail email address to send to
394 * @param string $incomingemail email address to set as 'from'
396 * @return boolean success flag
399 function mail_send_sms_notice_address($notice, $smsemail, $incomingemail)
401 $to = $nickname . ' <' . $smsemail . '>';
403 $other = $notice->getProfile();
405 common_log(LOG_INFO, 'Sending notice ' . $notice->id .
406 ' to ' . $smsemail, __FILE__);
410 $headers['From'] = ($incomingemail) ? $incomingemail : mail_notify_from();
411 $headers['To'] = $to;
412 $headers['Subject'] = sprintf(_('%s status'),
413 $other->getBestName());
415 $body = $notice->content;
417 return mail_send($smsemail, $headers, $body);
421 * send a message to confirm a claim for an SMS number
423 * @param string $code confirmation code
424 * @param string $nickname nickname of user claiming number
425 * @param string $address email address to send the confirmation to
427 * @see common_confirmation_code()
432 function mail_confirm_sms($code, $nickname, $address)
434 $recipients = $address;
436 $headers['From'] = mail_notify_from();
437 $headers['To'] = $nickname . ' <' . $address . '>';
438 $headers['Subject'] = _('SMS confirmation');
442 $body = "$nickname: confirm you own this phone number with this code:";
447 mail_send($recipients, $headers, $body);
451 * send a mail message to notify a user of a 'nudge'
453 * @param User $from user nudging
454 * @param User $to user being nudged
456 * @return boolean success flag
459 function mail_notify_nudge($from, $to)
461 common_init_locale($to->language);
462 $subject = sprintf(_('You\'ve been nudged by %s'), $from->nickname);
464 $from_profile = $from->getProfile();
466 $body = sprintf(_("%1\$s (%2\$s) is wondering what you are up to ".
467 "these days and is inviting you to post some news.\n\n".
468 "So let's hear from you :)\n\n".
470 "Don't reply to this email; it won't get to them.\n\n".
471 "With kind regards,\n".
473 $from_profile->getBestName(),
475 common_local_url('all', array('nickname' => $to->nickname)),
476 common_config('site', 'name'));
477 common_init_locale();
478 return mail_to_user($to, $subject, $body);
482 * send a message to notify a user of a direct message (DM)
484 * This function checks to see if the recipient wants notification
485 * of DMs and has a configured email address.
487 * @param Message $message message to notify about
488 * @param User $from user sending message; default to sender
489 * @param User $to user receiving message; default to recipient
491 * @return boolean success code
494 function mail_notify_message($message, $from=null, $to=null)
496 if (is_null($from)) {
497 $from = User::staticGet('id', $message->from_profile);
501 $to = User::staticGet('id', $message->to_profile);
504 if (is_null($to->email) || !$to->emailnotifymsg) {
508 common_init_locale($to->language);
509 $subject = sprintf(_('New private message from %s'), $from->nickname);
511 $from_profile = $from->getProfile();
513 $body = sprintf(_("%1\$s (%2\$s) sent you a private message:\n\n".
514 "------------------------------------------------------\n".
516 "------------------------------------------------------\n\n".
517 "You can reply to their message here:\n\n".
519 "Don't reply to this email; it won't get to them.\n\n".
520 "With kind regards,\n".
522 $from_profile->getBestName(),
525 common_local_url('newmessage', array('to' => $from->id)),
526 common_config('site', 'name'));
528 common_init_locale();
529 return mail_to_user($to, $subject, $body);
533 * notify a user that one of their notices has been chosen as a 'fave'
535 * Doesn't check that the user has an email address nor if they
536 * want to receive notification of faves. Maybe this happens higher
539 * @param User $other The user whose notice was faved
540 * @param User $user The user who faved the notice
541 * @param Notice $notice The notice that was faved
546 function mail_notify_fave($other, $user, $notice)
548 $profile = $user->getProfile();
550 $bestname = $profile->getBestName();
552 common_init_locale($other->language);
554 $subject = sprintf(_('%s added your notice as a favorite'), $bestname);
556 $body = sprintf(_("%1\$s just added your notice from %2\$s".
557 " as one of their favorites.\n\n" .
558 "The URL of your notice is:\n\n" .
560 "The text of your notice is:\n\n" .
562 "You can see the list of %1\$s's favorites here:\n\n" .
564 "Faithfully yours,\n" .
567 common_exact_date($notice->created),
568 common_local_url('shownotice',
569 array('notice' => $notice->id)),
571 common_local_url('showfavorites',
572 array('nickname' => $user->nickname)),
573 common_config('site', 'name'));
575 common_init_locale();
576 mail_to_user($other, $subject, $body);
580 * notify a user that they have received an "attn:" message AKA "@-reply"
582 * @param User $user The user who recevied the notice
583 * @param Notice $notice The notice that was sent
588 function mail_notify_attn($user, $notice)
590 if (!$user->email || !$user->emailnotifyattn) {
594 $sender = $notice->getProfile();
596 $bestname = $sender->getBestName();
598 common_init_locale($user->language);
600 if ($notice->conversation != $notice->id) {
601 $conversationEmailText = "The full conversation can be read here:\n\n".
603 $conversationUrl = common_local_url('conversation',
604 array('id' => $notice->conversation)).'#notice-'.$notice->id;
606 $conversationEmailText = "%5\$s";
607 $conversationUrl = null;
610 $subject = sprintf(_('%s sent a notice to your attention'), $bestname);
612 $body = sprintf(_("%1\$s just sent a notice to your attention (an '@-reply') on %2\$s.\n\n".
613 "The notice is here:\n\n".
617 $conversationEmailText .
618 "You can reply back here:\n\n".
620 "The list of all @-replies for you here:\n\n" .
622 "Faithfully yours,\n" .
624 "P.S. You can turn off these email notifications here: %8\$s\n"),
626 common_config('site', 'name'),//%2
627 common_local_url('shownotice',
628 array('notice' => $notice->id)),//%3
629 $notice->content,//%4
630 $conversationUrl,//%5
631 common_local_url('newnotice',
632 array('replyto' => $sender->nickname)),//%6
633 common_local_url('replies',
634 array('nickname' => $user->nickname)),//%7
635 common_local_url('emailsettings'));//%8
637 common_init_locale();
638 mail_to_user($user, $subject, $body);
642 * Send a mail message to notify a user that her Twitter bridge link
643 * has stopped working, and therefore has been removed. This can
644 * happen when the user changes her Twitter password, or otherwise
647 * @param User $user user whose Twitter bridge link has been removed
649 * @return boolean success flag
652 function mail_twitter_bridge_removed($user)
654 common_init_locale($user->language);
656 $profile = $user->getProfile();
658 $subject = sprintf(_('Your Twitter bridge has been disabled.'));
660 $site_name = common_config('site', 'name');
662 $body = sprintf(_('Hi, %1$s. We\'re sorry to inform you that your ' .
663 'link to Twitter has been disabled. We no longer seem to have ' .
664 'permission to update your Twitter status. (Did you revoke ' .
665 '%3$s\'s access?)' . "\n\n" .
666 'You can re-enable your Twitter bridge by visiting your ' .
667 "Twitter settings page:\n\n\t%2\$s\n\n" .
668 "Regards,\n%3\$s\n"),
669 $profile->getBestName(),
670 common_local_url('twittersettings'),
671 common_config('site', 'name'));
673 common_init_locale();
674 return mail_to_user($user, $subject, $body);
678 * Send a mail message to notify a user that her Facebook Application
679 * access has been removed.
681 * @param User $user user whose Facebook app link has been removed
683 * @return boolean success flag
686 function mail_facebook_app_removed($user)
688 common_init_locale($user->language);
690 $profile = $user->getProfile();
692 $site_name = common_config('site', 'name');
695 _('Your %1$s Facebook application access has been disabled.',
698 $body = sprintf(_("Hi, %1\$s. We're sorry to inform you that we are " .
699 'unable to update your Facebook status from %2$s, and have disabled ' .
700 'the Facebook application for your account. This may be because ' .
701 'you have removed the Facebook application\'s authorization, or ' .
702 'have deleted your Facebook account. You can re-enable the ' .
703 'Facebook application and automatic status updating by ' .
704 "re-installing the %2\$s Facebook application.\n\nRegards,\n\n%2\$s"),
705 $user->nickname, $site_name);
707 common_init_locale();
708 return mail_to_user($user, $subject, $body);