在 Zend/PHP 中使用电子邮件模板的最佳方式是什么

发布于 2024-08-24 13:14:32 字数 1743 浏览 2 评论 0原文

我正在开发一个用户创建帐户的网站。我需要向许多海洋上的用户发送电子邮件。例如,当注册、忘记密码、订单摘要等时。我想为此使用电子邮件模板。我需要你对此的建议。我想使用一种方法,如果我更改任何电子邮件模板或登录,只需更少的时间和更改。

我考虑过以下方式:

我有一个这样的电子邮件模板表:

id
emailSubject
emailBody
emailType

例如,当用户忘记密码时:

id:

1

emailSubject:

ABC: Link for Password Change

emailBody:

<table border=0>
<tr><td>
   <b> Dear [[username]] <b/>
</td></tr>

<tr><td>
   This email is sent to you in response of your password recovery request. If you want to change your password, please click the following link:<br />
[[link1]]
<br/>
If you don't want to change your password then click the following link:<br/>
[[link2]]
</tr></td>

<tr><td>
   <b>ABC Team</b>
</td></tr>

</table>

emailType:

ForgotPassword

准备电子邮件数据:

$emailFields["to"] = "[email protected]";
$emailFields["username"] = "XYZ";
$emailFields["link1"] = "http://abc.com/changepassword?code='123'";
$emailFields["link2"] = "http://abc.com/ignorechangepasswordreq?code='123'";
$emailFields["emailTemplate"] = "ForgotPassword";

现在将所有字段传递给此函数以发送电子邮件:

sendEmail( $emailFields ){
 // Get email template from database using emailTemplate name.
 // Replace all required fields(username, link1,link2) in body.
 // set to,subject,body
 // send email using zend or php
}

I打算用上面的方法。您能否建议更好的方法或对上述逻辑进行一些更改。

谢谢。

I am working on a website where Users create their accounts. I need to send email to users on many oceans. For example when signup, forgot password, order summary etc. I want to use emails templates for this. I need your suggestions for this. I want to use a way that If I change any email template or login in less time and changes.

I have thought about the following way:

I have a table for email templates like this:

id
emailSubject
emailBody
emailType

For example when user forgot password:

id:

1

emailSubject:

ABC: Link for Password Change

emailBody:

<table border=0>
<tr><td>
   <b> Dear [[username]] <b/>
</td></tr>

<tr><td>
   This email is sent to you in response of your password recovery request. If you want to change your password, please click the following link:<br />
[[link1]]
<br/>
If you don't want to change your password then click the following link:<br/>
[[link2]]
</tr></td>

<tr><td>
   <b>ABC Team</b>
</td></tr>

</table>

emailType:

ForgotPassword

Prepare email data:

$emailFields["to"] = "[email protected]";
$emailFields["username"] = "XYZ";
$emailFields["link1"] = "http://abc.com/changepassword?code='123'";
$emailFields["link2"] = "http://abc.com/ignorechangepasswordreq?code='123'";
$emailFields["emailTemplate"] = "ForgotPassword";

Now Pass the all fields to this function to send email:

sendEmail( $emailFields ){
 // Get email template from database using emailTemplate name.
 // Replace all required fields(username, link1,link2) in body.
 // set to,subject,body
 // send email using zend or php
}

I planed to use above method. Can you suggest better way or some change in above logic.

Thanks.

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

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

发布评论

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

评论(3

情深已缘浅 2024-08-31 13:14:32

我会使用 Zend_View。将模板存储在 /views/email/EMAILNAME.phtml 中,使用所需的电子邮件模板创建一个 Zend_View 对象并向其传递所需的数据。

I'd use Zend_View. Store your templates in /views/email/EMAILNAME.phtml, create a Zend_View object with the required email template and pass it the required data.

晌融 2024-08-31 13:14:32

在我的脑海中,未经测试......但类似的东西应该有效:

$view = new Zend_View();
$view->setScriptPath( '/path/to/your/email/templates' );
$view->assign( $yourArrayOfEmailTemplateVariables );
$mail = new Zend_Mail();
// maybe leave out phtml extension here, not sure
$mail->setBodyHtml( $view->render( 'yourHtmlTemplate.phtml' ) );
$mail->setBodyText( $view->render( 'yourTextTemplate.phtml' ) );

Off the top of my head, so untested... but something similar should work:

$view = new Zend_View();
$view->setScriptPath( '/path/to/your/email/templates' );
$view->assign( $yourArrayOfEmailTemplateVariables );
$mail = new Zend_Mail();
// maybe leave out phtml extension here, not sure
$mail->setBodyHtml( $view->render( 'yourHtmlTemplate.phtml' ) );
$mail->setBodyText( $view->render( 'yourTextTemplate.phtml' ) );
尐籹人 2024-08-31 13:14:32

如前所述,Zend_View 就是这样。我是这样做的:

$template = clone($this->view);
$template->variable = $value;
$template->myObject = (object)$array;
// ...
$html = $template->render('/path/filename.phtml');

使用 Markdownify 转换为纯文本:

$converter = new Markdownify_Extra;
$text = $converter->parserString($html);

设置邮件:

$mail = new Zend_Mail();
$mail->setBodyHtml($html);
$mail->setBodyText($text);

然后设置传输和发送。

As previously mentioned, Zend_View is the way. Here is how I do this:

$template = clone($this->view);
$template->variable = $value;
$template->myObject = (object)$array;
// ...
$html = $template->render('/path/filename.phtml');

Use Markdownify to covert to plain text:

$converter = new Markdownify_Extra;
$text = $converter->parserString($html);

Setup mail:

$mail = new Zend_Mail();
$mail->setBodyHtml($html);
$mail->setBodyText($text);

Then setup Transport and send.

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