From b27ec5437972ff2a4dd2e6bff588196f526954bc Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Mon, 10 Dec 2018 14:35:33 +0100 Subject: [PATCH] A better way to manage unsubscribed emails --- .../install/mysql/migration/9.0.0-10.0.0.sql | 14 ++++ .../tables/llx_mailing_unsubscribe.key.sql | 20 ++++++ .../mysql/tables/llx_mailing_unsubscribe.sql | 30 +++++++++ .../public/emailing/mailing-unsubscribe.php | 64 +++++++++++-------- 4 files changed, 102 insertions(+), 26 deletions(-) create mode 100644 htdocs/install/mysql/tables/llx_mailing_unsubscribe.key.sql create mode 100644 htdocs/install/mysql/tables/llx_mailing_unsubscribe.sql diff --git a/htdocs/install/mysql/migration/9.0.0-10.0.0.sql b/htdocs/install/mysql/migration/9.0.0-10.0.0.sql index 101bcb6f4f8..0104db08d56 100644 --- a/htdocs/install/mysql/migration/9.0.0-10.0.0.sql +++ b/htdocs/install/mysql/migration/9.0.0-10.0.0.sql @@ -36,3 +36,17 @@ insert into llx_c_action_trigger (code,label,description,elementtype,rang) values ('TICKET_CREATE','Ticket created','Executed when a ticket is created','ticket',161); insert into llx_c_action_trigger (code,label,description,elementtype,rang) values ('TICKET_MODIFY','Ticket modified','Executed when a ticket is modified','ticket',163); insert into llx_c_action_trigger (code,label,description,elementtype,rang) values ('TICKET_DELETE','Ticket deleted','Executed when a ticket is deleted','ticket',164); + +create table llx_mailing_unsubscribe +( + rowid integer AUTO_INCREMENT PRIMARY KEY, + entity integer DEFAULT 1 NOT NULL, -- multi company id + email varchar(255), + unsubscribegroup varchar(128) DEFAULT '', + ip varchar(128), + date_creat datetime, -- creation date + tms timestamp +)ENGINE=innodb; + +ALTER TABLE llx_mailing_unsubscribe ADD UNIQUE uk_mailing_unsubscribe(email, entity, unsubscribegroup); + diff --git a/htdocs/install/mysql/tables/llx_mailing_unsubscribe.key.sql b/htdocs/install/mysql/tables/llx_mailing_unsubscribe.key.sql new file mode 100644 index 00000000000..baa74a4d133 --- /dev/null +++ b/htdocs/install/mysql/tables/llx_mailing_unsubscribe.key.sql @@ -0,0 +1,20 @@ +-- =================================================================== +-- Copyright (C) 2018 Laurent Destailleur +-- +-- This program is free software; you can redistribute it and/or modify +-- it under the terms of the GNU General Public License as published by +-- the Free Software Foundation; either version 3 of the License, or +-- (at your option) any later version. +-- +-- This program 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 General Public License for more details. +-- +-- You should have received a copy of the GNU General Public License +-- along with this program. If not, see . +-- +-- =================================================================== + + +ALTER TABLE llx_mailing_unsubscribe ADD UNIQUE uk_mailing_unsubscribe(email, entity, unsubscribegroup); \ No newline at end of file diff --git a/htdocs/install/mysql/tables/llx_mailing_unsubscribe.sql b/htdocs/install/mysql/tables/llx_mailing_unsubscribe.sql new file mode 100644 index 00000000000..743e6ba1220 --- /dev/null +++ b/htdocs/install/mysql/tables/llx_mailing_unsubscribe.sql @@ -0,0 +1,30 @@ +-- ======================================================================== +-- Copyright (C) 2005 Rodolphe Quiedeville +-- Copyright (C) 2005-2012 Regis Houssin +-- +-- This program is free software; you can redistribute it and/or modify +-- it under the terms of the GNU General Public License as published by +-- the Free Software Foundation; either version 3 of the License, or +-- (at your option) any later version. +-- +-- This program 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 General Public License for more details. +-- +-- You should have received a copy of the GNU General Public License +-- along with this program. If not, see . +-- +-- ======================================================================== + + +create table llx_mailing_unsubscribe +( + rowid integer AUTO_INCREMENT PRIMARY KEY, + entity integer DEFAULT 1 NOT NULL, -- multi company id + email varchar(255), + unsubscribegroup varchar(128) DEFAULT '', + ip varchar(128), + date_creat datetime, -- creation date + tms timestamp +)ENGINE=innodb; diff --git a/htdocs/public/emailing/mailing-unsubscribe.php b/htdocs/public/emailing/mailing-unsubscribe.php index c27cb024aab..67672f19e7c 100644 --- a/htdocs/public/emailing/mailing-unsubscribe.php +++ b/htdocs/public/emailing/mailing-unsubscribe.php @@ -23,7 +23,8 @@ /** * \file public/emailing/mailing-unsubscribe.php * \ingroup mailing - * \brief Script use to update unsubcribe contact to prospect mailing list + * \brief Script use to update unsubcribe status of an email + * https://myserver/public/emailing/mailing-unsubscribe.php?unsuscrib=1&securitykey=securitykey&tag=abcdefghijklmn */ if (! defined('NOLOGIN')) define("NOLOGIN",1); // This means this output page does not require to be logged. @@ -75,38 +76,49 @@ if ($securitykey != $conf->global->MAILING_EMAIL_UNSUBSCRIBE_KEY) if (! empty($tag) && ($unsuscrib=='1')) { - // Update status of mail in recipient mailing list table - $statut='3'; - $sql = "UPDATE ".MAIN_DB_PREFIX."mailing_cibles SET statut=".$statut." WHERE tag='".$db->escape($tag)."'"; - dol_syslog("public/emailing/mailing-unsubscribe.php : Mail unsubcribe : ".$sql, LOG_DEBUG); + dol_syslog("public/emailing/mailing-unsubscribe.php : Launch unsubscribe requests", LOG_DEBUG); - $resql=$db->query($sql); - if (! $resql) dol_print_error($db); - - // Update status communication of thirdparty prospect - $sql = "UPDATE ".MAIN_DB_PREFIX."societe SET fk_stcomm=-1 WHERE rowid IN (SELECT source_id FROM ".MAIN_DB_PREFIX."mailing_cibles WHERE tag='".$db->escape($tag)."' AND source_type='thirdparty' AND source_id is not null)"; - dol_syslog("public/emailing/mailing-unsubscribe.php : Mail unsubcribe thirdparty : ".$sql, LOG_DEBUG); - - $resql=$db->query($sql); - if (! $resql) dol_print_error($db); - - // Update status communication of contact prospect - $sql = "UPDATE ".MAIN_DB_PREFIX."socpeople SET no_email=1 WHERE rowid IN (SELECT source_id FROM ".MAIN_DB_PREFIX."mailing_cibles WHERE tag = '".$db->escape($tag)."' AND source_type='contact' AND source_id is not null)"; - dol_syslog("public/emailing/mailing-unsubscribe.php : Mail unsubcribe contact : ".$sql, LOG_DEBUG); - - $resql=$db->query($sql); - if (! $resql) dol_print_error($db); - - - $sql = "SELECT mc.email"; - $sql .= " FROM ".MAIN_DB_PREFIX."mailing_cibles as mc"; - $sql .= " WHERE mc.tag='".$db->escape($tag)."'"; + $sql = "SELECT mc.email, m.entity"; + $sql .= " FROM ".MAIN_DB_PREFIX."mailing_cibles as mc, ".MAIN_DB_PREFIX."mailing as m"; + $sql .= " WHERE mc.fk_mailing = m.rowid AND mc.tag='".$db->escape($tag)."'"; $resql=$db->query($sql); if (! $resql) dol_print_error($db); $obj = $db->fetch_object($resql); + if (empty($obj->email)) + { + print 'Email not found. No need to unsubscribe.'; + exit; + } + + // Update status of mail in recipient mailing list table + $statut='3'; + $sql = "UPDATE ".MAIN_DB_PREFIX."mailing_cibles SET statut=".$statut." WHERE tag='".$db->escape($tag)."'"; + + $resql=$db->query($sql); + if (! $resql) dol_print_error($db); + + // Update status communication of thirdparty prospect (old data) + $sql = "UPDATE ".MAIN_DB_PREFIX."societe SET fk_stcomm=-1 WHERE rowid IN (SELECT source_id FROM ".MAIN_DB_PREFIX."mailing_cibles WHERE tag = '".$db->escape($tag)."' AND source_type='thirdparty' AND source_id is not null)"; + + $resql=$db->query($sql); + if (! $resql) dol_print_error($db); + + // Update status communication of contact prospect (old data) + $sql = "UPDATE ".MAIN_DB_PREFIX."socpeople SET no_email=1 WHERE rowid IN (SELECT source_id FROM ".MAIN_DB_PREFIX."mailing_cibles WHERE tag = '".$db->escape($tag)."' AND source_type='contact' AND source_id is not null)"; + + $resql=$db->query($sql); + if (! $resql) dol_print_error($db); + + // Update status communication of contact prospect (old data) + $sql = "INSERT INTO ".MAIN_DB_PREFIX."mailing_unsubscribe (date_creat, entity, email) VALUES ('".$db->idate(dol_now())."', ".$obj->entity.", '".$obj->email."')"; + + $resql=$db->query($sql); + //if (! $resql) dol_print_error($db); No test on errors, may fail if already unsubscribed + + header("Content-type: text/html; charset=".$conf->file->character_set_client); print '';