Overview
Comment:Add ability to re-subscribe an unsubscribed address
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | emails
Files: files | file ages | folders
SHA3-256: f81589e26c068c0269619780ab953facec46b2fe5be4b59127ab2aaded9113c4
User & Date: bohwaz on 2022-05-30 16:18:14
Other Links: branch diff | manifest | tags
Context
2022-05-30
20:51
Move emails queue run to a separate task check-in: 922fd4f0a6 user: bohwaz tags: emails
16:18
Add ability to re-subscribe an unsubscribed address check-in: f81589e26c user: bohwaz tags: emails
14:12
Fix missing commit Sync file with schema.sql check-in: 17e017e227 user: bohwaz tags: emails
Changes

Modified src/include/data/1.1.0_schema.sql from [9f8fb4da93] to [89a0b562ed].

383
384
385
386
387
388
389

390
391
392
393
394
395
396
397
398
399
400
401
402

403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420

CREATE TABLE IF NOT EXISTS compromised_passwords_cache_ranges
-- Cache des préfixes de mots de passe compromis
(
    prefix TEXT NOT NULL PRIMARY KEY,
    date INTEGER NOT NULL
);

CREATE TABLE emails (
-- List of emails addresses
-- We are not storing actual email addresses here for privacy reasons
-- So that we can keep the record (for opt-out reasons) even when the
-- email address has been removed from the users table
	id INTEGER NOT NULL PRIMARY KEY,
    hash TEXT NOT NULL,
    verified INTEGER NOT NULL DEFAULT 0,
    optout INTEGER NOT NULL DEFAULT 0,
    invalid INTEGER NOT NULL DEFAULT 0,
    fail_count INTEGER NOT NULL DEFAULT 0,
    sent_count INTEGER NOT NULL DEFAULT 0,
    fail_log TEXT NULL,

    added TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP
);

CREATE UNIQUE INDEX emails_hash ON emails (hash);

CREATE TABLE emails_queue (
-- List of emails waiting to be sent
    id INTEGER NOT NULL PRIMARY KEY,
    sender TEXT NULL,
    recipient TEXT NOT NULL,
    recipient_hash TEXT NOT NULL,
    subject TEXT NOT NULL,
    content TEXT NOT NULL,
    content_html TEXT NULL,
    sending INTEGER NOT NULL DEFAULT 0, -- Will be changed to 1 when the queue run will start
    sending_started TEXT NULL, -- Will be filled with the datetime when the email sending was started
    context INTEGER NOT NULL
);







>
|












>



|

|












383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422

CREATE TABLE IF NOT EXISTS compromised_passwords_cache_ranges
-- Cache des préfixes de mots de passe compromis
(
    prefix TEXT NOT NULL PRIMARY KEY,
    date INTEGER NOT NULL
);

CREATE TABLE IF NOT EXISTS emails (
-- List of emails addresses
-- We are not storing actual email addresses here for privacy reasons
-- So that we can keep the record (for opt-out reasons) even when the
-- email address has been removed from the users table
	id INTEGER NOT NULL PRIMARY KEY,
    hash TEXT NOT NULL,
    verified INTEGER NOT NULL DEFAULT 0,
    optout INTEGER NOT NULL DEFAULT 0,
    invalid INTEGER NOT NULL DEFAULT 0,
    fail_count INTEGER NOT NULL DEFAULT 0,
    sent_count INTEGER NOT NULL DEFAULT 0,
    fail_log TEXT NULL,
    last_sent TEXT NULL,
    added TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP
);

CREATE UNIQUE INDEX IF NOT EXISTS emails_hash ON emails (hash);

CREATE TABLE IF NOT EXISTS emails_queue (
-- List of emails waiting to be sent
    id INTEGER NOT NULL PRIMARY KEY,
    sender TEXT NULL,
    recipient TEXT NOT NULL,
    recipient_hash TEXT NOT NULL,
    subject TEXT NOT NULL,
    content TEXT NOT NULL,
    content_html TEXT NULL,
    sending INTEGER NOT NULL DEFAULT 0, -- Will be changed to 1 when the queue run will start
    sending_started TEXT NULL, -- Will be filled with the datetime when the email sending was started
    context INTEGER NOT NULL
);

Modified src/include/data/schema.sql from [9f8fb4da93] to [89a0b562ed].

383
384
385
386
387
388
389

