3 * Laconica - a distributed open-source microblogging tool
4 * Copyright (C) 2008, Controlez-Vous, Inc.
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
16 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 if (!defined('LACONICA')) { exit(1); }
22 require_once('Mail.php');
24 function mail_backend() {
25 static $backend = NULL;
29 $backend = Mail::factory($config['mail']['backend'],
30 ($config['mail']['params']) ? $config['mail']['params'] : array());
31 if (PEAR::isError($backend)) {
32 common_server_error($backend->getMessage(), 500);
38 # XXX: use Mail_Queue... maybe
40 function mail_send($recipients, $headers, $body) {
41 $backend = mail_backend();
42 if (!isset($headers['Content-Type'])) {
43 $headers['Content-Type'] = 'text/plain; charset=UTF-8';
45 assert($backend); # throws an error if it's bad
46 $sent = $backend->send($recipients, $headers, $body);
47 if (PEAR::isError($sent)) {
48 common_log(LOG_ERR, 'Email error: ' . $sent->getMessage());
54 function mail_domain() {
55 $maildomain = common_config('mail', 'domain');
57 $maildomain = common_config('site', 'server');
62 function mail_notify_from() {
63 $notifyfrom = common_config('mail', 'notifyfrom');
65 $domain = mail_domain();
66 $notifyfrom = common_config('site', 'name') .' <noreply@'.$domain.'>';
71 function mail_to_user(&$user, $subject, $body, $address=NULL) {
73 $address = $user->email;
76 $recipients = $address;
77 $profile = $user->getProfile();
79 $headers['From'] = mail_notify_from();
80 $headers['To'] = $profile->getBestName() . ' <' . $address . '>';
81 $headers['Subject'] = $subject;
83 return mail_send($recipients, $headers, $body);
86 # For confirming a Jabber address
88 function mail_confirm_address($user, $code, $nickname, $address) {
90 $subject = _('Email address confirmation');
92 $body = sprintf(_("Hey, %s.\n\nSomeone just entered this email address on %s.\n\n" .
93 "If it was you, and you want to confirm your entry, use the URL below:\n\n\t%s\n\n" .
94 "If not, just ignore this message.\n\nThanks for your time, \n%s\n")
95 , $nickname, common_config('site', 'name')
96 , common_local_url('confirmaddress', array('code' => $code)), common_config('site', 'name'));
97 return mail_to_user($user, $subject, $body, $address);
100 function mail_subscribe_notify($listenee, $listener) {
101 $other = $listener->getProfile();
102 mail_subscribe_notify_profile($listenee, $other);
105 function mail_subscribe_notify_profile($listenee, $other) {
106 if ($listenee->email && $listenee->emailnotifysub) {
107 // use the recipients localization
108 common_init_locale($listenee->language);
109 $profile = $listenee->getProfile();
110 $name = $profile->getBestName();
111 $long_name = ($other->fullname) ? ($other->fullname . ' (' . $other->nickname . ')') : $other->nickname;
112 $recipients = $listenee->email;
113 $headers['From'] = mail_notify_from();
114 $headers['To'] = $name . ' <' . $listenee->email . '>';
115 $headers['Subject'] = sprintf(_('%1$s is now listening to your notices on %2$s.'),
116 $other->getBestName(),
117 common_config('site', 'name'));
118 $body = sprintf(_('%1$s is now listening to your notices on %2$s.'."\n\n".
123 "\n".'Faithfully yours,'."\n".'%7$s.'."\n\n".
125 "Change your email address or notification options at %8$s"),
127 common_config('site', 'name'),
129 ($other->location) ? sprintf(_("Location: %s\n"), $other->location) : '',
130 ($other->homepage) ? sprintf(_("Homepage: %s\n"), $other->homepage) : '',
131 ($other->bio) ? sprintf(_("Bio: %s\n\n"), $other->bio) : '',
132 common_config('site', 'name'),
133 common_local_url('emailsettings'));
134 // reset localization
135 common_init_locale();
136 mail_send($recipients, $headers, $body);
140 function mail_new_incoming_notify($user) {
142 $profile = $user->getProfile();
143 $name = $profile->getBestName();
145 $headers['From'] = $user->incomingemail;
146 $headers['To'] = $name . ' <' . $user->email . '>';
147 $headers['Subject'] = sprintf(_('New email address for posting to %s'),
148 common_config('site', 'name'));
150 $body = sprintf(_("You have a new posting address on %1\$s.\n\n".
151 "Send email to %2\$s to post new messages.\n\n".
152 "More email instructions at %3\$s.\n\n".
153 "Faithfully yours,\n%4\$s"),
154 common_config('site', 'name'),
155 $user->incomingemail,
156 common_local_url('doc', array('title' => 'email')),
157 common_config('site', 'name'));
159 mail_send($user->email, $headers, $body);
162 function mail_new_incoming_address() {
163 $prefix = common_confirmation_code(64);
164 $suffix = mail_domain();
165 return $prefix . '@' . $suffix;
168 function mail_broadcast_notice_sms($notice) {
170 # Now, get users subscribed to this profile
174 $user->query('SELECT nickname, smsemail, incomingemail ' .
175 'FROM user JOIN subscription ' .
176 'ON user.id = subscription.subscriber ' .
177 'WHERE subscription.subscribed = ' . $notice->profile_id . ' ' .
178 'AND user.smsemail IS NOT NULL ' .
179 'AND user.smsnotify = 1 ' .
180 'AND subscription.sms = 1 ');
182 while ($user->fetch()) {
184 'Sending notice ' . $notice->id . ' to ' . $user->smsemail,
186 $success = mail_send_sms_notice_address($notice, $user->smsemail, $user->incomingemail);
188 # XXX: Not sure, but I think that's the right thing to do
189 common_log(LOG_WARNING,
190 'Sending notice ' . $notice->id . ' to ' . $user->smsemail . ' FAILED, cancelling.',
202 function mail_send_sms_notice($notice, $user) {
203 return mail_send_sms_notice_address($notice, $user->smsemail, $user->incomingemail);
206 function mail_send_sms_notice_address($notice, $smsemail, $incomingemail) {
208 $to = $nickname . ' <' . $smsemail . '>';
209 $other = $notice->getProfile();
211 common_log(LOG_INFO, "Sending notice " . $notice->id . " to " . $smsemail, __FILE__);
214 $headers['From'] = (isset($incomingemail)) ? $incomingemail : mail_notify_from();
215 $headers['To'] = $to;
216 $headers['Subject'] = sprintf(_('%s status'),
217 $other->getBestName());
218 $body = $notice->content;
220 return mail_send($smsemail, $headers, $body);
223 function mail_confirm_sms($code, $nickname, $address) {
225 $recipients = $address;
227 $headers['From'] = mail_notify_from();
228 $headers['To'] = $nickname . ' <' . $address . '>';
229 $headers['Subject'] = _('SMS confirmation');
231 $body = "$nickname: confirm you own this phone number with this code:";
236 mail_send($recipients, $headers, $body);
239 function mail_notify_nudge($from, $to) {
240 common_init_locale($to->language);
241 $subject = sprintf(_('You\'ve been nudged by %s'), $from->nickname);
243 $from_profile = $from->getProfile();
245 $body = sprintf(_("%1\$s (%2\$s) is wondering what you are up to these days and is inviting you to post some news.\n\n".
246 "So let's hear from you :)\n\n".
248 "Don't reply to this email; it won't get to them.\n\n".
249 "With kind regards,\n".
251 $from_profile->getBestName(),
253 common_local_url('all', array('nickname' => $to->nickname)),
254 common_config('site', 'name'));
255 common_init_locale();
256 return mail_to_user($to, $subject, $body);
259 function mail_notify_message($message, $from=NULL, $to=NULL) {
261 if (is_null($from)) {
262 $from = User::staticGet('id', $message->from_profile);
266 $to = User::staticGet('id', $message->to_profile);
269 if (is_null($to->email) || !$to->emailnotifymsg) {
273 common_init_locale($to->language);
274 $subject = sprintf(_('New private message from %s'), $from->nickname);
276 $from_profile = $from->getProfile();
278 $body = sprintf(_("%1\$s (%2\$s) sent you a private message:\n\n".
279 "------------------------------------------------------\n".
281 "------------------------------------------------------\n\n".
282 "You can reply to their message here:\n\n".
284 "Don't reply to this email; it won't get to them.\n\n".
285 "With kind regards,\n".
287 $from_profile->getBestName(),
290 common_local_url('newmessage', array('to' => $from->id)),
291 common_config('site', 'name'));
293 common_init_locale();
294 return mail_to_user($to, $subject, $body);
297 function mail_notify_fave($other, $user, $notice) {
299 $profile = $user->getProfile();
300 $bestname = $profile->getBestName();
301 common_init_locale($other->language);
302 $subject = sprintf(_('%s added your notice as a favorite'), $bestname);
303 $body = sprintf(_("%1\$s just added your notice from %2\$s as one of their favorites.\n\n" .
304 "In case you forgot, you can see the text of your notice here:\n\n" .
306 "You can see the list of %1\$s's favorites here:\n\n" .
308 "Faithfully yours,\n" .
311 common_exact_date($notice->created),
312 common_local_url('shownotice', array('notice' => $notice->id)),
313 common_local_url('showfavorites', array('nickname' => $user->nickname)),
314 common_config('site', 'name'));
316 common_init_locale();
317 mail_to_user($other, $subject, $body);