NEW Add validation of MX domain for emails

This commit is contained in:
Laurent Destailleur 2020-10-22 20:01:59 +02:00
parent a6484e933b
commit 8074d2cc1d
4 changed files with 28 additions and 16 deletions

View File

@ -462,9 +462,9 @@ if (empty($reshook))
{
$mesgs = array();
$object->email_from = GETPOST("from");
$object->email_replyto = GETPOST("replyto");
$object->email_errorsto = GETPOST("errorsto");
$object->email_from = GETPOST("from", "none"); // Must allow 'name <email>'
$object->email_replyto = GETPOST("replyto", "none"); // Must allow 'name <email>'
$object->email_errorsto = GETPOST("errorsto", "none"); // Must allow 'name <email>'
$object->title = GETPOST("title");
$object->sujet = GETPOST("sujet");
$object->body = GETPOST("bodyemail", 'restricthtml');
@ -491,7 +491,7 @@ if (empty($reshook))
$mesgs[] = $object->error;
}
setEventMessages($mesg, $mesgs, 'errors');
setEventMessages(null, $mesgs, 'errors');
$action = "create";
}
@ -501,11 +501,10 @@ if (empty($reshook))
$upload_dir = $conf->mailing->dir_output."/".get_exdir($object->id, 2, 0, 1, $object, 'mailing');
if ($action == 'settitle') $object->title = trim(GETPOST('title', 'alpha'));
elseif ($action == 'setemail_from') $object->email_from = trim(GETPOST('email_from', 'alpha'));
elseif ($action == 'setemail_replyto') $object->email_replyto = trim(GETPOST('email_replyto', 'alpha'));
elseif ($action == 'setemail_errorsto') {
$object->email_errorsto = trim(GETPOST('email_errorsto', 'alpha'));
} elseif ($action == 'settitle' && empty($object->title)) {
elseif ($action == 'setemail_from') $object->email_from = trim(GETPOST('email_from', 'none')); // Must allow 'name <email>'
elseif ($action == 'setemail_replyto') $object->email_replyto = trim(GETPOST('email_replyto', 'none')); // Must allow 'name <email>'
elseif ($action == 'setemail_errorsto') $object->email_errorsto = trim(GETPOST('email_errorsto', 'none')); // Must allow 'name <email>'
elseif ($action == 'settitle' && empty($object->title)) {
$mesg = $langs->trans("ErrorFieldRequired", $langs->transnoentities("MailTitle"));
} elseif ($action == 'setfrom' && empty($object->email_from)) {
$mesg = $langs->trans("ErrorFieldRequired", $langs->transnoentities("MailFrom"));
@ -711,7 +710,7 @@ if ($action == 'create')
dol_fiche_head();
print '<table class="border centpercent">';
print '<tr><td class="fieldrequired titlefieldcreate">'.$langs->trans("MailTitle").'</td><td><input class="flat minwidth300" name="titre" value="'.dol_escape_htmltag(GETPOST('titre')).'" autofocus="autofocus"></td></tr>';
print '<tr><td class="fieldrequired titlefieldcreate">'.$langs->trans("MailTitle").'</td><td><input class="flat minwidth300" name="title" value="'.dol_escape_htmltag(GETPOST('title')).'" autofocus="autofocus"></td></tr>';
print '<tr><td class="fieldrequired">'.$langs->trans("MailFrom").'</td><td><input class="flat minwidth200" name="from" value="'.$conf->global->MAILING_EMAIL_FROM.'"></td></tr>';
print '<tr><td>'.$langs->trans("MailErrorsTo").'</td><td><input class="flat minwidth200" name="errorsto" value="'.(!empty($conf->global->MAILING_EMAIL_ERRORSTO) ? $conf->global->MAILING_EMAIL_ERRORSTO : $conf->global->MAIN_MAIL_ERRORS_TO).'"></td></tr>';
@ -868,7 +867,11 @@ if ($action == 'create')
if ($email && !isValidEmail($email)) {
$langs->load("errors");
print img_warning($langs->trans("ErrorBadEMail", $email));
} elseif ($email && !isValidMailDomain($email)) {
$langs->load("errors");
print img_warning($langs->trans("ErrorBadMXDomain", $email));
}
print '</td></tr>';
// Errors to
@ -880,6 +883,9 @@ if ($action == 'create')
if ($email && !isValidEmail($email)) {
$langs->load("errors");
print img_warning($langs->trans("ErrorBadEMail", $email));
} elseif ($email && !isValidMailDomain($email)) {
$langs->load("errors");
print img_warning($langs->trans("ErrorBadMXDomain", $email));
}
print '</td></tr>';

View File

@ -629,6 +629,9 @@ function checkVal($out = '', $check = 'alphanohtml', $filter = null, $options =
case 'san_alpha':
$out = filter_var($out, FILTER_SANITIZE_STRING);
break;
case 'email':
$out = filter_var($out, FILTER_SANITIZE_EMAIL);
break;
case 'aZ':
if (!is_array($out))
{
@ -2284,7 +2287,7 @@ function dol_print_url($url, $target = '_blank', $max = 32, $withpicto = 0)
}
/**
* Show EMail link
* Show EMail link formatted for HTML output.
*
* @param string $email EMail to show (only email, without 'Name of recipient' before)
* @param int $cid Id of contact if known
@ -2299,7 +2302,7 @@ function dol_print_email($email, $cid = 0, $socid = 0, $addlink = 0, $max = 64,
{
global $conf, $user, $langs, $hookmanager;
$newemail = $email;
$newemail = dol_escape_htmltag($email);
if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER) && $withpicto) $withpicto = 0;
@ -2936,11 +2939,12 @@ function dol_print_address($address, $htmlid, $element, $id, $noprint = 0, $char
/**
* Return true if email syntax is ok
* Return true if email syntax is ok.
*
* @param string $address email (Ex: "toto@examle.com", "John Do <johndo@example.com>")
* @param int $acceptsupervisorkey If 1, the special string '__SUPERVISOREMAIL__' is also accepted as valid
* @return boolean true if email syntax is OK, false if KO or empty string
* @see isValidMXRecord()
*/
function isValidEmail($address, $acceptsupervisorkey = 0)
{
@ -2956,6 +2960,7 @@ function isValidEmail($address, $acceptsupervisorkey = 0)
*
* @param string $domain Domain name (Ex: "yahoo.com", "yhaoo.com", "dolibarr.fr")
* @return int -1 if error (function not available), 0=Not valid, 1=Valid
* @see isValidEmail()
*/
function isValidMXRecord($domain)
{

View File

@ -484,15 +484,15 @@ function dolAddEmailTrackId($email, $trackingid)
}
/**
* Return true if email has a domain name that can't be resolved
* Return true if email has a domain name that can be resolved to MX type.
*
* @param string $mail Email address (Ex: "toto@example.com", "John Do <johndo@example.com>")
* @return boolean True if domain email is OK, False if KO
* @return int -1 if error (function not available), 0=Not valid, 1=Valid
*/
function isValidMailDomain($mail)
{
list($user, $domain) = explode("@", $mail, 2);
return checkdnsrr($domain, "MX");
return ($domain ? isValidMXRecord($domain, "MX") : 0);
}
/**

View File

@ -5,6 +5,7 @@ NoErrorCommitIsDone=No error, we commit
# Errors
ErrorButCommitIsDone=Errors found but we validate despite this
ErrorBadEMail=Email %s is wrong
ErrorBadMXDomain=Email %s seems wrong (domain has no valid MX record)
ErrorBadUrl=Url %s is wrong
ErrorBadValueForParamNotAString=Bad value for your parameter. It appends generally when translation is missing.
ErrorLoginAlreadyExists=Login %s already exists.