2009-08-02 60 views
62

我想在Zend Framework中製作電子郵件模板。如何在Zend Framework中創建電子郵件模板?

例如,

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

我想使這個文件,把它在Zend框架,設置這些參數(用戶名,朋友),然後發送電子郵件。

我該怎麼做? Zend支持這個嗎?

+3

親愛的羅伯特瓊斯,這是你的約翰史密斯發出的邀請。問候,管理員。 :) – 2010-09-21 15:51:40

回答

103

嗨,這是非常普遍的。

創建像一個視圖腳本:/views/emails/template.phtml

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

和創建電子郵件時:

// 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

值得注意的是,如果你在控制器動作中,並且你沒有偏離默認的MVC架構,你可以簡單地使用現有的視圖實例,而不是創建一個新的視圖實例(如果你'不擔心變量範圍)。 `$ bodyText = $ this-> view-> render('template.phtml')`在大多數情況下就足夠了。 – jason 2009-08-03 16:53:33

22

就在這裏完成ArneRie的回答(這已經是非常相關的),我喜歡在我的項目中有一個同時處理電子郵件發送和不同模板的類。

這個類可能是你的庫,例如(/庫/ /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(); 
    } 
} 

我喜歡有一定的Zend_Mail選項(如運輸,默認發件人名稱等。 )設置在我的application.ini如下:

;------------------------------------------------------------------------------ 
;; 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 

而現在,從我的應用程序的任何地方,我可以簡單地使用例如發送電子郵件:

$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(); 

這將通過魔術設置器設置模板和模板變量。 最後,我的模板被本地化爲APPLICATION_PATH「/ modules/default/views/scripts/emails」(可在application.ini中更改)。一個典型的模板是:

// 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 

其中$this->email$this->token都是模板變量。

+3

+1,以類似的方式做,而不是窮人的DI我擴展Zend_Mail – 2012-05-07 06:53:31