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
84 # XXX: change to use mail_to_user() above
86 function mail_confirm_address($code, $nickname, $address) {
87 $recipients = $address;
88 $headers['From'] = mail_notify_from();
89 $headers['To'] = $nickname . ' <' . $address . '>';
90 $headers['Subject'] = _('Email address confirmation');
92 $body = "Hey, $nickname.";
94 $body .= 'Someone just entered this email address on ' . common_config('site', 'name') . '.';
96 $body .= 'If it was you, and you want to confirm your entry, use the URL below:';
98 $body .= "\t".common_local_url('confirmaddress',
99 array('code' => $code));
101 $body .= 'If not, just ignore this message.';
103 $body .= 'Thanks for your time, ';
105 $body .= common_config('site', 'name');
108 mail_send($recipients, $headers, $body);
111 function mail_subscribe_notify($listenee, $listener) {
112 if ($listenee->email && $listenee->emailnotifysub) {
113 $profile = $listenee->getProfile();
114 $other = $listener->getProfile();
115 $name = $profile->getBestName();
116 $long_name = ($other->fullname) ? ($other->fullname . ' (' . $other->nickname . ')') : $other->nickname;
117 $recipients = $listenee->email;
118 $headers['From'] = mail_notify_from();
119 $headers['To'] = $name . ' <' . $listenee->email . '>';
120 $headers['Subject'] = sprintf(_('%1$s is now listening to your notices on %2$s.'), $other->getBestName(),
121 common_config('site', 'name'));
122 $body = sprintf(_('%1$s is now listening to your notices on %2$s.'."\n\n".
124 'Faithfully yours,'."\n".'%4$s.'."\n"),
126 common_config('site', 'name'),
128 common_config('site', 'name'));
129 mail_send($recipients, $headers, $body);
133 function mail_new_incoming_notify($user) {
135 $profile = $user->getProfile();
136 $name = $profile->getBestName();
138 $headers['From'] = $user->incomingemail;
139 $headers['To'] = $name . ' <' . $user->email . '>';
140 $headers['Subject'] = sprintf(_('New email address for posting to %s'),
141 common_config('site', 'name'));
143 $body = sprintf(_("You have a new posting address on %1\$s.\n\n".
144 "Send email to %2\$s to post new messages.\n\n".
145 "More email instructions at %3\$s.\n\n".
146 "Faithfully yours,\n%4\$s"),
147 common_config('site', 'name'),
148 $user->incomingemail,
149 common_local_url('doc', array('title' => 'email')),
150 common_config('site', 'name'));
152 mail_send($user->email, $headers, $body);
155 function mail_new_incoming_address() {
156 $prefix = common_good_rand(8);
157 $suffix = mail_domain();
158 return $prefix . '@' . $suffix;
161 function mail_broadcast_notice_sms($notice) {
163 # Now, get users subscribed to this profile
164 # XXX: use a join here rather than looping through results
166 $sub = new Subscription();
167 $sub->subscribed = $notice->profile_id;
170 while ($sub->fetch()) {
171 $user = User::staticGet($sub->subscriber);
172 if ($user && $user->smsemail && $user->smsnotify) {
174 'Sending notice ' . $notice->id . ' to ' . $user->smsemail,
176 $success = mail_send_sms_notice($notice, $user);
178 # XXX: Not sure, but I think that's the right thing to do
179 common_log(LOG_WARNING,
180 'Sending notice ' . $notice->id . ' to ' . $user->smsemail . ' FAILED, cancelling.',
191 function mail_send_sms_notice($notice, $user) {
192 $profile = $user->getProfile();
193 $name = $profile->getBestName();
194 $to = $name . ' <' . $user->smsemail . '>';
195 $other = $notice->getProfile();
197 common_log(LOG_INFO, "Sending notice " . $notice->id . " to " . $user->smsemail, __FILE__);
200 $headers['From'] = $user->incomingemail;
201 $headers['To'] = $to;
202 $headers['Subject'] = sprintf(_('%s status'),
203 $other->getBestName());
204 $body = $notice->content;
206 return mail_send($user->smsemail, $headers, $body);
209 function mail_confirm_sms($code, $nickname, $address) {
211 $recipients = $address;
213 $headers['From'] = mail_notify_from();
214 $headers['To'] = $nickname . ' <' . $address . '>';
215 $headers['Subject'] = _('SMS confirmation');
217 $body = "$nickname: confirm you own this phone number with this code:";
222 mail_send($recipients, $headers, $body);