390
391
392
393
394
395
396
397
398
399
400
401
402

403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420

CREATE TABLE IF NOT EXISTS compromised_passwords_cache_ranges
-- Cache des préfixes de mots de passe compromis
(
    prefix TEXT NOT NULL PRIMARY KEY,
    date INTEGER NOT NULL
);

CREATE TABLE emails (
-- List of emails addresses
-- We are not storing actual email addresses here for privacy reasons
-- So that we can keep the record (for opt-out reasons) even when the
-- email address has been removed from the users table
	id INTEGER NOT NULL PRIMARY KEY,
    hash TEXT NOT NULL,
    verified INTEGER NOT NULL DEFAULT 0,
    optout INTEGER NOT NULL DEFAULT 0,
    invalid INTEGER NOT NULL DEFAULT 0,
    fail_count INTEGER NOT NULL DEFAULT 0,
    sent_count INTEGER NOT NULL DEFAULT 0,
    fail_log TEXT NULL,

    added TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP
);

CREATE UNIQUE INDEX emails_hash ON emails (hash);

CREATE TABLE emails_queue (
-- List of emails waiting to be sent
    id INTEGER NOT NULL PRIMARY KEY,
    sender TEXT NULL,
    recipient TEXT NOT NULL,
    recipient_hash TEXT NOT NULL,
    subject TEXT NOT NULL,
    content TEXT NOT NULL,
    content_html TEXT NULL,
    sending INTEGER NOT NULL DEFAULT 0, -- Will be changed to 1 when the queue run will start
    sending_started TEXT NULL, -- Will be filled with the datetime when the email sending was started
    context INTEGER NOT NULL
);







>
|












>



|

|












383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422

CREATE TABLE IF NOT EXISTS compromised_passwords_cache_ranges
-- Cache des préfixes de mots de passe compromis
(
    prefix TEXT NOT NULL PRIMARY KEY,
    date INTEGER NOT NULL
);

CREATE TABLE IF NOT EXISTS emails (
-- List of emails addresses
-- We are not storing actual email addresses here for privacy reasons
-- So that we can keep the record (for opt-out reasons) even when the
-- email address has been removed from the users table
	id INTEGER NOT NULL PRIMARY KEY,
    hash TEXT NOT NULL,
    verified INTEGER NOT NULL DEFAULT 0,
    optout INTEGER NOT NULL DEFAULT 0,
    invalid INTEGER NOT NULL DEFAULT 0,
    fail_count INTEGER NOT NULL DEFAULT 0,
    sent_count INTEGER NOT NULL DEFAULT 0,
    fail_log TEXT NULL,
    last_sent TEXT NULL,
    added TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP
);

CREATE UNIQUE INDEX IF NOT EXISTS emails_hash ON emails (hash);

CREATE TABLE IF NOT EXISTS emails_queue (
-- List of emails waiting to be sent
    id INTEGER NOT NULL PRIMARY KEY,
    sender TEXT NULL,
    recipient TEXT NOT NULL,
    recipient_hash TEXT NOT NULL,
    subject TEXT NOT NULL,
    content TEXT NOT NULL,
    content_html TEXT NULL,
    sending INTEGER NOT NULL DEFAULT 0, -- Will be changed to 1 when the queue run will start
    sending_started TEXT NULL, -- Will be filled with the datetime when the email sending was started
    context INTEGER NOT NULL
);

Modified src/include/lib/Garradin/Entities/Users/Email.php from [bd3d1e1b08] to [26439d5e99].

1
2
3
4
5
6
7
8


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

32
33
34
35
36
37
38
<?php
declare(strict_types=1);

namespace Garradin\Entities\Users;

use Garradin\Entity;
use Garradin\UserException;
use Garradin\Users\Emails;



use const Garradin\{WWW_URL, SECRET_KEY};

class Email extends Entity
{
	const TABLE = 'emails';

	/**
	 * Antispam services that require to do a manual action to accept emails
	 */
	const BLACKLIST_MANUAL_VALIDATION_MX = '/mailinblack\.com|spamenmoins\.com/';

	const COMMON_DOMAINS = ['laposte.net', 'gmail.com', 'hotmail.fr', 'hotmail.com', 'wanadoo.fr', 'free.fr', 'sfr.fr', 'yahoo.fr', 'orange.fr', 'live.fr', 'outlook.fr', 'yahoo.com', 'neuf.fr', 'outlook.com', 'icloud.com', 'riseup.net', 'vivaldi.net', 'aol.com', 'gmx.de', 'lilo.org', 'mailo.com', 'protonmail.com'];

