Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Embed URL in email

Here's my scenario:

  1. User registers him/herself to the system.
  2. System sends confirmation email
  3. User clicks the URL within the email to complete the registration

In the 2nd step, I'm trying to embed "activation" URL(resolved by @@{Controller.action()}) in email. Email will be sent by custom class extended from Mailer. I set the "application.baseUrl" for development and production server by reading the following page, which explains application.baseUrl setting. 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/notifiers/MailSender.java)

public class MailSender extends Mailer {
    public static void registration(User user, String token) {
        setSubject("User Registration Confirmation"));
        addRecipient(user.email);
        setFrom("XXXSystem <[email protected]>");
        send(user, token);
    }
}

(conf/application.conf)

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

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

Expecting to get:

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?

like image 850
TAT Avatar asked Feb 06 '12 03:02

TAT


People also ask

How do you make a URL clickable in email?

Select the text or picture that you want to display as a hyperlink. Press Ctrl+K. You can also right-click the text or picture and click Link on the shortcut menu. In the Insert Hyperlink box, type or paste your link in the Address box.

Can I embed a link in Gmail?

Highlight the text. Go to the task bar located at the bottom of the composition window and click the hyperlink icon, an icon that looks like a few links in a chain - or click Ctrl + K. 2) Make sure you have Web address selected. Put the link to the site in the URL link box and then click OK.


2 Answers

I guess you are running play behind some web server like apache in production.

By default in play "application.baseUrl" is only used 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 a Controller, the "Http.Request.current.getBase()" method is called which can be "http://127.0.0.1:9000" in case you are running behind a front server.

Maybe there is some tuning in the server to correctly pass the request. The other possibility is to manually set the base part of the url.

In your MailSender class you can keep the url into a static variable

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

add it in your sent 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 <[email protected]>");
        send(user, token, applicationUrl);
    }
}

and use it in your mail

${applicationUrl}@{Registerer.activateUser(token)}
like image 180
Seb Cesbron Avatar answered Sep 21 '22 16:09

Seb Cesbron


Are you maybe confusing application mode with framework id? In the following line in your application conf, %prod refers to framework id prod. It does not refer to application mode PROD.

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

If you want that setting to be in effect, you have to set the framework id to "prod" with the play id command. See Play framework documentation for more info.

like image 38
Tommi Avatar answered Sep 22 '22 16:09

Tommi