Paste URL into Email

Here is my scenario:

  • The user is registered in the system.
  • The system sends a confirmation email
  • User clicks URL in email to complete registration

In the second step, I try to inject the activation URL (resolved @@ {Controller.action ()}) via email. Email will be sent by a custom class extended from Mailer. I installed "application.baseUrl" for the development and production server by reading the following page that explains the configuration of application.baseUrl. http://www.playframework.org/documentation/1.2.4/configuration#application.baseUrl

(app / views / registerer.txt)

Click below to confirm user registration: @@{Registerer.activateUser(token)} 

(app / Notifier / MailSender.java)

 public class MailSender extends Mailer { public static void registration(User user, String token) { setSubject("User Registration Confirmation")); addRecipient(user.email); setFrom("XXXSystem < auto-mail@xxxsystem.com >"); send(user, token); } } 

(CONF / application.conf)

 application.baseUrl=http://localhost:9000/ %prod.application.baseUrl=http://www.realaddressgoeshere.com/ 

I would like to get the URL of the working server, but I get the URL for development.

Expect to receive:

 Click below to confirm user registration: http://www.realaddressgoeshere.com/registerer/activateuser?token=sometokengoeshere 

But I get:

 Click below to confirm user registration: http://127.0.0.1:9000/registerer/activateuser?token=sometokengoeshere 

What am I missing?

+6
source share
3 answers

I assume that you run the game behind some web server, such as apache in production.

By default, the game "application.baseUrl" is used only when the Request object is null (when invoking mail from a job). Here is the code from the framework

 String base = Http.Request.current() == null ? Play.configuration.getProperty("application.baseUrl", "application.baseUrl") : Http.Request.current().getBase(); 

When you call your mail from the controller, the Http.Request.current.getBase () method is called, which may be "http://127.0.0.1:9000" if you are working on the front server.

Perhaps there is some tweaking on the server to correctly transmit the request. Another option is to manually set the base portion of the URL.

In your MailSender class you can store url in a static variable

 private static String APPLICATION_URL = Play.configuration.getProperty("application.baseUrl"); 

add it to your submit method

 public class MailSender extends Mailer { public static void registration(User user, String token) { String applicationUrl = APPLICATION_URL; setSubject("User Registration Confirmation")); addRecipient(user.email); setFrom("XXXSystem < auto-mail@xxxsystem.com >"); send(user, token, applicationUrl); } } 

and use it in your mail

 ${applicationUrl}@{Registerer.activateUser(token)} 
+6
source

Perhaps you are confusing application mode with framework id ? In the next line of your application, conf, %prod refers to the id prod framework. It does not apply to prod application mode.

 %prod.application.baseUrl=http://www.realaddressgoeshere.com/ 

If you want this parameter to work, you must set the frame identifier for "prod" using the play id command. See "Documentation Documentation" for more information.

+1
source

Just in case, you still need, or someone else has the same problem ...

Since you are running the Apache server, if you use the mod_proxy and ProxyPass directives to proxy requests to localhost: 9000, you can actually save the original Host line from the incoming request to the proxy server instead of the local host: 9000 (or any other host name specified for ProxyPass directives). To do this, add the following directive to the VirtualHost apache2 configuration:

 ProxyPreserveHost On 

For more information, see the Apache documentation for the ProxyPreserveHost Directive here .

0
source

Source: https://habr.com/ru/post/907737/


All Articles