1 <?php
2
3 4 5 6 7
8 class NewsletterEmailProcess extends BatchProcess {
9
10 protected $subject;
11 protected $body;
12 protected $from;
13 protected $newsletter;
14 protected $nlType;
15 protected $messageID;
16
17 18 19 20 21
22 function __construct( $subject, $body, $from, $newsletter, $nlType, $messageID = null, $recipients) {
23
24 $this->subject = $subject;
25 $this->body = $body;
26 $this->from = $from;
27 $this->newsletter = $newsletter;
28 $this->nlType = $nlType;
29 $this->messageID = $messageID;
30
31 parent::__construct( $recipients );
32
33 }
34
35 function next( $count = 10 ) {
36 $max = $this->current + $count;
37
38 $max = $max < count( $this->objects ) ? $max : count( $this->objects );
39
40 while($this->current < $max) {
41 $index = $this->current++;
42 $member = $this->objects[$index];
43
44
45
46 if(defined('DB::USE_ANSI_SQL')) {
47 $unsubscribeRecord = DataObject::get_one('UnsubscribeRecord', "\"MemberID\"='{$member->ID}' AND \"NewsletterTypeID\"='{$this->nlType->ID}'");
48 } else {
49 $unsubscribeRecord = DataObject::get_one('UnsubscribeRecord', "`MemberID`='{$member->ID}' AND `NewsletterTypeID`='{$this->nlType->ID}'");
50 }
51
52 if( !$unsubscribeRecord ) {
53
54 $address = $member->Email;
55
56 57 58
59 if($member->BlacklistedEmail && NewsletterEmailBlacklist::isBlocked($address)){
60 $bounceRecord = new Email_BounceRecord();
61 $bounceRecord->BounceEmail = $member->Email;
62 $bounceRecord->BounceTime = date("Y-m-d H:i:s",time());
63 $bounceRecord->BounceMessage = "BlackListed Email";
64 $bounceRecord->MemberID = $member->ID;
65 $bounceRecord->write();
66
67
68 $newsletter = new Newsletter_SentRecipient();
69 $newsletter->Email = $address;
70 $newsletter->MemberID = $member->ID;
71 $newsletter->Result = 'BlackListed';
72 $newsletter->ParentID = $this->newsletter->ID;
73 $newsletter->write();
74
75 } else {
76 $e = new Newsletter_Email($this->nlType);
77 $e->setBody( $this->body );
78 $e->setSubject( $this->subject );
79 $e->setFrom( $this->from );
80 $e->setTemplate( $this->nlType->Template );
81
82 $nameForEmail = (method_exists($member, "getNameForEmail")) ? $member->getNameForEmail() : false;
83
84 $e->populateTemplate(array(
85 'Member' => $member,
86 'FirstName' => $member->FirstName,
87 'NameForEmail'=> $nameForEmail,
88 'NewsLetterData'=> $this->newsletter
89 ));
90 $this->sendToAddress($e, $address, $this->messageID, $member);
91 }
92 }
93 }
94
95 if( $this->current >= count( $this->objects ) )
96 return $this->complete();
97 else
98 return parent::next();
99 }
100
101 102 103 104 105
106 private function sendToAddress( $email, $address, $messageID = null, $member) {
107 $email->setTo( $address );
108 $result = $email->send( $messageID );
109
110 $newsletter = new Newsletter_SentRecipient();
111 $newsletter->Email = $address;
112 $newsletter->MemberID = $member->ID;
113
114 if ($result == true) {
115 $newsletter->Result = 'Sent';
116 } else {
117 $newsletter->Result = 'Failed';
118 }
119 $newsletter->ParentID = $this->newsletter->ID;
120 $newsletter->write();
121
122
123 }
124
125 function complete() {
126 parent::complete();
127
128 if( $this->newsletter->SentDate ) {
129 $resent = true;
130 } else {
131 $resent = false;
132 }
133
134 $this->newsletter->SentDate = 'now';
135 $this->newsletter->Status = 'Send';
136 $this->newsletter->write();
137
138
139 if( $resent ) {
140 return "resent_ok( '{$this->nlType->ID}', '{$this->newsletter->ID}', '".count( $this->objects )."' ); ";
141 } else {
142 return "draft_sent_ok( '{$this->nlType->ID}', '{$this->newsletter->ID}', '".count( $this->objects )."' ); ";
143 }
144 }
145 }
146 ?>
[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the
Silverstripe Forums to ask development related questions.
-