简体   繁体   中英

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. 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)? 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)?

You need two views - one with form (let's call it mailForm ), second - with body ( bodyHtml ) of the mail. (optionally you can create bodyTxt if you want to send HTML and TXT version.

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. Anyway you can also operate on the map of Strings - especially if you plan to make many dynamic forms (with unknown number of fields).

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. Of course instead of returning rendered view as a action's result you should use it with toString - to send it with mailer. 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 ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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