Facebook to email

Facebook to email Git Source Tree

Root/modules/email.php

1<?php
2/**
3 * Copyright 2014 Grégory Soutadé
4 *
5 * This file is part of f2email.
6 *
7 * f2email is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * f2email is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with f2email. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21function startsWith($haystack, $needle)
22{
23 return $needle === "" || strpos($haystack, $needle) === 0;
24}
25
26class Email extends Module
27{
28 function getName() { return "Email"; }
29 function getDescription() { return "Sends an email for each new notification"; }
30 function getHelp() { return "Global preference is the default email to send to (if a local one has not been specified).<br/>\nYou can use multiples emails addresses separated by a comma or a semicolon (\",\" ot \";\")";}
31 function hasGlobalPref() { return 1; }
32 function hasLocalPref() { return 1; }
33
34 function __construct($db, $user)
35 {
36 parent::__construct($db, $user);
37
38 if ($this->global_pref == "" && array_key_exists("email", $user))
39 $this->setGlobalPref($user["email"], true);
40 }
41
42 function validatePref($pref)
43 {
44 foreach(preg_split("/[,;]/", $pref) as $email)
45 {
46 if (!filter_var(trim($email), FILTER_VALIDATE_EMAIL))
47 return false;
48 }
49
50 return true ;
51 }
52
53 function manageNotification($fb_object, $notification)
54 {
55 $local_pref = $this->getLocalPref($fb_object["id"]);
56 if ($local_pref != "")
57 $to = $local_pref;
58 else
59 $to = $this->global_pref;
60
61 switch($notification["type"])
62 {
63 case "status": $subject = "[" . $fb_object["name"] . "] Status update"; break;
64 case "message":
65 case "photo":
66 case "video":
67 case "link": $subject = "[" . $fb_object["name"] . "] New " . $notification["type"]; break;
68 default:
69 $subject = "[" . $fb_object["name"] . "] New event " . $fb_object["name"]; break;
70 }
71
72 $message = '<html><body>From <a href="https://www.facebook.com/' . $notification["from"]["id"] . '">' . $notification["from"]["name"] . "</a> :<br/><br/>";
73
74 if (array_key_exists("picture", $notification))
75 $message .= '<img src="' . str_replace("_s", "_n", $notification["picture"]) . '"/><br/>';
76 if (array_key_exists("caption", $notification))
77 $message .= $notification["caption"] . "<br/>";
78
79 if (array_key_exists("message", $notification))
80 $message .= "<br/>" . str_replace("\n", "<br/>", $notification["message"]) . "<br/>";
81 else if (array_key_exists("story", $notification))
82 $message .= "<br/>" . str_replace("\n", "<br/>", $notification["story"]) . "<br/>";
83
84 if (array_key_exists("link", $notification))
85 if (startsWith($notification["link"], "http"))
86 $message .= '<br/><a href="' . $notification["link"] . '">' . $notification["link"] . "</a>";
87 else
88 $message .= '<br/><a href="https://www.facebook.com/' . $notification["link"] . '">https://www.facebook.com/' . $notification["link"] . "</a>";
89 else
90 $message .= '<br/><a href="https://www.facebook.com/' . $notification["id"] . '">https://www.facebook.com/' . $notification["id"] . "</a>";
91
92 $message .= "</body></html>";
93
94 $headers = "Message-ID: <" . $notification['id'] . "@f2email>";
95
96 foreach(preg_split("/[,;]/", $to) as $email)
97 send_email($email, $subject, $message, $headers);
98 }
99}
100?>

Archive Download this file

Branches