function waitIfNeeded($start) { // Wait a certain amount of time before going to the next message, if necessary $end = microtime(true); $duration = $end - $start; if ($duration * 1000 < SMS_MS_BETWEEN_MESSAGES) { millisleep(SMS_MS_BETWEEN_MESSAGES - $duration + 50); } // add a short duration for integrity against network latency }
public function Send() { set_time_limit(0); $this->FailedRecipients = array(); foreach ($this->To as $recip) { $start = microtime(true); // Seconds since Unix epoch, precise to the nearest millionth (microsecond) // Add the next recipient (we send one email at a time) $this->Mailer->AddAddress($recip['email'], $recip['name']); // If this is going to the sender, make the "Cc" note $subjectModified = false; if ($recip['email'] == $this->From['email']) { $this->Mailer->Subject = "Cc: " . $this->Subject; $subjectModified = true; } try { // Send the message $this->Mailer->Send(); } catch (Exception $e) { // '$mail->ErrorInfo' gets even more information about the error than may be in the exception. // For now, we don't use that (stupid, I know) -- just add this recipient to the list of failed // recipients and try the next one. At least we'll know who didn't get the message. // And this echo statement should print something to the error_log... echo $this->Mailer->ErrorInfo . "\r\n"; $this->FailedRecipients[] = $recip; } // VERY IMPORTANT! (Only send to one recipient at a time.) // Unfortunately, I've had issues sending Bcc-only emails with SES... // I get this error: SMTP Error: Data not accepted. // As a work-around, I send one email to each recipient at a time with "To" // while keeping the SMTP connection alive. So between // each email I need to clear the "To" address. $this->Mailer->ClearAddresses(); // If we modified the subject, change it back if ($subjectModified) { $this->Mailer->Subject = $this->Subject; } $end = microtime(true); $duration = $end - $start; // Wait a certain amount of time before going to the next message, if necessary if ($duration * 1000 < self::MILLISECONDS_BETWEEN_MESSAGES) { millisleep(self::MILLISECONDS_BETWEEN_MESSAGES - $duration); } } // Clear the "To" array $this->To = array(); }