Skip to content

Commit

Permalink
Fix loop to show SPF was not complete
Browse files Browse the repository at this point in the history
  • Loading branch information
eldy committed Sep 7, 2024
1 parent bd04568 commit db5bfa7
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 30 deletions.
69 changes: 40 additions & 29 deletions htdocs/admin/mails.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
require '../main.inc.php';
require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
require_once DOL_DOCUMENT_ROOT.'/core/lib/geturl.lib.php';

// Load translation files required by the page
$langs->loadLangs(array("companies", "products", "admin", "mails", "other", "errors"));
Expand Down Expand Up @@ -1040,46 +1041,56 @@ function change_smtp_auth_method() {
$text .= ($text ? '<br><br>' : '').$langs->trans("WarningPHPMailSPF", getDolGlobalString('MAIN_EXTERNAL_SMTP_SPF_STRING_TO_ADD'));
}
}
// Test SPF email company
$companyemail = getDolGlobalString('MAIN_INFO_SOCIETE_MAIL');
$dnsinfo = false;
if (!empty($companyemail) && function_exists('dns_get_record') && !getDolGlobalString('MAIN_DISABLE_DNS_GET_RECORD')) {
$arrayofemailparts = explode('@', $companyemail);
if (count($arrayofemailparts) == 2) {
$domain = $arrayofemailparts[1];
$dnsinfo = dns_get_record($domain, DNS_TXT);
}

// Build list of main email addresses in $emailstotest and their domain to test in $domainstotest
$emailstotest = array();
if (getDolGlobalString('MAIN_INFO_SOCIETE_MAIL')) {
$emailstotest[getDolGlobalString('MAIN_INFO_SOCIETE_MAIL')] = getDomainFromURL(preg_replace('/^.*@/', '', getDolGlobalString('MAIN_INFO_SOCIETE_MAIL')), 1);
}
if (!empty($dnsinfo) && is_array($dnsinfo)) {
foreach ($dnsinfo as $info) {
if (strpos($info['txt'], 'v=spf') !== false) {
$text .= ($text ? '<br><br>' : '').$langs->trans("ActualMailSPFRecordFound", $companyemail, $info['txt']);
}
if (getDolGlobalString('MAIN_MAIL_EMAIL_FROM')) {
$emailstotest[getDolGlobalString('MAIN_MAIL_EMAIL_FROM')] = getDomainFromURL(preg_replace('/^.*@/', '', getDolGlobalString('MAIN_MAIL_EMAIL_FROM')), 1);
}
if (!empty($user->email)) {
$emailstotest[$user->email] = getDomainFromURL(preg_replace('/^.*@/', '', $user->email), 1);
}
$domainstotest = array();
foreach ($emailstotest as $email => $domain) {
if (empty($domainstotest[$domain])) {
$domainstotest[$domain] = array($email => $email);
} else {
$domainstotest[$domain][$email] = $email;
}
}
// Test SPF default automatic email from
$defaultnoreplyemail = getDolGlobalString('MAIN_MAIL_EMAIL_FROM');
if ($defaultnoreplyemail != $companyemail) { // We show if email differs
$dnsinfo = false;
if (!empty($defaultnoreplyemail) && function_exists('dns_get_record') && !getDolGlobalString('MAIN_DISABLE_DNS_GET_RECORD')) {
$arrayofemailparts = explode('@', $defaultnoreplyemail);
if (count($arrayofemailparts) == 2) {
$domain = $arrayofemailparts[1];

// Test DNS entry for emails
foreach (array('SPF', 'DMARC') as $dnstype) {
foreach ($domainstotest as $domaintotest => $listofemails) {
$dnsinfo = false;
$foundforemail = 0;
if (!empty($domaintotest) && function_exists('dns_get_record') && !getDolGlobalString('MAIN_DISABLE_DNS_GET_RECORD')) {
$domain = $domaintotest;
if ($dnstype == 'DMARC') {
$domain = '_dmarc.'.$domain;
}
$dnsinfo = dns_get_record($domain, DNS_TXT);
}
}
if (!empty($dnsinfo) && is_array($dnsinfo)) {
foreach ($dnsinfo as $info) {
if (strpos($info['txt'], 'v=spf') !== false) {
$text .= ($text ? '<br><br>' : '').$langs->trans("ActualMailSPFRecordFound", $defaultnoreplyemail, $info['txt']);
if (!empty($dnsinfo) && is_array($dnsinfo)) {
foreach ($dnsinfo as $info) {
if (($dnstype == 'SPF' && stripos($info['txt'], 'v=spf') !== false)
|| ($dnstype == 'DMARC' && stripos($info['txt'], 'v=dmarc') !== false)) {
$foundforemail++;
$text .= ($text ? '<br>' : '').$langs->trans("ActualMailDNSRecordFound", $dnstype, implode(', ', $listofemails), $info['txt']);
}
}
}
if (!$foundforemail) {
$text .= ($text ? '<br>' : '').$langs->trans("ActualMailDNSRecordFound", $dnstype, implode(', ', $listofemails), '<span class="opacitymedium">'.$langs->transnoentitiesnoconv("None").'</span>');
}
}
}


if ($text) {
print info_admin($text, 0, 0, '1', '');
print info_admin($langs->trans("SPFAndDMARCInformation").' :<br>'.$text, 0, 0, '1', '');
}
}

Expand Down
3 changes: 2 additions & 1 deletion htdocs/langs/en_US/admin.lang
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,8 @@ WarningPHPMailD=It is therefore recommended to change the sending method of e-ma
WarningPHPMailDbis=If you really want to keep the default "PHP" method to send emails, just ignore this warning, or remove it by %sclicking here%s.
WarningPHPMail2=If your email SMTP provider need to restrict email client to some IP addresses (very rare), this is the IP address of the mail user agent (MUA) for your ERP CRM application: <strong>%s</strong>.
WarningPHPMailSPF=If the domain name in your sender email address is protected by a SPF record (ask your domain name registar), you must add the following IPs in the SPF record of the DNS of your domain: <strong>%s</strong>.
ActualMailSPFRecordFound=Actual SPF record found (for email %s) : %s
SPFAndDMARCInformation=SPF and DMARC DNS record for main email addresses
ActualMailDNSRecordFound=Actual %s record found (for email %s) : %s
ClickToShowDescription=Click to show description
DependsOn=This module needs the module(s)
RequiredBy=This module is required by module(s)
Expand Down

0 comments on commit db5bfa7

Please sign in to comment.