	protected int $id;
	protected string $hash;
	protected bool $verified;
	protected bool $optout;
	protected bool $invalid;
	protected int $sent_count;
	protected int $fail_count;
	protected ?string $fail_log;
	protected \DateTime $added;


	/**
	 * Normalize email address and create a hash from this
	 */
	static public function getHash(string $email): string
	{
		$email = strtolower(trim($email));








>
>
















|
|
|
|
|


>







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<?php
declare(strict_types=1);

namespace Garradin\Entities\Users;

use Garradin\Entity;
use Garradin\UserException;
use Garradin\Users\Emails;

use KD2\SMTP;

use const Garradin\{WWW_URL, SECRET_KEY};

class Email extends Entity
{
	const TABLE = 'emails';

	/**
	 * Antispam services that require to do a manual action to accept emails
	 */
	const BLACKLIST_MANUAL_VALIDATION_MX = '/mailinblack\.com|spamenmoins\.com/';

	const COMMON_DOMAINS = ['laposte.net', 'gmail.com', 'hotmail.fr', 'hotmail.com', 'wanadoo.fr', 'free.fr', 'sfr.fr', 'yahoo.fr', 'orange.fr', 'live.fr', 'outlook.fr', 'yahoo.com', 'neuf.fr', 'outlook.com', 'icloud.com', 'riseup.net', 'vivaldi.net', 'aol.com', 'gmx.de', 'lilo.org', 'mailo.com', 'protonmail.com'];

	protected int $id;
	protected string $hash;
	protected bool $verified = false;
	protected bool $optout = false;
	protected bool $invalid = false;
	protected int $sent_count = 0;
	protected int $fail_count = 0;
	protected ?string $fail_log;
	protected \DateTime $added;
	protected ?\DateTime $last_sent;

	/**
	 * Normalize email address and create a hash from this
	 */
	static public function getHash(string $email): string
	{
		$email = strtolower(trim($email));
82
83
84
85
86
87
88
89

















90
91
92
93
94
95
96
		$this->set('verified', true);
		$this->set('optout', false);
		$this->set('invalid', false);
		$this->set('fail_count', 0);
		return true;
	}

	static public function validate(string $email): void

















	{
		$user = strtok($email, '@');
		$host = strtok('');

		// Ce domaine n'existe pas (MX inexistant), erreur de saisie courante
		if ($host == 'gmail.fr') {
			throw new UserException('L\'adresse e-mail est invalide : est-ce que vous avez voulu écrire "gmail.com" ?');







|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
		$this->set('verified', true);
		$this->set('optout', false);
		$this->set('invalid', false);
		$this->set('fail_count', 0);
		return true;
	}

	public function validate(string $email): bool
	{
		if (!$this->canSend()) {
			return false;
		}

		try {
			self::validateAddress($email);
		}
		catch (UserException $e) {
			$this->hasFailed(['type' => 'permanent', 'message' => $e->getMessage()]);
			return false;
		}

		return true;
	}

	static public function validateAddress(string $email): void
	{
		$user = strtok($email, '@');
		$host = strtok('');

		// Ce domaine n'existe pas (MX inexistant), erreur de saisie courante
		if ($host == 'gmail.fr') {
			throw new UserException('L\'adresse e-mail est invalide : est-ce que vous avez voulu écrire "gmail.com" ?');
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156

			throw new UserException('Adresse e-mail invalide : vérifiez que vous n\'avez pas fait une faute de frappe.');
		}

		getmxrr($host, $mx_list);

		if (!count($mx_list)) {
			throw new UserException('Adresse e-mail invalide : vérifiez que vous n\'avez pas fait une faute de frappe.');
		}

		$mx_list = array_filter($mx_list,
  			fn ($mx) => !preg_match(self::BLACKLIST_MANUAL_VALIDATION_MX, $mx)
  		);

		if (!count($mx_list)) {
			throw new UserException('Adresse e-mail invalide : impossible d\'envoyer des mails à un service (de type mailinblack ou spamenmoins) qui demande une validation manuelle de l\'expéditeur. Merci de choisir une autre adresse e-mail.');
		}
	}

	public function canSend(): bool
	{
		if (!$this->verified) {
			return false;
		}

		if ($this->optout) {
			return false;
		}

		if ($this->invalid) {
			return false;
		}

		if ($this->hasReachedFailLimit()) {
			return false;
		}

		return true;
	}

	public function hasReachedFailLimit(): bool
	{
		return ($this->fail_count >= Emails::FAIL_LIMIT);
	}

	public function incrementSentCount(): void
	{
		$this->set('sent_count', $this->sent_count+1);
	}








|













|



<
<
<
<
|












|







127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151




152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172

			throw new UserException('Adresse e-mail invalide : vérifiez que vous n\'avez pas fait une faute de frappe.');
		}

		getmxrr($host, $mx_list);

		if (!count($mx_list)) {
			throw new UserException('Adresse e-mail invalide (le domaine indiqué n\'a pas de service e-mail) : vérifiez que vous n\'avez pas fait une faute de frappe.');
		}

		$mx_list = array_filter($mx_list,
  			fn ($mx) => !preg_match(self::BLACKLIST_MANUAL_VALIDATION_MX, $mx)
  		);

		if (!count($mx_list)) {
			throw new UserException('Adresse e-mail invalide : impossible d\'envoyer des mails à un service (de type mailinblack ou spamenmoins) qui demande une validation manuelle de l\'expéditeur. Merci de choisir une autre adresse e-mail.');
		}
	}

	public function canSend(): bool
	{
		if (!empty($this->optout)) {
			return false;
		}





		if (!empty($this->invalid)) {
			return false;
		}

		if ($this->hasReachedFailLimit()) {
			return false;
		}

		return true;
	}

	public function hasReachedFailLimit(): bool
	{
		return !empty($this->fail_count) && ($this->fail_count >= Emails::FAIL_LIMIT);
	}

	public function incrementSentCount(): void
	{
		$this->set('sent_count', $this->sent_count+1);
	}

Modified src/include/lib/Garradin/Form.php from [7d0dc0a0a9] to [f8d6d1ac6f].

36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
		}

