]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/mail.php
send all emails in the destination's language.
[quix0rs-gnu-social.git] / lib / mail.php
1 <?php
2 /*
3  * Laconica - a distributed open-source microblogging tool
4  * Copyright (C) 2008, Controlez-Vous, Inc.
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 if (!defined('LACONICA')) { exit(1); }
21
22 require_once('Mail.php');
23
24 function mail_backend() {
25         static $backend = NULL;
26
27         if (!$backend) {
28                 global $config;
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);
33                 }
34         }
35         return $backend;
36 }
37
38 # XXX: use Mail_Queue... maybe
39
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());
46                 return false;
47         }
48         return true;
49 }
50
51 function mail_domain() {
52         $maildomain = common_config('mail', 'domain');
53         if (!$maildomain) {
54                 $maildomain = common_config('site', 'server');
55         }
56         return $maildomain;
57 }
58
59 function mail_notify_from() {
60         $notifyfrom = common_config('mail', 'notifyfrom');
61         if (!$notifyfrom) {
62                 $domain = mail_domain();
63                 $notifyfrom = common_config('site', 'name') .' <noreply@'.$domain.'>';
64         }
65         return $notifyfrom;
66 }
67
68 function mail_to_user(&$user, $subject, $body, $address=NULL) {
69         if (!$address) {
70                 $address = $user->email;
71         }
72
73         $recipients = $address;
74         $profile = $user->getProfile();
75
76         $headers['From'] = mail_notify_from();
77         $headers['To'] = $profile->getBestName() . ' <' . $address . '>';
78         $headers['Subject'] = $subject;
79
80         return mail_send($recipients, $headers, $body);
81 }
82
83 # For confirming a Jabber address
84
85 function mail_confirm_address($code, $nickname, $address) {
86     $user = common_current_user();
87         $subject = _('Email address confirmation');
88
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);
95 }
96
97 function mail_subscribe_notify($listenee, $listener) {
98         $other = $listener->getProfile();
99         mail_subscribe_notify_profile($listenee, $other);
100 }
101
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".
115                                                    "\t".'%3$s'."\n\n".
116                                                    'Faithfully yours,'."\n".'%4$s.'."\n"),
117                                                  $long_name,
118                                                  common_config('site', 'name'),
119                                                  $other->profileurl,
120                                                  common_config('site', 'name'));
121
122         // reset localization
123         common_init_locale();
124                 mail_send($recipients, $headers, $body);
125         }
126 }
127
128 function mail_new_incoming_notify($user) {
129
130         $profile = $user->getProfile();
131         $name = $profile->getBestName();
132
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'));
137
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'));
146
147         mail_send($user->email, $headers, $body);
148 }
149
150 function mail_new_incoming_address() {
151         $prefix = common_confirmation_code(64);
152         $suffix = mail_domain();
153         return $prefix . '@' . $suffix;
154 }
155
156 function mail_broadcast_notice_sms($notice) {
157
158     # Now, get users subscribed to this profile
159
160         $user = new User();
161
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');
168
169         while ($user->fetch()) {
170                 common_log(LOG_INFO,
171                                    'Sending notice ' . $notice->id . ' to ' . $user->smsemail,
172                                    __FILE__);
173                 $success = mail_send_sms_notice_address($notice, $user->smsemail, $user->incomingemail);
174                 if (!$success) {
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.',
178                                            __FILE__);
179                         return false;
180                 }
181         }
182
183         $user->free();
184         unset($user);
185
186         return true;
187 }
188
189 function mail_send_sms_notice($notice, $user) {
190         return mail_send_sms_notice_address($notice, $user->smsemail, $user->incomingemail);
191 }
192
193 function mail_send_sms_notice_address($notice, $smsemail, $incomingemail) {
194
195         $to = $nickname . ' <' . $smsemail . '>';
196         $other = $notice->getProfile();
197
198         common_log(LOG_INFO, "Sending notice " . $notice->id . " to " . $smsemail, __FILE__);
199
200         $headers = array();
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;
206
207         return mail_send($smsemail, $headers, $body);
208 }
209
210 function mail_confirm_sms($code, $nickname, $address) {
211
212         $recipients = $address;
213
214         $headers['From'] = mail_notify_from();
215         $headers['To'] = $nickname . ' <' . $address . '>';
216         $headers['Subject'] = _('SMS confirmation');
217
218         $body = "$nickname: confirm you own this phone number with this code:";
219         $body .= "\n\n";
220         $body .= $code;
221         $body .= "\n\n";
222
223         mail_send($recipients, $headers, $body);
224 }
225
226
227 function mail_notify_nudge($from, $to) {
228     common_init_locale($to->language);
229         $subject = sprintf(_('You\'ve been nudged by %s'), $from->nickname);
230
231         $from_profile = $from->getProfile();
232
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".
235                                           "%3\$s\n\n".
236                                           "Don't reply to this email; it won't get to them.\n\n".
237                                           "With kind regards,\n".
238                                           "%4\$s\n"),
239                                         $from_profile->getBestName(),
240                                         $from->nickname,
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);
245 }
246
247
248
249 function mail_notify_message($message, $from=NULL, $to=NULL) {
250
251         if (is_null($from)) {
252                 $from = User::staticGet('id', $message->from_profile);
253         }
254
255         if (is_null($to)) {
256                 $to = User::staticGet('id', $message->to_profile);
257         }
258
259         if (is_null($to->email) || !$to->emailnotifymsg) {
260                 return true;
261         }
262
263     common_init_locale($to->language);
264         $subject = sprintf(_('New private message from %s'), $from->nickname);
265
266         $from_profile = $from->getProfile();
267
268         $body = sprintf(_("%1\$s (%2\$s) sent you a private message:\n\n".
269                                           "------------------------------------------------------\n".
270                                           "%3\$s\n".
271                                           "------------------------------------------------------\n\n".
272                                           "You can reply to their message here:\n\n".
273                                           "%4\$s\n\n".
274                                           "Don't reply to this email; it won't get to them.\n\n".
275                                           "With kind regards,\n".
276                                           "%5\$s\n"),
277                                         $from_profile->getBestName(),
278                                         $from->nickname,
279                                         $message->content,
280                                         common_local_url('newmessage', array('to' => $from->id)),
281                                         common_config('site', 'name'));
282
283     common_init_locale();
284         return mail_to_user($to, $subject, $body);
285 }
286
287 function mail_notify_fave($other, $user, $notice) {
288
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" .
295                                           "%3\$s\n\n" .
296                                           "You can see the list of %1\$s's favorites here:\n\n" .
297                                           "%4\$s\n\n" .
298                                           "Faithfully yours,\n" .
299                                           "%5\$s\n"),
300                                         $bestname,
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'));
305
306     common_init_locale();
307         mail_to_user($other, $subject, $body);
308 }