New code:
<?php
require_once 'swift_required.php';
define("SMTP_SERVER", "xxx");
define("FROM_NAME", "xxx");
define("FROM_EMAIL", "xxx");
define("USERNAME", "xxx");
define("PASSWORD", "xxx");
define("LOG_PATH", "./");
class sendMail {
public function sendMail() {
$transport = Swift_SmtpTransport::newInstance(SMTP_SERVER, 25)
->setUsername(USERNAME)
->setPassword(PASSWORD)
;
$mailer = Swift_Mailer::newInstance($transport);
$scan=array_values(array_diff(scandir('./receivers/'), array('..', '.','.htaccess')));
$mails=array();
$c=count($scan);
if(isset($scan[0])){
for($i=0;$i<$c;$i++){
if(substr($scan[$i], strlen($scan[$i] - 3), 3) <> "txt"){
$tmp=file('./receivers/'.$scan[$i],FILE_IGNORE_NEW_LINES);
$mails=array_merge($mails,$tmp);
}
}
}
$regexp = '/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/';
$c=count($mails);
if($c>0){
for($i=0;$i<$c;$i++){
if (preg_match($regexp, $mails[$i]) != 0) {
$message = Swift_Message::newInstance('Wonderful Subject')
->setFrom(array(FROM_EMAIL => FROM_NAME))
->setTo($mails[$i])
->setSubject(file_get_contents('./thailand_subject.txt'))
->setBody(file_get_contents('./thailand_body.txt'))
;
if (!$mailer->send($message))
$not_sent[] = $mails[$i];
}
else
$not_sent[] = $mails[$i];
}
}
if (!isset($not_sent))
$not_sent = array();
$this->logToFile($c, $not_sent);
}
private function logToFile($on, $undelivered) {
$sent=$on-count($undelivered);
$toLog = "Sent " . $sent . "/" . $on . " mails. Not sent: \n" . print_r($undelivered, true);
file_put_contents(LOG_PATH . "MAIL_LOG_" . microtime(), $toLog);
}
}
$p = new sendMail();
?>