Question

I have a webpage (using angular and some javascript) that requests data from a server and displays as a table.

I would like to send a copy of this webpage by email, (using node and nodemailer).

I use a http.get to get the html of the webpage, however naturally this only gets me the HTML. I would like to get the page as shown in the browser (after loading has completed) into the email.

How would I accomplish this?

This is my own attempt that naturally only works with a pure html page, and so lacks the javascript and the angular stuff. Please note that this uses coffeescript.

nodemailer = require("nodemailer")
http = require 'http'

sendmail = (html)->
  transport = nodemailer.createTransport("SMTP",{
    service: "Gmail"
    auth:
      user: u
      pass: p
  })

  mailOptions =
    from: f
    to: t
    subject: s
    html: html

  transport.sendMail mailOptions, (err,res)->
    console.log err if err?
    do transport.close

http.get 'http://localhost:3000/something', (res) ->
  res.on 'data', (data) ->
    sendmail data.toString()
Was it helpful?

Solution

PhantomJS is likely the solution you're looking for. It's a headless Webkit browser that will render your pages on the server to a form that could be mailed. The main application of this is enabling SEO for Single Page Applications (SPAs). Lots more info here: "Single-page" JS websites and SEO

OTHER TIPS

You can't add Javascript to an HTML email. Neither should you be able to do so. That would be a huge security issue.

The best approach would probably be to send out a basic HTML email with a link to a webpage that does what you want the email to do.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top