Mail Provider not properly templating data


#1

Hello,

I’ve boiled my problem down as simply as I can, and still can’t seem to figure out what’s going on with my Mail provider.

I have a very simple method that pulls the first row of my reps table.
I then call the Mail provider and pass the returned result of my Lucid query. The email fires, but the data isn’t rendered in the email template.

Directly below that, I create a view with the same data and the edge file. Yet the data is passed and renders properly.

I’ve never had issues with this before. Any insight would be appreciated.

  async sendMail({ view }) {
    let rep = await REP.find(1)
    console.log(rep.toJSON())

    try {
      await Mail.send('emails.test', rep.toJSON(), (message) => {
        message
          .to('<mypersonalemail@example.com>')
          .from(Env.get('MAIL_USERNAME'))
          .subject('Welcome to yardstick')
      })

      return view.render('emails.test', {
        rep
      })
    } catch (err) {
      console.log(chalk.red(err.message))
    }
  }

I’ve tried serializing before passing the data to my Mail method. I’ve copy and pasted the example in the docs 2 or 3 times. I just can’t figure out what’s going on here. I’m not getting any errors in my catch block either.

Thanks in advance.

UPDATE: I was able to render the FIRST_NAME column of my table row by changing {{rep.FIRST_NAME}} to {{FIRST_NAME}}, but that broke my view template.

Ultimately, I am trying to send an email to my sales team with a small amount of tabular data, and I will need to be able to pass an array of objects containing all of the sales reps data. Is this simply not possible with the Mail provider? How do I render iterated data in an email?


#2

When passing Lucid result to views, make sure to call toJSON() before passing it

view.render('view-name', { rep: rep.toJSON() })

#3

Got ya.

Why doesn’t ‘rep.toJSON()’ worn within the mail function though?


#4

@virk Please see the following gists and screenshots. I’ve recreated the errors in as little code as possible.

I’m hoping this illustrates the problem. The mail function isn’t behaving as expected. What works just fine with the view function, doesn’t work with the mail function. My data isn’t rendering within the email, but is rendering within the browser.

My controller:

The edge template:

The JSON response for the data object being passed to the sendMail method on line 59 of the controller:

The email that is rendered from line 62 of the controller:

The browser template rendering properly from line 69 of the controller:


#5

It’s coz when you are rendering the view, you are passing data as a key to the template

view.render('name', { data })

And when calling Mail.send, you are passing data itself as a reference

Mail.send('name', data)

Simply make the 2nd parameter consistent


#6

I could kiss you right now.

Thanks so much, @virk! That fixed it.