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 assert($backend); # throws an error if it's bad
43 $sent = $backend->send($recipients, $headers, $body);
44 if (PEAR::isError($sent)) {
45 common_log(LOG_ERR, 'Email error: ' . $sent->getMessage());
51 function mail_domain() {
52 $maildomain = common_config('mail', 'domain');
54 $maildomain = common_config('site', 'server');
59 function mail_notify_from() {
60 $notifyfrom = common_config('mail', 'notifyfrom');
62 $domain = mail_domain();
63 $notifyfrom = common_config('site', 'name') .' <noreply@'.$domain.'>';
68 function mail_to_user(&$user, $subject, $body, $address=NULL) {
70 $address = $user->email;
73 $recipients = $address;
74 $profile = $user->getProfile();
76 $headers['From'] = mail_notify_from();
77 $headers['To'] = $profile->getBestName() . ' <' . $address . '>';
78 $headers['Subject'] = $subject;
80 return mail_send($recipients, $headers, $body);
83 # For confirming a Jabber address
85 function mail_confirm_address($code, $nickname, $address) {
86 $user = common_current_user();
87 $subject = _('Email address confirmation');
89 $body = sprintf(_("Hey, %s.\n\nSomeone just entered this email address on %s.\n\n" .
90 "If it was you, and you want to confirm your entry, use the URL below:\n\n\t%s\n\n" .
91 "If not, just ignore this message.\n\nThanks for your time, \n%s\n")
92 , $nickname, common_config('site', 'name')
93 , common_local_url('confirmaddress', array('code' => $code)), common_config('site', 'name'));
94 return mail_to_user($user, $subject, $body, $address);
97 function mail_subscribe_notify($listenee, $listener) {
98 $other = $listener->getProfile();
99 mail_subscribe_notify_profile($listenee, $other);
102 function mail_subscribe_notify_profile($listenee, $other) {
103 if ($listenee->email && $listenee->emailnotifysub) {
104 // use the recipients localization
105 common_init_locale($listenee->language);
106 $profile = $listenee->getProfile();
107 $name = $profile->getBestName();
108 $long_name = ($other->fullname) ? ($other->fullname . ' (' . $other->nickname . ')') : $other->nickname;
109 $recipients = $listenee->email;
110 $headers['From'] = mail_notify_from();
111 $headers['To'] = $name . ' <' . $listenee->email . '>';
112 $headers['Subject'] = sprintf(_('%1$s is now listening to your notices on %2$s.'), $other->getBestName(),
113 common_config('site', 'name'));
114 $body = sprintf(_('%1$s is now listening to your notices on %2$s.'."\n\n".
116 'Faithfully yours,'."\n".'%4$s.'."\n"),
118 common_config('site', 'name'),
120 common_config('site', 'name'));
122 // reset localization
123 common_init_locale();
124 mail_send($recipients, $headers, $body);
128 function mail_new_incoming_notify($user) {
130 $profile = $user->getProfile();
131 $name = $profile->getBestName();
133 $headers['From'] = $user->incomingemail;
134 $headers['To'] = $name . ' <' . $user->email . '>';
135 $headers['Subject'] = sprintf(_('New email address for posting to %s'),
136 common_config('site', 'name'));
138 $body = sprintf(_("You have a new posting address on %1\$s.\n\n".
139 "Send email to %2\$s to post new messages.\n\n".
140 "More email instructions at %3\$s.\n\n".
141 "Faithfully yours,\n%4\$s"),
142 common_config('site', 'name'),
143 $user->incomingemail,
144 common_local_url('doc', array('title' => 'email')),
145 common_config('site', 'name'));
147 mail_send($user->email, $headers, $body);
150 function mail_new_incoming_address() {
151 $prefix = common_confirmation_code(64);
152 $suffix = mail_domain();
153 return $prefix . '@' . $suffix;
156 function mail_broadcast_notice_sms($notice) {
158 # Now, get users subscribed to this profile
162 $user->query('SELECT nickname, smsemail, incomingemail ' .
163 'FROM user JOIN subscription ' .
164 'ON user.id = subscription.subscriber ' .
165 'WHERE subscription.subscribed = ' . $notice->profile_id . ' ' .
166 'AND user.smsemail IS NOT NULL ' .
167 'AND user.smsnotify = 1');
169 while ($user->fetch()) {
171 'Sending notice ' . $notice->id . ' to ' . $user->smsemail,
173 $success = mail_send_sms_notice_address($notice, $user->smsemail, $user->incomingemail);
175 # XXX: Not sure, but I think that's the right thing to do
176 common_log(LOG_WARNING,
177 'Sending notice ' . $notice->id . ' to ' . $user->smsemail . ' FAILED, cancelling.',
189 function mail_send_sms_notice($notice, $user) {
190 return mail_send_sms_notice_address($notice, $user->smsemail, $user->incomingemail);
193 function mail_send_sms_notice_address($notice, $smsemail, $incomingemail) {
195 $to = $nickname . ' <' . $smsemail . '>';
196 $other = $notice->getProfile();
198 common_log(LOG_INFO, "Sending notice " . $notice->id . " to " . $smsemail, __FILE__);
201 $headers['From'] = (isset($incomingemail)) ? $incomingemail : mail_notify_from();
202 $headers['To'] = $to;
203 $headers['Subject'] = sprintf(_('%s status'),
204 $other->getBestName());
205 $body = $notice->content;
207 return mail_send($smsemail, $headers, $body);
210 function mail_confirm_sms($code, $nickname, $address) {
212 $recipients = $address;
214 $headers['From'] = mail_notify_from();
215 $headers['To'] = $nickname . ' <' . $address . '>';
216 $headers['Subject'] = _('SMS confirmation');
218 $body = "$nickname: confirm you own this phone number with this code:";
223 mail_send($recipients, $headers, $body);
227 function mail_notify_nudge($from, $to) {
228 common_init_locale($to->language);
229 $subject = sprintf(_('You\'ve been nudged by %s'), $from->nickname);
231 $from_profile = $from->getProfile();
233 $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".
234 "So let's hear from you :)\n\n".
236 "Don't reply to this email; it won't get to them.\n\n".
237 "With kind regards,\n".
239 $from_profile->getBestName(),
241 common_local_url('all', array('nickname' => $to->nickname)),
242 common_config('site', 'name'));
243 common_init_locale();
244 return mail_to_user($to, $subject, $body);
249 function mail_notify_message($message, $from=NULL, $to=NULL) {
251 if (is_null($from)) {
252 $from = User::staticGet('id', $message->from_profile);
256 $to = User::staticGet('id', $message->to_profile);
259 if (is_null($to->email) || !$to->emailnotifymsg) {
263 common_init_locale($to->language);
264 $subject = sprintf(_('New private message from %s'), $from->nickname);
266 $from_profile = $from->getProfile();
268 $body = sprintf(_("%1\$s (%2\$s) sent you a private message:\n\n".
269 "------------------------------------------------------\n".
271 "------------------------------------------------------\n\n".
272 "You can reply to their message here:\n\n".
274 "Don't reply to this email; it won't get to them.\n\n".
275 "With kind regards,\n".
277 $from_profile->getBestName(),
280 common_local_url('newmessage', array('to' => $from->id)),
281 common_config('site', 'name'));
283 common_init_locale();
284 return mail_to_user($to, $subject, $body);
287 function mail_notify_fave($other, $user, $notice) {
289 $profile = $user->getProfile();
290 $bestname = $profile->getBestName();
291 common_init_locale($other->language);
292 $subject = sprintf(_('%s added your notice as a favorite'), $bestname);
293 $body = sprintf(_("%1\$s just added your notice from %2\$s as one of their favorites.\n\n" .
294 "In case you forgot, you can see the text of your notice here:\n\n" .
296 "You can see the list of %1\$s's favorites here:\n\n" .
298 "Faithfully yours,\n" .
301 common_exact_date($notice->created),
302 common_local_url('shownotice', array('notice' => $notice->id)),
303 common_local_url('showfavorites', array('nickname' => $user->nickname)),
304 common_config('site', 'name'));
306 common_init_locale();
307 mail_to_user($other, $subject, $body);