Validating HTML Emails with Nokogiri, Tidy, and validator.w3.org

```ruby # We're validating the HTML generated by the Devise mailer user = User.new email: 'xxx@xxx' mail = Devise.mailer.confirmation_instructions(user, self).deliver html = mail.html_part.body.to_s ``` Validate with tidy: ```ruby File.open('/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 #{File.read('/tmp/validation-errors.txt')}" end ``` Remember to install tidy first: ```ruby $ sudo apt-get install tidy ``` Validate with Nokogiri is not so fun or accurate: ```ruby # Validate XML doc = Nokogiri.XML(html) doc.errors # Validate against a schema, if you want xsd = Nokogiri::XML::Schema(open('http://www.w3.org/2002/08/xhtml/xhtml1-strict.xsd')) xsd.validate(doc) ``` For validation with w3.org, see [this script](http://snippets.aktagon.com/snippets/640-validating-html-with-validator-w3-org-and-ruby)