itora web

20Mar/100

Implementare velocemente una Newsletter in CakePHP

Link utili per costruire una newsletter in cakephp.
Utilizziamo un plugin giĆ  fatto, scaricarlo da qui
http://github.com/innovativepm/cakephp-newsletter-plugin

aggiungere questo behaviour in app/models/behaviour/
http://bakery.cakephp.org/articles/view/add-delete-habtm-behavior

Scaricare swift http://swiftmailer.org/download ed installarlo seguendo questo tutorial
http://bakery.cakephp.org/articles/view/updated-swiftmailer-4-xx-component-with-attachments-and-plugins

Assicurarsi che nell'appController ci sia un metodo SendMail, ovviamente con il component swiftMailer incluso.
Nel plugin scaricato, sono presenti dei file di configurazione, modificarli a seconda delle vostre esigenze
http://github.com/fabiokr/cakephp-newsletter-plugin

aggiungere la chiamata a jquery nella cartella del plugin newsletter
in mail/admin_send.ctp

link("jquery.min.js"); 

modificare i link che nel nostro percorso di navigazione richiamano parti relative al plugin, es "www.miosito.com/newsletter".
http://book.cakephp.org/view/868/Plugin-routing

Importante!
Il plugin richiede che nel vostro app controller ci sia un metodo sendEmail. Se usate questa funzione per l'invio di mal non legate alla newsletter, potrebbe non servirvi il template della newsletter.
Quindi nel metodo dovete specifcare il vostro template (linea 10).

class AppController extends Controller{
	var $components = array('Auth','SwiftMailer');

	function sendEmail($subject, $viewSend=null, $to=null, $from = null, $fromName = null) {
		$this->SwiftMailer->smtpHost = 'localhost';
		//$this->SwiftMailer->sendAs = 'html';
		if($to == null) {
			$to = Configure::read('Newsletter.from_email');
		}
		if($viewSend == null) {
			$view = Configure::read('Newsletter.default_view');
			$this->set('message', 'My message');
		}else{
			$view = 'newsletter';
			$this->set('message', $viewSend);
		}
		$this->SwiftMailer->from         = $from != null ? $from : Configure::read('Newsletter.from_email');
		$this->SwiftMailer->fromName     = $fromName != null ? $fromName : Configure::read('Newsletter.from');
		$this->SwiftMailer->to           = $to;
		$method           = 'smtp';

		try {
			if(!$this->SwiftMailer->send($view, $subject, $method, true)) {
				$this->log("Error sending email");
			}
		}
		catch(Exception $e) {
			$this->log("Failed to send email: ".$e->getMessage());
		}

	}

}