elofgren: Track whether mail() returns TRUE or FALSE when a Newsletter is sent and record the result in a new Newsletter_SentRecipient table.

Add a 'Sent Status Report' tab to Newsletters that shows the following 4 reports: 
* Sending to the Following Recipients Failed 
* Sending to the Following Recipients Bounced (@TODO: Make bounces actually show up here) 
* The Newsletter has Never Been Sent to Following Subscribers 
* Sending to the Following Recipients was Successful 
More info: http://support.silverstripe.com/gsoc/wiki/GSoc07UsabilityElijah 
(merged from branches/gsoc)


git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/cms/trunk@41995 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Ingo Schommer 2007-09-16 00:57:31 +00:00
parent 07c4301ca7
commit 4deaf09212
5 changed files with 213 additions and 8 deletions

View File

@ -11,12 +11,15 @@ class Newsletter extends DataObject {
require_once("forms/Form.php");
$group = DataObject::get_by_id("Group", $this->Parent()->GroupID);
$sent_status_report = $this->renderWith("Newsletter_SentStatusReport");
$ret = new FieldSet(
new TabSet("Root",
$mailTab = new Tab("Newsletter",
new TextField("Subject", "Subject", $this->Subject),
new HtmlEditorField("Content", "Content")
),
$sentToTab = new Tab("Sent Status Report",
new LiteralField("Sent Status Report", $sent_status_report)
)
)
);
@ -28,7 +31,58 @@ class Newsletter extends DataObject {
return $ret;
}
/**
* Returns a DataObject listing the recipients for the given status for this newsletter
*
* @param string $result 3 possible values: "Sent", (mail() returned TRUE), "Failed" (mail() returned FALSE), or "Bounced" ({@see $email_bouncehandler}).
*/
function SentRecipients($result) {
$SQL_result = Convert::raw2sql($result);
return DataObject::get("Newsletter_SentRecipient",array("ParentID='".$this->ID."'", "Result='".$SQL_result."'"));
}
/**
* Returns a DataObjectSet containing the subscribers who have never been sent this Newsletter
*
*/
function UnsentSubscribers() {
// Get a list of everyone who has been sent this newsletter
$sent_recipients = DataObject::get("Newsletter_SentRecipient","ParentID='".$this->ID."'");
// If this Newsletter has not been sent to anyone yet, $sent_recipients will be null
if ($sent_recipients != null) {
$sent_recipients_array = $sent_recipients->toNestedArray('MemberID');
} else {
$sent_recipients_array = array();
}
// Get a list of all the subscribers to this newsletter
$subscribers = DataObject::get( 'Member', "`GroupID`='".$this->Parent()->GroupID."'", null, "INNER JOIN `Group_Members` ON `MemberID`=`Member`.`ID`" );
// If this Newsletter has no subscribers, $subscribers will be null
if ($subscribers != null) {
$subscribers_array = $subscribers->toNestedArray();
} else {
$subscribers_array = array();
}
// Get list of subscribers who have not been sent this newsletter:
$unsent_subscribers_array = array_diff_key($subscribers_array, $sent_recipients_array);
// Create new data object set containing the subscribers who have not been sent this newsletter:
$unsent_subscribers = new DataObjectSet();
foreach($unsent_subscribers_array as $key => $data) {
$record = array(
'ID' => $data['ID'],
'FirstName' => $data['FirstName'],
'Surname' => $data['Surname'],
'Email' => $data['Email']
);
$unsent_subscribers->push(new ArrayData($record));
}
return $unsent_subscribers;
}
function getTitle() {
return $this->getField('Subject');
}
@ -51,6 +105,7 @@ class Newsletter extends DataObject {
static $has_many = array(
"Recipients" => "Newsletter_Recipient",
"SentRecipients" => "Newsletter_SentRecipient",
);
static function newDraft( $parentID, $subject, $content ) {
@ -69,6 +124,21 @@ class Newsletter extends DataObject {
}
}
class Newsletter_SentRecipient extends DataObject {
/**
* The DB schema for Newsletter_SentRecipient.
*
* ParentID is the the Newsletter
* Email and MemberID keep track of the recpients information
* Result has 3 possible values: "Sent", (mail() returned TRUE), "Failed" (mail() returned FALSE), or "Bounced" ({@see $email_bouncehandler}).
*/
static $db = array(
"ParentID" => "Int",
"Email" => "Varchar(255)",
"MemberID" => "Int",
"Result" => "Enum('Sent, Failed, Bounced', 'Sent')",
);
}
class Newsletter_Recipient extends DataObject {
static $db = array(
"ParentID" => "Int",

View File

@ -29,7 +29,7 @@ class NewsletterEmailProcess extends BatchProcess {
while($this->current < $max) {
$index = $this->current++;
$member = $this->objects[$index];
// 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}'");
@ -59,7 +59,7 @@ class NewsletterEmailProcess extends BatchProcess {
$e->populateTemplate( array( 'Member' => $member, 'FirstName' => $member->FirstName ) );
$this->sendToAddress( $e, $address, $this->messageID );
$this->sendToAddress( $e, $address, $this->messageID, $member);
}
}
@ -69,10 +69,29 @@ class NewsletterEmailProcess extends BatchProcess {
return parent::next();
}
private function sendToAddress( $email, $address, $messageID = null ) {
$email->setTo( $address );
$email->send( $messageID );
}
/*
* 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);
}
function complete() {
parent::complete();

View File

@ -326,7 +326,7 @@ class NewsletterAdmin extends LeftAndMain {
* Reloads the list of recipients via ajax
*/
function getrecipientslist() {
if( $_REQUEST['ajax'] ) {
if(Director::is_ajax()) {
$newsletterType = DataObject::get_by_id('NewsletterType', $this->urlParams['ID'] );
$fields = new FieldSet($memberList = new MemberTableField($this, "Recipients", $newsletterType->Group() ));
$memberList->setController($this);
@ -336,6 +336,17 @@ class NewsletterAdmin extends LeftAndMain {
}
}
/**
* Reloads the "Sent Status Report" tab via ajax
*/
function getsentstatusreport($params) {
if(Director::is_ajax()) {
$newsletter = DataObject::get_by_id( 'Newsletter', $params['ID'] );
$sent_status_report = $newsletter->renderWith("Newsletter_SentStatusReport");
return $sent_status_report;
}
}
public static function template_path() {
if(self::$template_path) return self::$template_path;
else return self::$template_path = project() . '/templates/email';

View File

@ -323,6 +323,17 @@ Behaviour.register({
// $('SendProgressBar').setText( 'Done!' );
$('SendProgressBar').reset();
// this.elements.Message.value = '';
// Reload the "Sent Status Report" tab after a Newsletter is sent
var id = this.elements.NewsletterID.value;
var request = new Ajax.Request(baseHref() + 'admin/newsletter/getsentstatusreport/' + id + '?ajax=1', {
onSuccess: function( response ) {
$('Root_SentStatusReport').innerHTML = response.responseText;
},
onFailure: function(response) {
statusMessage('Could not automatically refresh Sent Status Report tab', 'bad');
}
});
this.close();
if( response )
Ajax.Evaluator(response);
@ -359,6 +370,7 @@ Behaviour.register({
}
});
// @TODO: Check if this code is used anymore now that NewsletterList was removed.
NewsletterList = Class.create();
NewsletterList.applyTo('table.NewsletterList');
NewsletterList.prototype = {

View File

@ -0,0 +1,93 @@
<% if SentRecipients(Failed) %>
<h2 class="error">Sending to the Following Recipients Failed</h2>
<table class="CMSList">
<thead>
<tr>
<th class="Email">Email</th>
<th>Result</th>
</tr>
</thead>
<tbody>
<% control SentRecipients(Failed) %>
<tr>
<td>$Email</td>
<td>$LastEdited</td>
<td>$Result</td>
</tr>
<% end_control %>
</tbody>
</table>
<% end_if %>
<% if SentRecipients(Bounced) %>
<h2 class="error">Sending to the Following Recipients Bounced</h2>
<table class="CMSList">
<thead>
<tr>
<th class="Email">Email</th>
<th>Date</th>
<th>Result</th>
</tr>
</thead>
<tbody>
<% control SentRecipients(Bounced) %>
<tr>
<td>$Email</td>
<td>$LastEdited</td>
<td>$Result</td>
</tr>
<% end_control %>
</tbody>
</table>
<% end_if %>
<% if UnsentSubscribers %>
<h2>The Newsletter has Never Been Sent to Following Subscribers</h2>
<table class="CMSList">
<thead>
<tr>
<th class="FirstName">Firstname</th>
<th class="Surname">Surname</th>
<th class="Email">Email</th>
</tr>
</thead>
<tbody>
<% control UnsentSubscribers %>
<tr id="unsent-member-$ID">
<td>$FirstName</td>
<td>$Surname</td>
<td>$Email</td>
</tr>
<% end_control %>
</tbody>
</table>
<% end_if %>
<% if SentRecipients(Sent) %>
<h2>Sending to the Following Recipients was Successful</h2>
<table class="CMSList">
<thead>
<tr>
<th class="Email">Email</th>
<th>Date</th>
<th>Result</th>
</tr>
</thead>
<tbody>
<% control SentRecipients(Sent) %>
<tr id="sent-member-$ID">
<td>$Email</td>
<td>$LastEdited</td>
<td>$Result</td>
</tr>
<% end_control %>
</tbody>
</table>
<% end_if %>