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.'), $other->getBestName(),
116 common_config('site', 'name'));
117 $body = sprintf(_('%1$s is now listening to your notices on %2$s.'."\n\n".
119 'Faithfully yours,'."\n".'%4$s.'."\n"),
121 common_config('site', 'name'),
123 common_config('site', 'name'));
125 // reset localization
126 common_init_locale();
127 mail_send($recipients, $headers, $body);
131 function mail_new_incoming_notify($user) {
133 $profile = $user->getProfile();
134 $name = $profile->getBestName();
136 $headers['From'] = $user->incomingemail;
137 $headers['To'] = $name . ' <' . $user->email . '>';
138 $headers['Subject'] = sprintf(_('New email address for posting to %s'),
139 common_config('site', 'name'));
141 $body = sprintf(_("You have a new posting address on %1\$s.\n\n".
142 "Send email to %2\$s to post new messages.\n\n".
143 "More email instructions at %3\$s.\n\n".
144 "Faithfully yours,\n%4\$s"),
145 common_config('site', 'name'),
146 $user->incomingemail,
147 common_local_url('doc', array('title' => 'email')),
148 common_config('site', 'name'));
150 mail_send($user->email, $headers, $body);
153 function mail_new_incoming_address() {
154 $prefix = common_confirmation_code(64);
155 $suffix = mail_domain();
156 return $prefix . '@' . $suffix;
159 function mail_broadcast_notice_sms($notice) {
161 # Now, get users subscribed to this profile
165 $user->query('SELECT nickname, smsemail, incomingemail ' .
166 'FROM user JOIN subscription ' .
167 'ON user.id = subscription.subscriber ' .
168 'WHERE subscription.subscribed = ' . $notice->profile_id . ' ' .
169 'AND user.smsemail IS NOT NULL ' .
170 'AND user.smsnotify = 1 ' .
171 'AND subscription.sms = 1 ');
173 while ($user->fetch()) {
175 'Sending notice ' . $notice->id . ' to ' . $user->smsemail,
177 $success = mail_send_sms_notice_address($notice, $user->smsemail, $user->incomingemail);
179 # XXX: Not sure, but I think that's the right thing to do
180 common_log(LOG_WARNING,
181 'Sending notice ' . $notice->id . ' to ' . $user->smsemail . ' FAILED, cancelling.',
193 function mail_send_sms_notice($notice, $user) {
194 return mail_send_sms_notice_address($notice, $user->smsemail, $user->incomingemail);
197 function mail_send_sms_notice_address($notice, $smsemail, $incomingemail) {
199 $to = $nickname . ' <' . $smsemail . '>';
200 $other = $notice->getProfile();
202 common_log(LOG_INFO, "Sending notice " . $notice->id . " to " . $smsemail, __FILE__);
205 $headers['From'] = (isset($incomingemail)) ? $incomingemail : mail_notify_from();
206 $headers['To'] = $to;
207 $headers['Subject'] = sprintf(_('%s status'),
208 $other->getBestName());
209 $body = $notice->content;
211 return mail_send($smsemail, $headers, $body);
214 function mail_confirm_sms($code, $nickname, $address) {
216 $recipients = $address;
218 $headers['From'] = mail_notify_from();
219 $headers['To'] = $nickname . ' <' . $address . '>';
220 $headers['Subject'] = _('SMS confirmation');
222 $body = "$nickname: confirm you own this phone number with this code:";
227 mail_send($recipients, $headers, $body);
230 function mail_notify_nudge($from, $to) {
231 common_init_locale($to->language);
232 $subject = sprintf(_('You\'ve been nudged by %s'), $from->nickname);
234 $from_profile = $from->getProfile();
236 $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".
237 "So let's hear from you :)\n\n".
239 "Don't reply to this email; it won't get to them.\n\n".
240 "With kind regards,\n".
242 $from_profile->getBestName(),
244 common_local_url('all', array('nickname' => $to->nickname)),
245 common_config('site', 'name'));
246 common_init_locale();
247 return mail_to_user($to, $subject, $body);
250 function mail_notify_message($message, $from=NULL, $to=NULL) {
252 if (is_null($from)) {
253 $from = User::staticGet('id', $message->from_profile);
257 $to = User::staticGet('id', $message->to_profile);
260 if (is_null($to->email) || !$to->emailnotifymsg) {
264 common_init_locale($to->language);
265 $subject = sprintf(_('New private message from %s'), $from->nickname);
267 $from_profile = $from->getProfile();
269 $body = sprintf(_("%1\$s (%2\$s) sent you a private message:\n\n".
270 "------------------------------------------------------\n".
272 "------------------------------------------------------\n\n".
273 "You can reply to their message here:\n\n".
275 "Don't reply to this email; it won't get to them.\n\n".
276 "With kind regards,\n".
278 $from_profile->getBestName(),
281 common_local_url('newmessage', array('to' => $from->id)),
282 common_config('site', 'name'));
284 common_init_locale();
285 return mail_to_user($to, $subject, $body);
288 function mail_notify_fave($other, $user, $notice) {
290 $profile = $user->getProfile();
291 $bestname = $profile->getBestName();
292 common_init_locale($other->language);
293 $subject = sprintf(_('%s added your notice as a favorite'), $bestname);
294 $body = sprintf(_("%1\$s just added your notice from %2\$s as one of their favorites.\n\n" .
295 "In case you forgot, you can see the text of your notice here:\n\n" .
297 "You can see the list of %1\$s's favorites here:\n\n" .
299 "Faithfully yours,\n" .
302 common_exact_date($notice->created),
303 common_local_url('shownotice', array('notice' => $notice->id)),
304 common_local_url('showfavorites', array('nickname' => $user->nickname)),
305 common_config('site', 'name'));
307 common_init_locale();
308 mail_to_user($other, $subject, $body);