0

I'm using postmark to send emails in my rails application where the email content is auto generated. The thing is that in some cases I getting this error from postmark.

Postmark::InvalidEmailAddressError

Invalid 'HtmlBody' value. It should be less than 5242880 characters in length.

How can I truncate the HtmlBody content in order to avoid this error using ActionMailer

Hernan Acosta
  • 377
  • 5
  • 19
  • I have seen that exist something called interceptors https://guides.rubyonrails.org/action_mailer_basics.html#intercepting-and-observing-emails, I'm trying using this feature and doing something like `message.body = message.body.truncate(5242880)` inside the interceptor but how can I ensure that this is still an html ? – Hernan Acosta Sep 30 '22 at 19:27
  • This is just a straight up bad idea. You can't just truncate a string containing HTML and expect the result to be a valid HTML document - you're just going to end up with a bunch of mismatched tags and other issues. What you need to do is investigate why its so long and fix the issue - for example by cutting the amount of inline CSS and other clutter. – max Sep 30 '22 at 21:08
  • What do you think about doing something like `Nokogiri.HTML5(truncated_string).to_html` ?, to generate the html given the truncated string. – Hernan Acosta Oct 03 '22 at 15:45

0 Answers0