-
Notifications
You must be signed in to change notification settings - Fork 193
/
cli-sendmail.php
183 lines (144 loc) · 5.17 KB
/
cli-sendmail.php
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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
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
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
<?php
/*
* Copyright (C) 2012 Platoniq y Fundación Goteo (see README for details)
* This file is part of Goteo.
*
* Goteo is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Goteo is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Goteo. If not, see <http://www.gnu.org/licenses/agpl.txt>.
*
*/
/**
* Este es el proceso que envia un email al usuario especificado
* version linea de comandos
**/
if (PHP_SAPI !== 'cli') {
die("Acceso solo por linea de comandos!");
}
error_reporting(E_ALL & ~E_NOTICE & ~E_STRICT);
ini_set("display_errors",1);
//system timezone
date_default_timezone_set("Europe/Madrid");
use Goteo\Core\Resource,
Goteo\Core\Error,
Goteo\Core\Redirection,
Goteo\Core\Model,
Goteo\Library\Feed,
Goteo\Library\Mail,
Goteo\Library\Sender;
require_once 'config.php';
require_once 'core/common.php';
// Autoloader
spl_autoload_register(
function ($cls) {
$file = __DIR__ . '/' . implode('/', explode('\\', strtolower(substr($cls, 6)))) . '.php';
$file = realpath($file);
if ($file === false) {
// Try in library
$file = __DIR__ . '/library/' . strtolower($cls) . '.php';
}
if ($file !== false) {
include $file;
}
}
);
// set Lang
define('LANG', 'es');
$debug = true;
$id = $argv[1];
if(empty($id)) {
die("Se necesita un identificador de sender como argumento del script!");
}
$list = array();
$sql = "SELECT
mailer_send.id,
mailer_send.user,
mailer_send.name,
mailer_send.email,
mailer_content.id as mailing_id
FROM mailer_send
RIGHT JOIN mailer_content ON mailer_content.id=mailer_send.mailing AND mailer_content.active=1
WHERE mailer_send.id = ?
AND mailer_send.sended IS NULL
AND mailer_send.blocked IS NULL
";
if ($query = Model::query($sql, array($id))) {
$user = $query->fetchObject();
}
if(!is_object($user)) {
die("No se ha encontrado un usuario válido para el mailer_send.id=$id\n");
}
//si estamos aqui sabemos que el usuari es valido i el mailing tambien
if($debug) echo "dbg: Fecha inicio " .date("Y-m-d H:i:s"). "\n";
// cogemos el siguiente envío a tratar
$mailing = Sender::getSending($user->mailing_id);
// print_r($mailing);
// si no está activa fin
if (!$mailing->active) {
die("Mailing {$user->mailing_id} inactivo!\n");
}
// cogemos el contenido y la plantilla desde el historial
$query = Model::query('SELECT html, template FROM mail WHERE id = ?', array($mailing->mail));
$data = $query->fetch(\PDO::FETCH_ASSOC);
$content = $data['html'];
$template = $data['template'];
if (empty($content)) {
die("Mailing {$user->mailing_id} sin contenido!\n");
}
if($debug) echo "dbg: Bloqueando registro {$user->id} ({$user->email}) mailing: {$user->mailing_id}\n";
//bloquear usuario
Model::query("UPDATE mailer_send SET blocked = 1 WHERE id = '{$user->id}' AND mailing = '{$user->mailing_id}'");
//enviar email
$itime = microtime(true);
try {
$mailHandler = new Mail($debug);
// reply, si es especial
if (!empty($mailing->reply)) {
$mailHandler->reply = $mailing->reply;
if (!empty($mailing->reply_name)) {
$mailHandler->replyName = $mailing->reply_name;
}
}
$mailHandler->to = \trim($user->email);
$mailHandler->toName = $user->name;
$mailHandler->subject = $mailing->subject;
$mailHandler->content = str_replace(
array('%USERID%', '%USEREMAIL%', '%USERNAME%'),
array($user->user, $user->email, $user->name),
$content);
$mailHandler->html = true;
$mailHandler->template = $template;
$mailHandler->massive = true;
$errors = array();
if ($mailHandler->send($errors)) {
// Envio correcto
Model::query("UPDATE mailer_send SET sended = 1, datetime = NOW() WHERE id = '{$user->id}' AND mailing = '{$user->mailing_id}'");
if ($debug) echo "dbg: Enviado OK a $user->email\n";
} else {
// falló al enviar
$sql = "UPDATE mailer_send
SET sended = 0 , error = ? , datetime = NOW()
WHERE id = '{$user->id}' AND mailing = '{$user->mailing_id}'
";
Model::query($sql, array(implode(',', $errors)));
if ($debug) echo "dbg: Fallo ERROR a {$user->email} ".implode(',', $errors)."\n";
}
unset($mailHandler);
// tiempo de ejecución
$now = (microtime(true) - $itime);
if ($debug) echo "dbg: Tiempo de envio: $now segundos\n";
} catch (phpmailerException $e) {
die ($e->errorMessage());
}
//desbloquear usuario
if($debug) echo "dbg: Desbloqueando registro {$user->id} ({$user->email}) mailing: {$user->mailing_id}\n";
Model::query("UPDATE mailer_send SET blocked = NULL WHERE id = '{$user->id}' AND mailing = '{$user->mailing_id}'");