简体   繁体   English

Play Framework 2.0.X中的邮件表单

[英]Mail form in Play Framework 2.0.X

I'm using Play Framework (2.0.4) and I wonder what is the best way to create a page with a form for sending an email. 我使用的是Play Framework(2.0.4),我想知道用发送电子邮件的表单创建页面的最佳方法是什么。 I know that there is a plugin for sending email, so this isn't a problem - I can write the controller with a method which sends the email. 我知道有一个用于发送电子邮件的插件,所以这不是问题-我可以使用发送电子邮件的方法编写控制器。

My question is more about the action I should provide (in routes file). 我的问题更多是关于我应该提供的操作(在路由文件中)。 Should I create a POST action which takes for arguments (sender name, sender email, subject, body)? 是否应该创建一个接受参数(发件人姓名,发件人电子邮件,主题,正文)的POST操作? or should I somehow create a model object which would be filled in the form and pass to the action in the controller? 还是应该以某种方式创建一个模型对象,该模型对象将填写在表单中并传递给控制器​​中的操作? What is the best practice? 最佳做法是什么? And how to glue it properly (how should the action look in the routes file, how should the view look like)? 以及如何正确地粘贴它(动作应在route文件中如何显示,视图应如何显示)?

You need two views - one with form (let's call it mailForm ), second - with body ( bodyHtml ) of the mail. 您需要两个视图-一个视图具有表单(我们将其mailForm ),第二个bodyHtml具有该邮件的正文( bodyHtml )。 (optionally you can create bodyTxt if you want to send HTML and TXT version. (如果要发送HTML和TXT版本,则可以选择创建bodyTxt

Dedicated model will be good helper, as it will help you use the Play's Form<T> , also if required you will be able to store sent messages in DB. 专用模型将是一个很好的帮助,因为它将帮助您使用Play的Form<T> ,如果需要,您也可以将发送的消息存储在DB中。 Anyway you can also operate on the map of Strings - especially if you plan to make many dynamic forms (with unknown number of fields). 无论如何,您还可以在Strings的映射上进行操作-尤其是当您计划制作许多动态表单(字段数未知)时。

After filling the form it will go to for an example sendEmail() action, where you need to fill the Form ( bindFromRequest ) eventually create the object and save to DB and finally pass to bodyHtml view as argument. 填写表单后,将转到一个示例sendEmail()操作,在该示例中,您需要填写Form( bindFromRequest ),最终创建对象并保存到DB,最后作为参数传递给bodyHtml视图。 Of course instead of returning rendered view as a action's result you should use it with toString - to send it with mailer. 当然,与其将呈现的视图作为操作的结果返回,还不应该将其与toString一起使用-通过邮件发送。 The action should return a redirect to some 'Thank you' page. 该操作应将重定向返回到某些“谢谢”页面。 and that's all. 就这样。

Note: Written fast, if something will be unclear, let me know ... 注意:写得很快,如果有不清楚的地方,请告诉我...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM