2007-07-19 12:40:05 +02:00
|
|
|
<?php
|
|
|
|
class NewsletterEmailProcess extends BatchProcess {
|
|
|
|
|
|
|
|
protected $subject;
|
|
|
|
protected $body;
|
|
|
|
protected $from;
|
|
|
|
protected $newsletter;
|
|
|
|
protected $nlType;
|
|
|
|
protected $messageID;
|
|
|
|
|
2007-09-16 03:03:04 +02:00
|
|
|
/**
|
|
|
|
* Set up a Newsletter Email Process
|
|
|
|
*
|
|
|
|
* @recipients A DataObject containing the addresses of the recipients of this newsletter
|
|
|
|
*/
|
|
|
|
function __construct( $subject, $body, $from, $newsletter, $nlType, $messageID = null, $recipients) {
|
2007-07-19 12:40:05 +02:00
|
|
|
|
|
|
|
$this->subject = $subject;
|
|
|
|
$this->body = $body;
|
|
|
|
$this->from = $from;
|
|
|
|
$this->newsletter = $newsletter;
|
|
|
|
$this->nlType = $nlType;
|
|
|
|
$this->messageID = $messageID;
|
2007-09-16 03:03:04 +02:00
|
|
|
|
|
|
|
parent::__construct( $recipients );
|
2007-07-19 12:40:05 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function next( $count = 10 ) {
|
|
|
|
$max = $this->current + $count;
|
|
|
|
|
|
|
|
$max = $max < count( $this->objects ) ? $max : count( $this->objects );
|
|
|
|
|
|
|
|
while($this->current < $max) {
|
|
|
|
$index = $this->current++;
|
|
|
|
$member = $this->objects[$index];
|
2007-09-16 02:57:31 +02:00
|
|
|
|
2007-07-19 12:40:05 +02:00
|
|
|
// check to see if the user has unsubscribed from the mailing list
|
|
|
|
// TODO Join in the above query first
|
|
|
|
$unsubscribeRecord = DataObject::get_one('Member_UnsubscribeRecord', "`MemberID`='{$member->ID}' AND `NewsletterTypeID`='{$this->nlType->ID}'");
|
|
|
|
|
|
|
|
if( !$unsubscribeRecord ) {
|
|
|
|
|
|
|
|
$address = $member->Email;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Email Blacklisting Support
|
|
|
|
*/
|
2007-11-19 02:38:50 +01:00
|
|
|
if($member->BlacklistedEmail && Email_BlackList::isBlocked($address)){
|
|
|
|
$bounceRecord = new Email_BounceRecord();
|
|
|
|
$bounceRecord->BounceEmail = $member->Email;
|
|
|
|
$bounceRecord->BounceTime = date("Y-m-d H:i:s",time());
|
|
|
|
$bounceRecord->BounceMessage = "BlackListed Email";
|
|
|
|
$bounceRecord->MemberID = $member->ID;
|
|
|
|
$bounceRecord->write();
|
2007-09-16 04:17:14 +02:00
|
|
|
|
2007-11-19 02:38:50 +01:00
|
|
|
// Log the blacklist for this specific Newsletter
|
|
|
|
$newsletter = new Newsletter_SentRecipient();
|
|
|
|
$newsletter->Email = $address;
|
|
|
|
$newsletter->MemberID = $member->ID;
|
|
|
|
$newsletter->Result = 'BlackListed';
|
|
|
|
$newsletter->ParentID = $this->newsletter->ID;
|
|
|
|
$newsletter->write();
|
|
|
|
|
|
|
|
} else {
|
|
|
|
$e = new Newsletter_Email($this->nlType);
|
2007-09-16 04:17:14 +02:00
|
|
|
$e->setBody( $this->body );
|
|
|
|
$e->setSubject( $this->subject );
|
|
|
|
$e->setFrom( $this->from );
|
|
|
|
$e->setTemplate( $this->nlType->Template );
|
|
|
|
|
2007-11-19 02:38:50 +01:00
|
|
|
if(method_exists($member, "getNameForEmail"))
|
|
|
|
$nameForEmail = $member->getNameForEmail();
|
2007-09-16 04:17:14 +02:00
|
|
|
|
2007-11-19 02:38:50 +01:00
|
|
|
$e->populateTemplate( array( 'Member' => $member, 'FirstName' => $member->FirstName, 'NameForEmail'=>$nameForEmail ) );
|
|
|
|
$this->sendToAddress( $e, $address, $this->messageID, $member);
|
|
|
|
}
|
2007-07-19 12:40:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( $this->current >= count( $this->objects ) )
|
|
|
|
return $this->complete();
|
|
|
|
else
|
|
|
|
return parent::next();
|
|
|
|
}
|
|
|
|
|
2007-09-16 02:57:31 +02:00
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
if ($result == true) {
|
|
|
|
$newsletter->Result = 'Sent';
|
|
|
|
} else {
|
|
|
|
$newsletter->Result = 'Failed';
|
|
|
|
}
|
|
|
|
$newsletter->ParentID = $this->newsletter->ID;
|
|
|
|
$newsletter->write();
|
|
|
|
// Adding a pause between email sending can be useful for debugging purposes
|
|
|
|
// sleep(10);
|
|
|
|
}
|
2007-07-19 12:40:05 +02:00
|
|
|
|
|
|
|
function complete() {
|
|
|
|
parent::complete();
|
|
|
|
|
2007-09-15 23:11:36 +02:00
|
|
|
if( $this->newsletter->SentDate ) {
|
|
|
|
$resent = true;
|
|
|
|
} else {
|
|
|
|
$resent = false;
|
|
|
|
}
|
2007-09-16 03:04:10 +02:00
|
|
|
|
|
|
|
$this->newsletter->SentDate = 'now';
|
|
|
|
$this->newsletter->Status = 'Send';
|
|
|
|
$this->newsletter->write();
|
|
|
|
|
|
|
|
// Call the success message JS function with the Newsletter information
|
|
|
|
if( $resent ) {
|
|
|
|
return "resent_ok( '{$this->nlType->ID}', '{$this->newsletter->ID}', '".count( $this->objects )."' )";
|
|
|
|
} else {
|
|
|
|
return "draft_sent_ok( '{$this->nlType->ID}', '{$this->newsletter->ID}', '".count( $this->objects )."' )";
|
|
|
|
}
|
2007-07-19 12:40:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
?>
|