SPF record check
If you are runing a blast email service or for any other reason need to send emails from servers that are not hosting the domain name that you will set as sender in the email message you need to set SPF record on the web page server to make sure that the messages will not end in the SPAM. You can do manual checks but if you have multiple clients or you want to have an insurance that some client will not disable the SPF record right before the next blast message - here comes this useful function:
/**
* Check if IP address is allowed to send emails on behalf of the hostname by SPF record.
* @author Samui Banti - https://samiwell.eu
* @param string $hostname - The host name of the email address in format sutable for dns_get_record() function.
* @param string $ip - The IP address of the server that sends the email.
* @return TRUE if the server is allowed to send on the behalf of the hostname
*/
function check_spf_ip($hostname, $ip)
{
$txt_records = dns_get_record($hostname, DNS_TXT);
if(empty($txt_records)) {
return false;
}
foreach($txt_records as $record) {
if(array_key_exists('txt', $record)) {
if(strpos($record['txt'], 'v=spf1') !== false) {
if(strpos($record['txt'], $ip)) {
return true;
}
}
}
}
return false;
}