2009-08-02 8 views
62

Chcę tworzyć szablony wiadomości e-mail w Zend Framework.Jak utworzyć szablon wiadomości e-mail w Zend Framework?

Na przykład

<html> 
<body> 
Dear {$username$},<br> 
This is a invitation email sent by your {$friend$}.<br> 
Regards,<br> 
Admin 
</body> 
</html> 

Chcę, aby ten plik, dostać go w Zend Framework, należy ustawić te parametry (nazwa użytkownika, przyjaciel), a następnie wysłać wiadomość.

Jak mogę to zrobić? Czy Zend wspiera to?

+3

Drogi Robercie Jones, to zaproszenie wysłane przez twojego Johna Smitha. Pozdrawiam, Admin. :) –

Odpowiedz

103

Witam, to jest naprawdę powszechne.

Utwórz skrypt widok jak: /views/emails/template.phtml

<body> 
<?php echo $this->name; ?> 
<h1>Welcome</h1> 
<?php echo $this->mysite; ?> 
</body> 

i przy tworzeniu e-mail:

// create view object 
$html = new Zend_View(); 
$html->setScriptPath(APPLICATION_PATH . '/modules/default/views/emails/'); 

// assign valeues 
$html->assign('name', 'John Doe'); 
$html->assign('site', 'limespace.de'); 

// create mail object 
$mail = new Zend_Mail('utf-8'); 

// render view 
$bodyText = $html->render('template.phtml'); 

// configure base stuff 
$mail->addTo('[email protected]'); 
$mail->setSubject('Welcome to Limespace.de'); 
$mail->setFrom('[email protected]','Limespace'); 
$mail->setBodyHtml($bodyText); 
$mail->send(); 
+13

Warto zauważyć, że jeśli jesteś w akcji kontrolera i nie zbaczałeś zbyt daleko od domyślnej architektury MVC, możesz po prostu wykorzystać istniejącą instancję widoku zamiast tworzyć nową (jeśli " nie martw się o zmienne ustalanie zakresu). '$ bodyText = $ this-> view-> render ('template.phtml')' wystarczy w większości sytuacji. – jason

22

Wystarczy pełną odpowiedź ArneRie tu (co jest już bardzo istotne), Lubię mieć w swoich projektach klasę do obsługi wysyłania wiadomości e-mail i różnych szablonów w tym samym czasie.

Klasa ta może być w bibliotece, na przykład (/ Library/Moja /Mail.php):

class My_Mail 
{ 
    // templates name 
    const SIGNUP_ACTIVATION   = "signup-activation"; 
    const JOIN_CLUB_CONFIRMATION  = "join-club-confirmation"; 


    protected $_viewSubject; 
    protected $_viewContent; 
    protected $templateVariables = array(); 
    protected $templateName; 
    protected $_mail; 
    protected $recipient; 

    public function __construct() 
    { 
     $this->_mail = new Zend_Mail(); 
     $this->_viewSubject = new Zend_View(); 
     $this->_viewContent = new Zend_View(); 
    } 

    /** 
    * Set variables for use in the templates 
    * 
    * @param string $name The name of the variable to be stored 
    * @param mixed $value The value of the variable 
    */ 
    public function __set($name, $value) 
    { 
     $this->templateVariables[$name] = $value; 
    } 

    /** 
    * Set the template file to use 
    * 
    * @param string $filename Template filename 
    */ 
    public function setTemplate($filename) 
    { 
     $this->templateName = $filename; 
    } 

    /** 
    * Set the recipient address for the email message 
    * 
    * @param string $email Email address 
    */ 
    public function setRecipient($email) 
    { 
     $this->recipient = $email; 
    } 

    /** 
    * Send email 
    * 
    * @todo Add from name 
    */ 
    public function send() 
    { 
     $config = Zend_Registry::get('config'); 
     $emailPath = $config->email->templatePath; 
     $templateVars = $config->email->template->toArray(); 

     foreach ($templateVars as $key => $value) 
     { 
      if (!array_key_exists($key, $this->templateVariables)) { 
       $this->{$key} = $value; 
      } 
     } 


     $viewSubject = $this->_viewSubject->setScriptPath($emailPath); 
     foreach ($this->templateVariables as $key => $value) { 
      $viewSubject->{$key} = $value; 
     } 
     $subject = $viewSubject->render($this->templateName . '.subj.tpl'); 


     $viewContent = $this->_viewContent->setScriptPath($emailPath); 
     foreach ($this->templateVariables as $key => $value) { 
      $viewContent->{$key} = $value; 
     } 
     $html = $viewContent->render($this->templateName . '.tpl'); 

     $this->_mail->addTo($this->recipient); 
     $this->_mail->setSubject($subject); 
     $this->_mail->setBodyHtml($html); 

     $this->_mail->send(); 
    } 
} 

lubię mieć kilka opcji Zend_Mail (takich jak transport, domyślne nazwy nadawcy itp) ustawić w moim application.ini następująco:

;------------------------------------------------------------------------------ 
;; Email 
;------------------------------------------------------------------------------ 
resources.mail.transport.type  = smtp 
resources.mail.transport.host  = "192.168.1.8" 
;resources.mail.transport.auth  = login 
;resources.mail.transport.username = username 
;resources.mail.transport.password = password 
;resources.mail.transport.register = true 
resources.mail.defaultFrom.email = [email protected] 
resources.mail.defaultFrom.name  = "My Site Name" 
resources.mail.defaultReplyTo.email = [email protected] 
resources.mail.defaultReplyTo.name = "My Site Name" 

email.templatePath = APPLICATION_PATH "/modules/default/views/scripts/emails" 
email.template.newsletter = "My Site Name - Newsletter" // default templates 

a teraz, z dowolnego miejsca w mojej aplikacji, mogę po prostu wysłać e-maila na przykład:

$mail = new My_Mail; 
    $mail->setRecipient("[email protected]"); 
    $mail->setTemplate(My_Mail::SIGNUP_ACTIVATION); 
    $mail->email = $user->email; 
    $mail->token = $token; // generate token for activation link 
    $mail->firstName = $user->firstName; 
    $mail->lastName = $user->lastName; 
    $mail->send(); 

Spowoduje to ustawienie szablonu i zmiennych szablonu za pomocą zestawu . W końcu moje szablony są zlokalizowane w APPLICATION_PATH "/ modules/default/views/scripts/e-mail" (można zmienić w aplikacji application.ini). Typowym szablon będzie:

// in /views/scripts/emails/signup-activation.tpl 
<p> Hi,<br /><br /> You almost done, please finish your registration:<br /> 
<a href="http://www.example.com 
    <?= $this->url(array('controller' => 'account', 
         'action'  => 'index', 
         'e'   => $this->email, 
         't'   => $this->token), 'default', true) ?> 
    ">Click here</a> 
</p> 

// in /views/scripts/emails/signup-activation.subj.tpl 
My Site Name - Account Activation Link 

gdzie $this->email i $this->token są zmienne szablonu.

+3

+1, Robiąc to w podobny sposób, ale zamiast DI dla biednych, rozwijam Zend_Mail –