简体   繁体   中英

Ruby 'Mail' gem and corrupted HTML email

I am creating an html table and then sending it with the Mail gem and getting some unexpected html tags in the output like this:

< /tr>
DAY PRODUCT_CODE    USAGE_TYPE  LATE_ARRIVING_USAGE PERCENT
27-SEP-14   sdfgdfg sdfgdf  0.001   0.000%
27-SEP-14   sdfgdg  gfdgs   0.1 39  0.000%
27-SEP-14   sdfgdg  sdfgdsfg    0.001   0.000%

UPDATE: In the for loop below at 'HERE', if I place something like 'ANYTHING' after tr but inside the quotes, the left over tr at the top of the table just turns into 'ANYTHING' and the tr goes away. How does this happen??

I am not sure how that tr tag is getting there. Depending on how I concatenate the string some other ones show up like td style... but the way i'm doing now only has that extra tr tag. This is the for loop:

def get_file_as_html(filename)
begin

  data = ""
  if @has_report_header == "Y" && @report_header != ""
    data = "</br><table border =\"1\" style=\"border-width: 1px; border-style: solid;\"><tr>"
    data_row = @report_header.split(",")
    data_row.each_with_index do |elem,idx|
      str = elem.gsub("\"","").strip
      data += "<th style= 'align: center; width: 100;'>#{str}</th>"
    end
    data += "</tr>" # HERE --> "</tr>ANYTHING"
  end

  data_file = CSV.read(filename, { :col_sep => "\t" , :quote_char => '"' })
  #puts data_file
  data_file.each do |data_row|
    new_data_row = "<tr>"
    data_row.each_with_index do |elem,idx|
      new_data_row += "<td style= 'text-align: left;align:left; width: 100;'>#{elem}</td>"
    end
    new_data_row += "</tr>"
    data += new_data_row
  end

  table_footer = "</table></br></br>"
  data += table_footer

  return data

rescue Exception => e

  raise "PROBLEM_IN_HTML_REPORT_GENERATION"
end
end

I have 'put' the 'data' variable to the console and then into a fiddle and it looks perfect. When I send it the content_type is 'text/html':

def send_attachment(filename,s3url,job_instance_id)
activity_id = get_activity_info(job_instance_id)
recipient_list = get_recipient_list()
my_body_is_ready = get_file_as_html(filename)

mail = Mail.deliver do
  to      'xxx@xxx.com'
  from    'xxx@xxx.com'
  subject @subject

  text_part do
    body 'This is plain text'
  end

  html_part do
    content_type 'text/html; charset=UTF-8'
    body my_body_is_ready
  end
end

puts mail.to_s #=>

end

Anyone know what could be corrupting the message? Thanks, Tim

It looks like the gem checks validity of HTML somehow.

The structure of table tag is:

<table>
  <th>
    <td></td><td></td><td></td>
  </th>
  <tr>
    <td></td><td></td><td></td>
  </tr>
  <tr>
    <td></td><td></td><td></td>
  </tr>
</table>

You misplaced th inside tr instead of td inside th . th states for table header, tr for table row.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM