From b46842d2ce7d9bba6f2eb836f9755a1464a5ddec Mon Sep 17 00:00:00 2001 From: Tim Pearson Date: Thu, 30 Aug 2012 22:15:15 +0800 Subject: [PATCH] Fixed Newsletters 'view' and 'edit' actions. You can now send test emails of the newsletter from the view action. --- Config/bootstrap.php | 1 + Config/email.php | 53 +++++++++++++++++++ .../Component/InfinitasActionsComponent.php | 1 + .../Controller/NewslettersController.php | 45 +++++++++++----- .../View/Newsletters/admin_view.ctp | 2 +- 5 files changed, 89 insertions(+), 13 deletions(-) create mode 100644 Config/email.php diff --git a/Config/bootstrap.php b/Config/bootstrap.php index 3d03cea2c..7e17f003b 100644 --- a/Config/bootstrap.php +++ b/Config/bootstrap.php @@ -36,6 +36,7 @@ App::uses('AppModel', 'Model'); App::uses('AppController', 'Controller'); App::uses('AppHelper', 'View/Helper'); + App::uses('InfinitasEmail', 'Core/Emails/Network/Email'); App::uses('ClearCache', 'Data.Lib'); App::uses('EventCore', 'Events.Lib'); diff --git a/Config/email.php b/Config/email.php new file mode 100644 index 000000000..248fd1679 --- /dev/null +++ b/Config/email.php @@ -0,0 +1,53 @@ + The name of a supported transport; valid options are as follows: + * Mail - Send using PHP mail function + * Smtp - Send using SMTP + * Debug - Do not send the email, just return the result + * + * You can add custom transports (or override existing transports) by adding the + * appropriate file to app/Network/Email. Transports should be named 'YourTransport.php', + * where 'Your' is the name of the transport. + * + * from => + * The origin email. See CakeEmail::from() about the valid values + * + */ +class EmailConfig { + + public $gmail = array( + 'host' => 'ssl://smtp.gmail.com', + 'port' => 465, + 'username' => 'infinitastest@gmail.com', + 'password' => 'testtesttest', + 'transport' => 'Smtp' + ); + +} diff --git a/Core/Libs/Controller/Component/InfinitasActionsComponent.php b/Core/Libs/Controller/Component/InfinitasActionsComponent.php index 4987e66b9..eb429a4f5 100644 --- a/Core/Libs/Controller/Component/InfinitasActionsComponent.php +++ b/Core/Libs/Controller/Component/InfinitasActionsComponent.php @@ -162,6 +162,7 @@ public function actionAdminEdit($id = null, $query = array()) { $query['conditions'][$this->Controller->{$this->Controller->modelClass}->alias . '.' . $this->Controller->{$this->Controller->modelClass}->primaryKey] = $id; $this->Controller->request->data = $this->Controller->{$this->Controller->modelClass}->find('first', $query); + if(empty($this->Controller->request->data)) { $this->Controller->notice('invalid'); } diff --git a/Core/Newsletter/Controller/NewslettersController.php b/Core/Newsletter/Controller/NewslettersController.php index c313433f6..4a139972a 100644 --- a/Core/Newsletter/Controller/NewslettersController.php +++ b/Core/Newsletter/Controller/NewslettersController.php @@ -244,6 +244,22 @@ public function admin_index() { } public function admin_add() { + if ($this->request->isPost()) { + $campaignId = $this->request->data['Newsletter']['campaign_id']; + + $campaign = $this->Newsletter->Campaign->find('first', array( + 'fields' => array( + 'template_id' + ), + 'conditions' => array( + 'Campaign.id' => $campaignId + ) + ) + ); + + $this->request->data['Newsletter']['template_id'] = $campaign['Campaign']['template_id']; + } + parent::admin_add(); $campaigns = $this->Newsletter->Campaign->find('list'); @@ -268,6 +284,8 @@ public function admin_view($id = null) { } $newsletter = $this->Newsletter->read(null, $id); + $templateId = $newsletter['Newsletter']['template_id']; + $template = $this->Newsletter->Template->read(null, $templateId); if (!empty($this->request->data)) { $id = $this->request->data['Newsletter']['id']; @@ -282,24 +300,20 @@ public function admin_view($id = null) { ) ); } - + $sent = 0; foreach($addresses as $address) { - $this->Email->from = 'Infinitas Test Mail <' . $newsletter['Newsletter']['from'] . '>'; - $this->Email->to = 'Test <' . $address . '>'; + $email = new InfinitasEmail('gmail'); + $email->from(array($newsletter['Newsletter']['from'] => 'Infinitas')); + $email->to($address); - $this->Email->subject = strip_tags($newsletter['Newsletter']['subject']); - $this->set('email', $newsletter['Template']['header'] . $newsletter['Newsletter']['html'] . $newsletter['Template']['footer']); + $email->subject(strip_tags($newsletter['Newsletter']['subject'])); - if ($this->Email->send()) { + if ($email->send($template['Template']['header'] . $newsletter['Newsletter']['html'] . $template['Template']['footer'])) { $sent++; } - - pr($this->Email->smtpError); - - $this->Email->reset(); - } - + } + $this->notice(sprintf(__('%s mails were sent'), $sent)); } @@ -310,6 +324,12 @@ public function admin_view($id = null) { $this->set('newsletter', $this->Newsletter->read(null, $id)); } + public function admin_edit($id = null) { + parent::admin_edit(); + + $this->set('campaigns', $this->Newsletter->Campaign->find('list')); + } + public function admin_preview($id = null) { $this->layout = 'ajax'; @@ -338,6 +358,7 @@ public function admin_preview($id = null) { ); $this->set('data', $newsletter['Template']['header'] . $newsletter['Newsletter']['html'] . $newsletter['Template']['footer']); + Configure::write('debug', 0); } } diff --git a/Core/Newsletter/View/Newsletters/admin_view.ctp b/Core/Newsletter/View/Newsletters/admin_view.ctp index 6509da610..b389e4be3 100755 --- a/Core/Newsletter/View/Newsletters/admin_view.ctp +++ b/Core/Newsletter/View/Newsletters/admin_view.ctp @@ -27,7 +27,7 @@

Form->create('Newsletter', array('action' => 'view')); - echo '

', __('Enter the email addresses you would like to send to seperated by a , {comma}.'), '

'; + echo '

', __('Enter the email addresses you would like to send to seperated by a , {comma} (but with no spaces)'), '

'; echo $this->Form->input('id', array('value' => $this->data['Newsletter']['id'])); echo $this->Form->input('email_addresses', array('type' => 'textarea', 'class' => 'title')); echo $this->Form->end('Send the test');