Ruby' Mail'宝石和损坏的HTML电子邮件

时间:2014-10-03 17:10:51

标签: html ruby email for-loop gem

我正在创建一个html表,然后使用Mail gem发送它并在输出中获得一些意外的html标签,如下所示:

< /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%

更新:在'HERE'下面的for循环中,如果我在tr之后放置了类似'ANYTHING'但在引号内的东西,则表格顶部的左上方变成'ANYTHING'并且tr变为远。这是怎么发生的?

我不确定那个tr标签是如何到达那里的。根据我如何连接字符串,其他一些显示像td样式...但我现在的方式只有额外的tr标签。这是for循环:

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

我已将'data'变量'放'到控制台然后变成小提琴,它看起来很完美。当我发送它时,content_type是'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

任何人都知道什么可能会破坏消息?谢谢, 添

1 个答案:

答案 0 :(得分:0)

看起来宝石以某种方式检查HTML的有效性。

表标签的结构是:

<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>

您在thtr而不是td内的th错放了thtr表示表格标题,{{1}}表格行。