emails snippets

Validating HTML Emails with Nokogiri, Tidy, and

Tagged devise, emails, nokogiri, validate, tidy  Languages ruby
# We're validating the HTML generated by the Devise mailer
user = email: '[email protected]'
mail = Devise.mailer.confirmation_instructions(user, self).deliver
html = mail.html_part.body.to_s

Validate with tidy:'/tmp/html-validation.html', 'w') { |f| f << html }
unless system "tidy -errors -q -f /tmp/validation-errors.txt /tmp/html-validation.html"
  fail "Validation failed:\n #{'/tmp/validation-errors.txt')}"

Remember to install tidy first:

$ sudo apt-get install tidy

Validate with Nokogiri is not so fun or accurate:

# Validate XML
doc = Nokogiri.XML(html)

# Validate against a schema, if you want
xsd = Nokogiri::XML::Schema(open(''))

For validation with, see this script