]>
git.wh0rd.org - tt-rss.git/blob - include/digest.php
3 * Send by mail a digest of last articles.
5 * @param mixed $link The database connection.
6 * @param integer $limit The maximum number of articles by digest.
7 * @return boolean Return false if digests are not enabled.
9 function send_headlines_digests($link, $debug = false) {
11 require_once 'lib/phpmailer/class.phpmailer.php';
13 $user_limit = 15; // amount of users to process (e.g. emails to send out)
14 $limit = 1000; // maximum amount of headlines to include
16 if ($debug) _debug("Sending digests, batch of max $user_limit users, headline limit = $limit");
18 if (DB_TYPE
== "pgsql") {
19 $interval_query = "last_digest_sent < NOW() - INTERVAL '1 days'";
20 } else if (DB_TYPE
== "mysql") {
21 $interval_query = "last_digest_sent < DATE_SUB(NOW(), INTERVAL 1 DAY)";
24 $result = db_query($link, "SELECT id,email FROM ttrss_users
25 WHERE email != '' AND (last_digest_sent IS NULL OR $interval_query)");
27 while ($line = db_fetch_assoc($result)) {
29 if (get_pref($link, 'DIGEST_ENABLE', $line['id'], false)) {
30 $preferred_ts = strtotime(get_pref($link, 'DIGEST_PREFERRED_TIME', $line['id'], '00:00'));
32 // try to send digests within 2 hours of preferred time
33 if ($preferred_ts && time() >= $preferred_ts &&
34 time() - $preferred_ts <= 7200) {
36 if ($debug) print "Sending digest for UID:" . $line['id'] . " - " . $line["email"] . " ... ";
38 $do_catchup = get_pref($link, 'DIGEST_CATCHUP', $line['id'], false);
42 // reset tz_offset global to prevent tz cache clash between users
45 $tuple = prepare_headlines_digest($link, $line["id"], 1, $limit);
47 $headlines_count = $tuple[1];
48 $affected_ids = $tuple[2];
49 $digest_text = $tuple[3];
51 if ($headlines_count > 0) {
53 $mail = new PHPMailer();
55 $mail->PluginDir
= "lib/phpmailer/";
56 $mail->SetLanguage("en", "lib/phpmailer/language/");
58 $mail->CharSet
= "UTF-8";
60 $mail->From
= SMTP_FROM_ADDRESS
;
61 $mail->FromName
= SMTP_FROM_NAME
;
62 $mail->AddAddress($line["email"], $line["login"]);
65 $mail->Host
= SMTP_HOST
;
66 $mail->Mailer
= "smtp";
67 $mail->SMTPAuth
= SMTP_LOGIN
!= '';
68 $mail->Username
= SMTP_LOGIN
;
69 $mail->Password
= SMTP_PASSWORD
;
73 $mail->Subject
= DIGEST_SUBJECT
;
74 $mail->Body
= $digest;
75 $mail->AltBody
= $digest_text;
79 if (!$rc && $debug) print "ERROR: " . $mail->ErrorInfo
;
81 if ($debug) print "RC=$rc\n";
83 if ($rc && $do_catchup) {
84 if ($debug) print "Marking affected articles as read...\n";
85 catchupArticlesById($link, $affected_ids, 0, $line["id"]);
88 if ($debug) print "No headlines\n";
91 db_query($link, "UPDATE ttrss_users SET last_digest_sent = NOW()
92 WHERE id = " . $line["id"]);
98 if ($debug) _debug("All done.");
102 function prepare_headlines_digest($link, $user_id, $days = 1, $limit = 1000) {
104 require_once "lib/MiniTemplator.class.php";
106 $tpl = new MiniTemplator
;
107 $tpl_t = new MiniTemplator
;
109 $tpl->readTemplateFromFile("templates/digest_template_html.txt");
110 $tpl_t->readTemplateFromFile("templates/digest_template.txt");
112 $user_tz_string = get_pref($link, 'USER_TIMEZONE', $user_id);
113 $local_ts = convert_timestamp(time(), 'UTC', $user_tz_string);
115 $tpl->setVariable('CUR_DATE', date('Y/m/d', $local_ts));
116 $tpl->setVariable('CUR_TIME', date('G:i', $local_ts));
118 $tpl_t->setVariable('CUR_DATE', date('Y/m/d', $local_ts));
119 $tpl_t->setVariable('CUR_TIME', date('G:i', $local_ts));
121 $affected_ids = array();
123 if (DB_TYPE
== "pgsql") {
124 $interval_query = "ttrss_entries.date_updated > NOW() - INTERVAL '$days days'";
125 } else if (DB_TYPE
== "mysql") {
126 $interval_query = "ttrss_entries.date_updated > DATE_SUB(NOW(), INTERVAL $days DAY)";
129 $result = db_query($link, "SELECT ttrss_entries.title,
130 ttrss_feeds.title AS feed_title,
131 COALESCE(ttrss_feed_categories.title, '".__('Uncategorized')."') AS cat_title,
133 ttrss_user_entries.ref_id,
137 ".SUBSTRING_FOR_DATE
."(last_updated,1,19) AS last_updated
139 ttrss_user_entries,ttrss_entries,ttrss_feeds
141 ttrss_feed_categories ON (cat_id = ttrss_feed_categories.id)
143 ref_id = ttrss_entries.id AND feed_id = ttrss_feeds.id
144 AND include_in_digest = true
146 AND ttrss_user_entries.owner_uid = $user_id
149 ORDER BY ttrss_feed_categories.title, ttrss_feeds.title, score DESC, date_updated DESC
152 $cur_feed_title = "";
154 $headlines_count = db_num_rows($result);
156 $headlines = array();
158 while ($line = db_fetch_assoc($result)) {
159 array_push($headlines, $line);
162 for ($i = 0; $i < sizeof($headlines); $i++
) {
164 $line = $headlines[$i];
166 array_push($affected_ids, $line["ref_id"]);
168 $updated = make_local_datetime($link, $line['last_updated'], false,
171 /* if ($line["score"] != 0) {
172 if ($line["score"] > 0) $line["score"] = '+' . $line["score"];
174 $line["title"] .= " (".$line['score'].")";
177 if (get_pref($link, 'ENABLE_FEED_CATS', $user_id)) {
178 $line['feed_title'] = $line['cat_title'] . " / " . $line['feed_title'];
181 $tpl->setVariable('FEED_TITLE', $line["feed_title"]);
182 $tpl->setVariable('ARTICLE_TITLE', $line["title"]);
183 $tpl->setVariable('ARTICLE_LINK', $line["link"]);
184 $tpl->setVariable('ARTICLE_UPDATED', $updated);
185 $tpl->setVariable('ARTICLE_EXCERPT',
186 truncate_string(strip_tags($line["content"]), 300));
187 // $tpl->setVariable('ARTICLE_CONTENT',
188 // strip_tags($article_content));
190 $tpl->addBlock('article');
192 $tpl_t->setVariable('FEED_TITLE', $line["feed_title"]);
193 $tpl_t->setVariable('ARTICLE_TITLE', $line["title"]);
194 $tpl_t->setVariable('ARTICLE_LINK', $line["link"]);
195 $tpl_t->setVariable('ARTICLE_UPDATED', $updated);
196 // $tpl_t->setVariable('ARTICLE_EXCERPT',
197 // truncate_string(strip_tags($line["excerpt"]), 100));
199 $tpl_t->addBlock('article');
201 if ($headlines[$i]['feed_title'] != $headlines[$i+
1]['feed_title']) {
202 $tpl->addBlock('feed');
203 $tpl_t->addBlock('feed');
208 $tpl->addBlock('digest');
209 $tpl->generateOutputToString($tmp);
211 $tpl_t->addBlock('digest');
212 $tpl_t->generateOutputToString($tmp_t);
214 return array($tmp, $headlines_count, $affected_ids, $tmp_t);