		try {
			call_user_func($fn);

			if (null !== $redirect) {
				if (array_key_exists('_dialog', $_GET)) {
					Utils::reloadParentFrame();
				}

				Utils::redirect($redirect);
			}

			return true;
		}







|







36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
		}

		try {
			call_user_func($fn);

			if (null !== $redirect) {
				if (array_key_exists('_dialog', $_GET)) {
					Utils::reloadParentFrame($redirect);
				}

				Utils::redirect($redirect);
			}

			return true;
		}

Modified src/include/lib/Garradin/Users/Emails.php from [6c6b6f4e31] to [5b79111d94].

1
2
3
4
5
6

7
8
9
10
11
12
13
<?php

namespace Garradin\Users;

use Garradin\Config;
use Garradin\DB;

use Garradin\Plugin;
use Garradin\Entities\Users\Email;

use const Garradin\{USE_CRON};
use const Garradin\{SMTP_HOST, SMTP_PORT, SMTP_USER, SMTP_PASSWORD, SMTP_SECURITY};

use KD2\SMTP;






>







1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?php

namespace Garradin\Users;

use Garradin\Config;
use Garradin\DB;
use Garradin\DynamicList;
use Garradin\Plugin;
use Garradin\Entities\Users\Email;

use const Garradin\{USE_CRON};
use const Garradin\{SMTP_HOST, SMTP_PORT, SMTP_USER, SMTP_PASSWORD, SMTP_SECURITY};

