Search code examples
emailmeteoriron-router

Meteor Sending dynamic urls through email


I'm trying to send a dynamic url using a template. I want to direct users to a specific link, but I'm not sure how to do that dynamically? I can just hardcode the url, but would prefer to use dynamic urls in case I make changes in the future.

here's what I have, I realize that trying to store a url variable like this

url: "{{pathFor 'welcome'}}"

is pretty dumb, but I can't figure out how to do this. Any help is appreciated! This is what I have:

var dataContext = {
      message: "To set up your profile follow the link",
      url: "{{pathFor 'welcome'}}",
      hyperlinkText: "Get Started",
      title: "Welcome to site!"
    };


    var html = Blaze.toHTMLWithData(Template.emailTemplate, dataContext);
    var from = "myemail@mysite.com";
    var to = currentUser.emails[0].address;
    var subject = "Welcome to site";

    Meteor.call("sendEmail", to, from, subject, html);

Solution

  • In javascript just evaluate Router.path(pathName) to get the path. You can get the base path with Meteor.absoluteUrl(). Note that the former will include a leading / while the latter includes a trailing / so you'll need to remove one. Ex:

    function dynamicPath(pathName){
      return Meteor.absoluteUrl() + Router.path(pathName).substr(1);
    }