Last active
December 17, 2015 00:38
-
-
Save abennouna/5522162 to your computer and use it in GitHub Desktop.
Tutoriel Yii : Intercepter les rapports de non remise d’e-mail (NDR) pour exécuter une commande spécifique - http://tellibus.com/blog/2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE TABLE IF NOT EXISTS `User` ( | |
`id` int unsigned NOT NULL AUTO_INCREMENT, | |
`status` tinyint unsigned NOT NULL, | |
`login` varchar(30) NOT NULL, | |
`password` tinytext NOT NULL, | |
`email` varchar(128) NOT NULL, | |
`firstName` varchar(128) NOT NULL, | |
`lastName` varchar(128) NOT NULL, | |
PRIMARY KEY (`id`), | |
UNIQUE KEY `login` (`login`) | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class HandleBounceMessageCommand extends CConsoleCommand | |
{ | |
public function run($args) | |
{ | |
$socket = fopen ("php://stdin", 'r'); | |
$email = ''; | |
// On charge d’abord le contenu de l’e-mail | |
while (!feof($socket)) { | |
$email .= fread($socket, 1024); | |
} | |
fclose($socket); | |
// Ici, on a inclus deux types de motifs que j’ai pu retrouver dans les NDR | |
// Vous pouvez ajouter d’autres expressions régulières au besoin | |
if (preg_match("/X-Failed-Recipients\:\s*(.*)/", $email, $emailMatch) == 0) { | |
preg_match("/RCPT TO:<(.*)>/", $email, $emailMatch); | |
} | |
// On cherche l’utilisateur correspondant à l’e-mail non remis | |
$connection = Yii::app()->db; | |
$sql = 'SELECT id, login, firstName, lastName FROM User WHERE email = "' . trim($emailMatch[1]) . '"'; | |
$command = $connection->createCommand($sql); | |
$user = $command->queryRow(); | |
// On désactive cet utilisateur (en changeant son « status » à 1) | |
$sql = 'UPDATE User SET status = 1 WHERE id = ' . $user['id']; | |
$command = $connection->createCommand($sql); | |
$command->execute(); | |
// On notifie ensuite l’admin de l’application Web | |
$header = "MIME-Version: 1.0\r\nContent-type: text/plain; charset=UTF-8\r\nFrom: Notification de NDR <[email protected]>"; | |
$header .= "\r\nX-Priority: 1 (Highest)\r\nX-MSMail-Priority: High\r\nImportance: High"; | |
$subject = 'Un utilistateur a saisi une adresse e-mail erronée'; | |
$body = 'Bonjour, | |
L’utilisateur « ' . $user['firstName'] . ' ' . $user['lastName'] . ' » a saisi une adresse e-mail erronée dans le formulaire d’enregistrement. | |
Par conséquent, son login « ' . $user['login'] . ' » a été automatiquement désactivé. | |
Aucune autre action n’est nécessaire.'; | |
mail('[email protected]', '=?UTF-8?B?' . base64_encode($subject) . '?=', $body . ' | |
Pour votre référence, voici le contenu du rapport de non remise correspondant : | |
' . $email, $header); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|/path/to/protected/yiic handleBounceMessage |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Pour plus de détails, consultez le billet de blog http://tellibus.com/blog/2-Tutoriel-Yii--Intercepter-les-rapports-de-non-remise-d-email-NDR-pour-exécuter-une-commande-spécifique