3 * Laconica, 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 Control Yourself, Inc.
29 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
30 * @link http://laconi.ca/
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;
54 $backend = Mail::factory($config['mail']['backend'],
55 ($config['mail']['params']) ?
56 $config['mail']['params'] :
58 if (PEAR::isError($backend)) {
59 common_server_error($backend->getMessage(), 500);
66 * send an email to one or more recipients
68 * @param array $recipients array of strings with email addresses of recipients
69 * @param array $headers array mapping strings to strings for email headers
70 * @param string $body body of the email
72 * @return boolean success flag
75 function mail_send($recipients, $headers, $body)
77 // XXX: use Mail_Queue... maybe
78 $backend = mail_backend();
79 if (!isset($headers['Content-Type'])) {
80 $headers['Content-Type'] = 'text/plain; charset=UTF-8';
82 assert($backend); // throws an error if it's bad
83 $sent = $backend->send($recipients, $headers, $body);
84 if (PEAR::isError($sent)) {
85 common_log(LOG_ERR, 'Email error: ' . $sent->getMessage());
92 * returns the configured mail domain
94 * Defaults to the server name.
96 * @return string mail domain, suitable for making email addresses.
99 function mail_domain()
101 $maildomain = common_config('mail', 'domain');
103 $maildomain = common_config('site', 'server');
109 * returns a good address for sending email from this server
111 * Uses either the configured value or a faked-up value made
112 * from the mail domain.
114 * @return string notify from address
117 function mail_notify_from()
119 $notifyfrom = common_config('mail', 'notifyfrom');
123 $domain = mail_domain();
125 $notifyfrom = common_config('site', 'name') .' <noreply@'.$domain.'>';
132 * sends email to a user
134 * @param User &$user user to send email to
135 * @param string $subject subject of the email
136 * @param string $body body of the email
137 * @param string $address optional specification of email address
139 * @return boolean success flag
142 function mail_to_user(&$user, $subject, $body, $address=null)
145 $address = $user->email;
148 $recipients = $address;
149 $profile = $user->getProfile();
151 $headers['From'] = mail_notify_from();
152 $headers['To'] = $profile->getBestName() . ' <' . $address . '>';
153 $headers['Subject'] = $subject;
155 return mail_send($recipients, $headers, $body);
159 * Send an email to confirm a user's control of an email address
161 * @param User $user User claiming the email address
162 * @param string $code Confirmation code
163 * @param string $nickname Nickname of user
164 * @param string $address email address to confirm
166 * @see common_confirmation_code()
168 * @return success flag
171 function mail_confirm_address($user, $code, $nickname, $address)
173 $subject = _('Email address confirmation');
175 $body = sprintf(_("Hey, %s.\n\n".
176 "Someone just entered this email address on %s.\n\n" .
177 "If it was you, and you want to confirm your entry, ".
178 "use the URL below:\n\n\t%s\n\n" .
179 "If not, just ignore this message.\n\n".
180 "Thanks for your time, \n%s\n"),
181 $nickname, common_config('site', 'name'),
182 common_local_url('confirmaddress', array('code' => $code)),
183 common_config('site', 'name'));
184 return mail_to_user($user, $subject, $body, $address);
188 * notify a user of subscription by another user
190 * This is just a wrapper around the profile-based version.
192 * @param User $listenee user who is being subscribed to
193 * @param User $listener user who is subscribing
195 * @see mail_subscribe_notify_profile()
200 function mail_subscribe_notify($listenee, $listener)
202 $other = $listener->getProfile();
203 mail_subscribe_notify_profile($listenee, $other);
207 * notify a user of subscription by a profile (remote or local)
209 * This function checks to see if the listenee has an email
210 * address and wants subscription notices.
212 * @param User $listenee user who's being subscribed to
213 * @param Profile $other profile of person who's listening
218 function mail_subscribe_notify_profile($listenee, $other)
220 if ($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 $user->query('SELECT nickname, smsemail, incomingemail ' .
335 'FROM user JOIN subscription ' .
336 'ON user.id = subscription.subscriber ' .
337 'WHERE subscription.subscribed = ' . $notice->profile_id . ' ' .
338 'AND user.smsemail IS NOT null ' .
339 'AND user.smsnotify = 1 ' .
340 'AND subscription.sms = 1 ');
342 while ($user->fetch()) {
344 'Sending notice ' . $notice->id . ' to ' . $user->smsemail,
346 $success = mail_send_sms_notice_address($notice,
348 $user->incomingemail);
350 // XXX: Not sure, but I think that's the right thing to do
351 common_log(LOG_WARNING,
352 'Sending notice ' . $notice->id . ' to ' .
353 $user->smsemail . ' FAILED, cancelling.',
366 * send a notice to a user via SMS
368 * A convenience wrapper around mail_send_sms_notice_address()
370 * @param Notice $notice notice to send
371 * @param User $user user to receive notice
373 * @see mail_send_sms_notice_address()
375 * @return boolean success flag
378 function mail_send_sms_notice($notice, $user)
380 return mail_send_sms_notice_address($notice,
382 $user->incomingemail);
386 * send a notice to an SMS email address from a given address
388 * We use the user's incoming email address as the "From" address to make
389 * replying to notices easier.
391 * @param Notice $notice notice to send
392 * @param string $smsemail email address to send to
393 * @param string $incomingemail email address to set as 'from'
395 * @return boolean success flag
398 function mail_send_sms_notice_address($notice, $smsemail, $incomingemail)
400 $to = $nickname . ' <' . $smsemail . '>';
402 $other = $notice->getProfile();
404 common_log(LOG_INFO, 'Sending notice ' . $notice->id .
405 ' to ' . $smsemail, __FILE__);
409 $headers['From'] = ($incomingemail) ? $incomingemail : mail_notify_from();
410 $headers['To'] = $to;
411 $headers['Subject'] = sprintf(_('%s status'),
412 $other->getBestName());
414 $body = $notice->content;
416 return mail_send($smsemail, $headers, $body);
420 * send a message to confirm a claim for an SMS number
422 * @param string $code confirmation code
423 * @param string $nickname nickname of user claiming number
424 * @param string $address email address to send the confirmation to
426 * @see common_confirmation_code()
431 function mail_confirm_sms($code, $nickname, $address)
433 $recipients = $address;
435 $headers['From'] = mail_notify_from();
436 $headers['To'] = $nickname . ' <' . $address . '>';
437 $headers['Subject'] = _('SMS confirmation');
441 $body = "$nickname: confirm you own this phone number with this code:";
446 mail_send($recipients, $headers, $body);
450 * send a mail message to notify a user of a 'nudge'
452 * @param User $from user nudging
453 * @param User $to user being nudged
455 * @return boolean success flag
458 function mail_notify_nudge($from, $to)
460 common_init_locale($to->language);
461 $subject = sprintf(_('You\'ve been nudged by %s'), $from->nickname);
463 $from_profile = $from->getProfile();
465 $body = sprintf(_("%1\$s (%2\$s) is wondering what you are up to ".
466 "these days and is inviting you to post some news.\n\n".
467 "So let's hear from you :)\n\n".
469 "Don't reply to this email; it won't get to them.\n\n".
470 "With kind regards,\n".
472 $from_profile->getBestName(),
474 common_local_url('all', array('nickname' => $to->nickname)),
475 common_config('site', 'name'));
476 common_init_locale();
477 return mail_to_user($to, $subject, $body);
481 * send a message to notify a user of a direct message (DM)
483 * This function checks to see if the recipient wants notification
484 * of DMs and has a configured email address.
486 * @param Message $message message to notify about
487 * @param User $from user sending message; default to sender
488 * @param User $to user receiving message; default to recipient
490 * @return boolean success code
493 function mail_notify_message($message, $from=null, $to=null)
495 if (is_null($from)) {
496 $from = User::staticGet('id', $message->from_profile);
500 $to = User::staticGet('id', $message->to_profile);
503 if (is_null($to->email) || !$to->emailnotifymsg) {
507 common_init_locale($to->language);
508 $subject = sprintf(_('New private message from %s'), $from->nickname);
510 $from_profile = $from->getProfile();
512 $body = sprintf(_("%1\$s (%2\$s) sent you a private message:\n\n".
513 "------------------------------------------------------\n".
515 "------------------------------------------------------\n\n".
516 "You can reply to their message here:\n\n".
518 "Don't reply to this email; it won't get to them.\n\n".
519 "With kind regards,\n".
521 $from_profile->getBestName(),
524 common_local_url('newmessage', array('to' => $from->id)),
525 common_config('site', 'name'));
527 common_init_locale();
528 return mail_to_user($to, $subject, $body);
532 * notify a user that one of their notices has been chosen as a 'fave'
534 * Doesn't check that the user has an email address nor if they
535 * want to receive notification of faves. Maybe this happens higher
538 * @param User $other The user whose notice was faved
539 * @param User $user The user who faved the notice
540 * @param Notice $notice The notice that was faved
545 function mail_notify_fave($other, $user, $notice)
547 $profile = $user->getProfile();
549 $bestname = $profile->getBestName();
551 common_init_locale($other->language);
553 $subject = sprintf(_('%s added your notice as a favorite'), $bestname);
555 $body = sprintf(_("%1\$s just added your notice from %2\$s".
556 " as one of their favorites.\n\n" .
557 "In case you forgot, you can see the text".
558 " of your notice here:\n\n" .
560 "You can see the list of %1\$s's favorites here:\n\n" .
562 "Faithfully yours,\n" .
565 common_exact_date($notice->created),
566 common_local_url('shownotice',
567 array('notice' => $notice->id)),
568 common_local_url('showfavorites',
569 array('nickname' => $user->nickname)),
570 common_config('site', 'name'));
572 common_init_locale();
573 mail_to_user($other, $subject, $body);