use KD2\SMTP;
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
			VALUES (:sender, :subject, :recipient, :content, :content_html, :context);');

		$st->bindValue(':sender', $sender);
		$st->bindValue(':subject', $subject);
		$st->bindValue(':context', $context);

		foreach ($recipients as $to => $variables) {
			// Ignore obviously invalid emails here
			if (self::checkAddress($to)) {
				self::markAddressAsInvalid($to);
				continue;
			}

			// We won't try to reject invalid/optout recipients here,
			// it's done in the queue clearing (more efficient)
			$hash = Email::getHash($to);

			$content = $template->fetch($variables);
			$content_html = $template_html ? $template_html->fetch($variables) : null;








<
<
<
<
<
<







47
48
49
50
51
52
53






54
55
56
57
58
59
60
			VALUES (:sender, :subject, :recipient, :content, :content_html, :context);');

		$st->bindValue(':sender', $sender);
		$st->bindValue(':subject', $subject);
		$st->bindValue(':context', $context);

		foreach ($recipients as $to => $variables) {






			// We won't try to reject invalid/optout recipients here,
			// it's done in the queue clearing (more efficient)
			$hash = Email::getHash($to);

			$content = $template->fetch($variables);
			$content_html = $template_html ? $template_html->fetch($variables) : null;

97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
		$recipients = array_unique($recipients);

		$db = DB::getInstance();
		$st = $db->prepare('INSERT INTO emails_queue (sender, subject, recipient, recipient_hash, content, context)
			VALUES (:sender, :subject, :recipient, :recipient_hash, :content, :context);');

		foreach ($recipients as $to) {
			// Ignore obviously invalid emails here
			if (!self::checkAddress($to)) {
				self::markAddressAsInvalid($to);
				continue;
			}

			// We won't try to reject invalid/optout recipients here,
			// it's done in the queue clearing (more efficient)
			$hash = Email::getHash($to);

			$st->bindValue(':sender', $sender);
			$st->bindValue(':subject', $subject);
			$st->bindValue(':context', $context);







<
<
<
<
<
<







92
93
94
95
96
97
98






99
100
101
102
103
104
105
		$recipients = array_unique($recipients);

		$db = DB::getInstance();
		$st = $db->prepare('INSERT INTO emails_queue (sender, subject, recipient, recipient_hash, content, context)
			VALUES (:sender, :subject, :recipient, :recipient_hash, :content, :context);');

		foreach ($recipients as $to) {






			// We won't try to reject invalid/optout recipients here,
			// it's done in the queue clearing (more efficient)
			$hash = Email::getHash($to);

			$st->bindValue(':sender', $sender);
			$st->bindValue(':subject', $subject);
			$st->bindValue(':context', $context);
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177

178
179
180

181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199











200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216

217
218
219
220
221
222
223
224
	}

	static public function getEmail(string $address): ?Email
	{
		return EM::findOne(Email::class, 'SELECT * FROM @TABLE WHERE hash = ?;', Email::getHash($address));
	}

	/**
	 * Vérifie qu'une adresse est valide
	 * @param  string $address
	 * @return boolean FALSE si l'adresse est invalide (syntaxe)
	 */
	static public function checkAddress(string $address): bool
	{
		$address = strtolower(trim($address));

		// Ce domaine n'existe pas (MX inexistant), erreur de saisie courante
		if (false !== stristr($address, '@gmail.fr'))
		{
			return false;
		}


		if (!SMTP::checkEmailIsValid($address, true))
		{
			return false;

		}

		return true;
	}

	static public function runQueue()
	{
		$db = DB::getInstance();

		$queue = self::listQueueAndMarkAsSending();
		$ids = [];

		// listQueue nettoie déjà la queue
		foreach ($queue as $row) {
			// Don't send emails to opt-out address, unless it's a password reminder
			if ($row->context != self::CONTEXT_SYSTEM && $row->optout) {
				self::deleteFromQueue($row->id);
				continue;
			}












			$headers = [
				'From' => $row->sender,
				'To' => $row->recipient,
				'Subject' => $row->subject,
			];

			self::send($row->context, $row->recipient_hash, $headers, $row->content, $row->content_html);
			$ids[] = $row->id;
		}

		// Update emails list and send count
		// then delete messages from queue
		$db->exec(sprintf('
		BEGIN;
			UPDATE emails_queue SET sending = 2 WHERE %s;
			INSERT OR IGNORE INTO %s (hash) SELECT recipient_hash FROM emails_queue WHERE sending = 2;

			UPDATE %2$s SET sent_count = sent_count + 1 WHERE hash IN (SELECT recipient_hash FROM emails_queue WHERE sending = 2);
			DELETE FROM emails_queue WHERE sending = 2;
		END;', $db->where('id', $ids), Email::TABLE));
	}

	/**
	 * Liste la file d'attente et marque les éléments listés comme "en cours de traitement"
	 * @return array







<
<
<
<
<
|

|

<
|
<
<
<
|
>
|
<
|
>


|
















>
>
>
>
>
>
>
>
>
>
>

















>
|







145
146
147
148
149
150
151





152
153
154
155

156



157
158
159

160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
	}

	static public function getEmail(string $address): ?Email
	{
		return EM::findOne(Email::class, 'SELECT * FROM @TABLE WHERE hash = ?;', Email::getHash($address));
	}






	static public function getOrCreateEmail(string $address): Email
	{
		$e = self::getEmail($address);


		if (!$e) {



			$e = new Email;
			$e->added = new \DateTime;
			$e->hash = $e::getHash($address);

			$e->validate($address);
			$e->save();
		}

		return $e;
	}

	static public function runQueue()
	{
		$db = DB::getInstance();

		$queue = self::listQueueAndMarkAsSending();
		$ids = [];

		// listQueue nettoie déjà la queue
		foreach ($queue as $row) {
			// Don't send emails to opt-out address, unless it's a password reminder
			if ($row->context != self::CONTEXT_SYSTEM && $row->optout) {
				self::deleteFromQueue($row->id);
				continue;
			}

			// Create email address in database
			if (!$row->email_hash) {
				$email = self::getOrCreateEmail($row->recipient);

				if (!$email->canSend()) {
					// Email address is invalid, skip
					self::deleteFromQueue($row->id);
					continue;
				}
			}

			$headers = [
				'From' => $row->sender,
				'To' => $row->recipient,
				'Subject' => $row->subject,
			];

			self::send($row->context, $row->recipient_hash, $headers, $row->content, $row->content_html);
			$ids[] = $row->id;
		}

		// Update emails list and send count
		// then delete messages from queue
		$db->exec(sprintf('
		BEGIN;
			UPDATE emails_queue SET sending = 2 WHERE %s;
			INSERT OR IGNORE INTO %s (hash) SELECT recipient_hash FROM emails_queue WHERE sending = 2;
			UPDATE %2$s SET sent_count = sent_count + 1, last_sent = datetime()
				WHERE hash IN (SELECT recipient_hash FROM emails_queue WHERE sending = 2);
			DELETE FROM emails_queue WHERE sending = 2;
		END;', $db->where('id', $ids), Email::TABLE));
	}

	/**
	 * Liste la file d'attente et marque les éléments listés comme "en cours de traitement"
	 * @return array
252
253
254
255
256
257
258
259
260
261
262
263
264





265
266

267





268

269











270
271

272













273


274




275
276
277
278
279
280
281
282
	 * @return array
	 */
	static protected function listQueue(): array
	{
		// Nettoyage de la queue déjà
		self::purgeQueueFromRejected();
		self::resetFailed();
		return DB::getInstance()->get('SELECT q.*, e.optout, e.verified
			FROM emails_queue q
			LEFT JOIN emails e ON e.hash = q.recipient_hash
			WHERE q.sending = 0;');
	}






	static public function listRejectedUsers(): array
	{

		$sql = sprintf('SELECT e.*, u.email, u.id, u.%s AS identity





			FROMS emails e

			LEFT JOIN membres u ON u.email IS NOT NULL AND u.email != \'\' AND e.hash = email_hash(u.email)











			WHERE e.optout = 1 OR e.invalid = 1 OR e.fail_count >= %d;',
			$id_field,

			self::FAIL_LIMIT













		);







		return DB::getInstance()->get($sql);
	}

	/**
	 * Supprime de la queue les messages liés à des adresses invalides
	 * ou qui ne souhaitent plus recevoir de message
	 * @return boolean
	 */







|





>
>
>
>
>
|

>
|
>
>
>
>
>
|
>
|
>
>
>
>
>
>
>
>
>
>
>
|
|
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
|
>
>

>
>
>
>
|







245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
	 * @return array
	 */
	static protected function listQueue(): array
	{
		// Nettoyage de la queue déjà
		self::purgeQueueFromRejected();
		self::resetFailed();
		return DB::getInstance()->get('SELECT q.*, e.optout, e.verified, e.hash AS email_hash
			FROM emails_queue q
			LEFT JOIN emails e ON e.hash = q.recipient_hash
			WHERE q.sending = 0;');
	}

	static public function countQueue(): int
	{
		return DB::getInstance()->count('emails_queue');
	}

	static public function listRejectedUsers(): DynamicList
	{
		$db = DB::getInstance();

		$columns = [
			'identity' => [
				'label' => 'Membre',
				'select' => 'u.' . $db->quoteIdentifier(Config::getInstance()->champ_identite),
			],
			'email' => [
				'label' => 'Adresse',
				'select' => 'u.email',
			],
			'user_id' => [
				'select' => 'u.id',
			],
			'hash' => [
			],
			'status' => [
				'label' => 'Statut',
				'select' => sprintf('CASE
					WHEN e.optout = 1 THEN \'Désinscription\'
					WHEN e.invalid = 1 THEN \'Invalide\'
					WHEN e.fail_count >= %d THEN \'Trop de tentatives\'
					WHEN e.verified = 1 THEN \'Vérifiée\'
					ELSE \'\'
					END', self::FAIL_LIMIT),
			],
			'sent_count' => [
				'label' => 'Messages envoyés',
			],
			'fail_log' => [
				'label' => 'Journal d\'erreurs',
			],
			'last_sent' => [
				'label' => 'Dernière tentative d\'envoi',
			],
			'optout' => [],
			'fail_count' => [],
		];

		$tables = 'emails e
			INNER JOIN membres u ON u.email IS NOT NULL AND u.email != \'\' AND e.hash = email_hash(u.email)';

		$conditions = sprintf('e.optout = 1 OR e.invalid = 1 OR e.fail_count >= %d', self::FAIL_LIMIT);

		$list = new DynamicList($columns, $tables, $conditions);
		$list->orderBy('last_sent', true);
		return $list;
	}

	/**
	 * Supprime de la queue les messages liés à des adresses invalides
	 * ou qui ne souhaitent plus recevoir de message
	 * @return boolean
	 */

Modified src/scripts/cron.php from [dbd2c4e5c7] to [02ee9d71ef].

1
2
3
4
5

6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25



26
<?php

namespace Garradin;

use Garradin\Services\Reminders;


if (PHP_SAPI != 'cli') {
	die("Wrong call");
}

require_once __DIR__ . '/../include/init.php';

// Exécution des tâches automatiques

$config = Config::getInstance();

if (ENABLE_AUTOMATIC_BACKUPS && $config->get('frequence_sauvegardes') && $config->get('nombre_sauvegardes'))
{
	$s = new Sauvegarde;
	$s->auto();
}

// Exécution des rappels automatiques
Reminders::sendPending();




Plugin::fireSignal('cron');





>




















>
>
>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<?php

namespace Garradin;

use Garradin\Services\Reminders;
use Garradin\Users\Emails;

if (PHP_SAPI != 'cli') {
	die("Wrong call");
}

require_once __DIR__ . '/../include/init.php';

// Exécution des tâches automatiques

$config = Config::getInstance();

if (ENABLE_AUTOMATIC_BACKUPS && $config->get('frequence_sauvegardes') && $config->get('nombre_sauvegardes'))
{
	$s = new Sauvegarde;
	$s->auto();
}

// Exécution des rappels automatiques
Reminders::sendPending();

// Send messages in queue
Emails::runQueue();

Plugin::fireSignal('cron');

Added src/templates/admin/membres/emails.tpl version [d64ed6b92f].





































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
{include file="admin/_head.tpl" title="Adresses rejetées" current="membres/message"}

<nav class="tabs">
    <ul>
    	<li><a href="message_collectif.php">Envoyer</a></li>
    	<li class="current"><a href="emails.php">Adresses rejetées</a></li>
    </ul>
</nav>

{if isset($_GET['sent'])}
<p class="confirm block">
	Un message de demande de confirmation a bien été envoyé. Le destinataire doit désormais cliquer sur le lien dans ce message.
</p>
{/if}

<p class="help">
	{if !$queue_count}
		Il n'y a aucun message en attente d'envoi.
	{else}
		Il y a {$queue_count} messages dans la file d'attente, ils seront envoyés dans quelques instants.
	{/if}
</p>

{if !$list->count()}
	<p class="alert block">Aucune adresse e-mail n'a été rejetée pour le moment. Cette page présentera les adresses e-mail invalides ou qui ont demandé à se désinscrire.</p>
{else}
	{include file="common/dynamic_list_head.tpl"}

		{foreach from=$list->iterate() item="row"}
		<tr>
			<th><a href="fiche.php?id={$row.user_id}">{$row.identity}</a></th>
			<td>{$row.email}</td>
			<td>{$row.status}</td>
			<td class="num">{$row.sent_count}</td>
			<td>{$row.fail_log|escape|nl2br}</td>
			<td>{$row.last_sent|date}</td>
			<td>
				{if $row.email && ($row.optout || $row.fail_count)}
					{linkbutton target="_dialog" label="Vérifier l'adresse" href="?verify=%s"|args:$row.email shape="check"}
				{/if}
			</td>
		</tr>

		{/foreach}
	</tbody>
	</table>

	{pagination url=$list->paginationURL() page=$list.page bypage=$list.per_page total=$list->count()}

	<div class="block help">
		<h3>Statuts possibles d'une adresse e-mail&nbsp;:</h3>
		<dl class="cotisation">
			<dt>Vérifiée</dt>
			<dd>L'adresse a déjà reçu un message et a été vérifiée manuellement par le destinataire.</dd>
			<dt>Désinscription</dt>
			<dd>Le destinataire a demandé à être désinscrit et ne recevra plus de messages.</dd>
			<dt>Invalide</dt>
			<dd>L'adresse n'existe pas ou plus. Il n'est pas possible de lui envoyer des messages.</dd>
			<dt>Trop de tentatives</dt>
			<dd>Le service destinataire a répondu une erreur plus de {$max_fail_count} fois. Cela arrive par exemple si vos messages sont vus comme du spam trop souvent, ou si la boîte mail destinataire est pleine. Cette adresse ne recevra plus de message.</dd>
		</dl>
	</div>

{/if}

{include file="admin/_foot.tpl"}

Added src/templates/admin/membres/emails_verification.tpl version [ed88a61c4f].











































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
{include file="admin/_head.tpl" title="Vérification d'adresse" current="membres/message"}

<form method="post" action="{$self_url}">
	<fieldset>
		<legend>Demander la vérification de l'adresse</legend>
		<p class="help">
			Si le membre a cliqué par erreur sur le lien de désinscription, il est possible de rétablir l'envoi des messages.<br />
			Le membre recevra alors un message contenant un autre lien pour se réinscrire.
		</p>
		<p class="alert block">
			Attention, n'utiliser cette procédure qu'à la demande du membre.<br />
			Si vous essayez d'envoyer des messages à une adresse qui ne désire pas recevoir vos messages, vos messages aux autres membres pourront être bloqués par les serveurs destinataires.
		</p>
		<p class="submit">
			{csrf_field key=$csrf_key}
			{button type="submit" name="send" label="Envoyer un message de vérification" shape="right" class="main"}
		</p>
	</fieldset>
</form>

{include file="admin/_foot.tpl"}

Modified src/templates/admin/optout.tpl from [6f4c21faf2] to [7aa99a352c].

1
2
3
4
5
6
7
8
{include file="admin/_head.tpl" title="Désinscription"}

{if $verify === true}
	<p class="block confirm">
		Votre adresse e-mail a bien été vérifiée, merci !
	</p>
{elseif $verify === false}
	<p class="block error">
|







1
2
3
4
5
6
7
8
{include file="admin/_head.tpl" title="Désinscription" transparent=true}

{if $verify === true}
	<p class="block confirm">
		Votre adresse e-mail a bien été vérifiée, merci !
	</p>
{elseif $verify === false}
	<p class="block error">

Modified src/www/admin/membres/emails.php from [e4ad28add0] to [d7a452e64f].

1
2


3
4
5
6
7


















8

9




10
<?php
namespace Garradin;



require_once __DIR__ . '/_inc.php';

$session->requireAccess($session::SECTION_USERS, $session::ACCESS_WRITE);



















$tpl->assign('rejected', Categories::listRejectedUsers());






$tpl->display('admin/membres/emails.tpl');


>
>





>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
>

>
>
>
>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<?php
namespace Garradin;

use Garradin\Users\Emails;

require_once __DIR__ . '/_inc.php';

$session->requireAccess($session::SECTION_USERS, $session::ACCESS_WRITE);

if ($address = qg('verify')) {
    $email = Emails::getEmail($address);

    if (!$email) {
        throw new UserException('Adresse invalide');
    }

    $csrf_key = 'send_verification';

    $form->runIf('send', function () use ($email, $address) {
        $email->sendVerification($address);
    }, $csrf_key, '!membres/emails.php?sent');

    $tpl->assign(compact('csrf_key'));
    $tpl->display('admin/membres/emails_verification.tpl');
    exit;
}

$list = Emails::listRejectedUsers();
$list->loadFromQueryString();

$max_fail_count = Emails::FAIL_LIMIT;
$queue_count = Emails::countQueue();
$tpl->assign(compact('list', 'max_fail_count', 'queue_count'));

$tpl->display('admin/membres/emails.tpl');