/**
  * Sends a Newsletter email to the specified address
  *
  * @param $member The object containing information about the member being emailed
  */
 private function sendToAddress($email, $address, $messageID = null, $member)
 {
     $email->setTo($address);
     $result = $email->send($messageID);
     // Log result of the send
     $newsletter = new Newsletter_SentRecipient();
     $newsletter->Email = $address;
     $newsletter->MemberID = $member->ID;
     // If sending is successful
     $newsletter->Result = $result == true ? 'Sent' : 'Failed';
     $newsletter->ParentID = $this->newsletter->ID;
     $newsletter->write();
     // Adding a pause between email sending can be useful for debugging purposes
     // sleep(10);
 }
 private function recordBounce($email, $date = null, $time = null, $error = null)
 {
     if (preg_match('/<(.*)>/', $email, $parts)) {
         $email = $parts[1];
     }
     $SQL_email = Convert::raw2sql($email);
     $SQL_bounceTime = Convert::raw2sql("{$date} {$time}");
     $duplicateBounce = DataObject::get_one("Email_BounceRecord", "\"BounceEmail\" = '{$SQL_email}' AND (\"BounceTime\"+INTERVAL 1 MINUTE) > '{$SQL_bounceTime}'");
     if (!$duplicateBounce) {
         $record = new Email_BounceRecord();
         $member = DataObject::get_one('Member', "\"Email\"='{$SQL_email}'");
         if ($member) {
             $record->MemberID = $member->ID;
             // If the SilverStripeMessageID (taken from the X-SilverStripeMessageID header embedded in the email)
             // is sent, then log this bounce in a Newsletter_SentRecipient record so it will show up on the 'Sent
             // Status Report' tab of the Newsletter
             if (isset($_REQUEST['SilverStripeMessageID'])) {
                 // Note: was sent out with: $project . '.' . $messageID;
                 $message_id_parts = explode('.', $_REQUEST['SilverStripeMessageID']);
                 // Escape just in case
                 $SQL_memberID = Convert::raw2sql($member->ID);
                 // Log the bounce
                 if (class_exists('Newsletter_SentRecipient')) {
                     // Note: was encoded with: base64_encode( $newsletter->ID . '_' . date( 'd-m-Y H:i:s' ) );
                     $newsletter_id_date_parts = explode('_', base64_decode($message_id_parts[1]));
                     $SQL_newsletterID = Convert::raw2sql($newsletter_id_date_parts[0]);
                     $oldNewsletterSentRecipient = DataObject::get_one("Newsletter_SentRecipient", "\"MemberID\" = '{$SQL_memberID}' AND \"ParentID\" = '{$SQL_newsletterID}'" . " AND \"Email\" = '{$SQL_email}'");
                     // Update the Newsletter_SentRecipient record if it exists
                     if ($oldNewsletterSentRecipient) {
                         $oldNewsletterSentRecipient->Result = 'Bounced';
                         $oldNewsletterSentRecipient->write();
                     } else {
                         // For some reason it didn't exist, create a new record
                         $newNewsletterSentRecipient = new Newsletter_SentRecipient();
                         $newNewsletterSentRecipient->Email = $SQL_email;
                         $newNewsletterSentRecipient->MemberID = $member->ID;
                         $newNewsletterSentRecipient->Result = 'Bounced';
                         $newNewsletterSentRecipient->ParentID = $newsletter_id_date_parts[0];
                         $newNewsletterSentRecipient->write();
                     }
                     // Now we are going to Blacklist this member so that email will not be sent to them in the future.
                     // Note: Sending can be re-enabled by going to 'Mailing List' 'Bounced' tab and unchecking the box
                     // under 'Blacklisted'
                     $member->setBlacklistedEmail(TRUE);
                     echo '<p><b>Member: ' . $member->FirstName . ' ' . $member->Surname . ' <' . $member->Email . '> was added to the Email Blacklist!</b></p>';
                 }
             }
         }
         if (!$date) {
             $date = date('d-m-Y');
         }
         if (!$time) {
             $time = date('H:i:s');
         }
         $record->BounceEmail = $email;
         $record->BounceTime = $date . ' ' . $time;
         $record->BounceMessage = $error;
         $record->write();
         echo "Handled bounced email to address: {$email}";
     } else {
         echo 'Sorry, this bounce report has already been logged, not logging this duplicate bounce.';
     }
 }