Sketch: I have the following situation.
"host.domain" is a Round Robin DNS records which points to 3 IPs (ie: 10.0.0.2, 10.0.0.3, 10.0.0.4). The servers behind it, are SMTP servers for handling outgoing mail.
If I use PHP Pear's "Mail" package, and use the smtpinfo to set the host to "host.domain", I can not seem to get any other return value from the RR DNS record should the resulting mailserver be offline. This was my original script:
/* SMTP server name, port, user/passwd */
$smtpinfo = array();
$smtpinfo["host"] = "host.domain";
$smtpinfo["port"] = "25";
$smtpinfo["auth"] = false;
/* Ok send mail */
$errorCount = 0;
do {
/* Create the mail object using the Mail::factory method */
$mail_object =& Mail::factory("smtp", $smtpinfo);
$mail_result = $mail_object->send($recipients, $headers, $mailmsg);
if (PEAR::isError($mail_result)) {
$errorCount++;
echo "Error #". $errorCount .": ". $mail_result->getMessage() ."\n";
} else {
// Mail sent OK
echo "Attempt #". $errorCount .": mail sent via ". $smtpinfo["host"] ."\n";
break;
}
} while ($errorCount < 10);
This "basic" error handling keeps looping the send() action, hoping to use a new IP from "host.domain", but it doesn't. Every attempt (I've done 1000+) makes it loop for 10 times, and fail - because it kept using the same (offline) SMTP server as a result of the RR DNS record.
If I extend my code to the following, it works. But this shifts the "round robin" part to PHP code, which I attempt to avoid.
/* SMTP server name, port, user/passwd */
$smtpinfo = array();
$smtpinfo["host"] = "host.domain";
$smtpinfo["port"] = "25";
$smtpinfo["auth"] = false;
$arrSmtpServers = array("10.0.0.2", "10.0.0.3", "10.0.0.4");
/* Ok send mail */
$errorCount = 0;
do {
/* Randomly select a SMTP host */
$smtpinfo["host"] = $arrSmtpServers[rand(0, count($arrSmtpServers) - 1)];
/* Create the mail object using the Mail::factory method */
$mail_object =& Mail::factory("smtp", $smtpinfo);
$mail_result = $mail_object->send($recipients, $headers, $mailmsg);
if (PEAR::isError($mail_result)) { $errorCount++;
echo "Error #". $errorCount .": ". $mail_result->getMessage() ."\n";
} else {
// Mail sent OK
echo "Attempt #". $errorCount .": mail sent via ". $smtpinfo["host"] ."\n";
break;
}
} while ($errorCount < 10);
Is this known behavior in PHP? If not, what am I doing wrong?