如何在 Zend Framework 中制作电子邮件模板?

发布于 2024-07-30 08:21:33 字数 338 浏览 5 评论 0原文

我想在 Zend Framework 中制作电子邮件模板。

例如,

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

我想制作这个文件,在 Zend 框架中获取它,设置这些参数(用户名、朋友),然后发送电子邮件。

我怎样才能做到这一点? Zend 支持这个吗?

I want to make email templates in Zend Framework.

For example,

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

I want to make this file, get it in Zend framework, set those parameters (username, friend) and then send the email.

How can I do that? Does Zend support this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

逆流 2024-08-06 08:21:34

只是为了完成 ArneRie 在这里的答案(这已经非常相关),我喜欢在我的项目中拥有一个类来同时处理电子邮件发送和不同的模板。

例如,此类可以位于您的库中 (/library/My/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 是模板变量。

Just to complete ArneRie's answer here (which is already very relevant), I like to have in my projects a class to handle email sending and different templates at the same time.

This class could be in your library for example (/library/My/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();
    }
}

I like have some Zend_Mail options (such as transport, default sender name, etc.) set in my application.ini as follows:

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

And now, from anywhere in my application, I can simply send an email using for instance:

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

This will set the template, and template variables through a magic setter.
At last, my templates are localized in APPLICATION_PATH "/modules/default/views/scripts/emails" (can be changed in the application.ini). A typical template would be:

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

where $this->email and $this->token are the template variables.

奢望 2024-08-06 08:21:33

嗨,这确实很常见。

创建一个视图脚本,例如:/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();

Hi this is realy common.

Create an view script like : /views/emails/template.phtml

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

and when creating the email :

// 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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文