2
array = ["abc","mohasdan","321","324324"]
recipients = ["xxx@example.com"]
recipients_formatted = []
recipients.each {|s| recipients_formatted << "To: #{s}"}
message = <<MESSAGE_END
From: xxx@example.com <xxx@example.com>
#{recipients_formatted}

MIME-Version: 1.0
Content-type: text/html
Subject: Chef Report
<html>
<head>
<style type="text/css">
table {border-collapse:collapse;}
table, td, th {border:1px solid black;padding:5px;}
</style>
</head>
<body>
<h2>Chef Check-in Report</h2>
<p>
<p>
<table border=1>
<tr>
<th>Node</th>
<th>Time Since Last Check-in (Mins)</th>
</tr>
</table>
</body>
</html>
MESSAGE_END

Net::SMTP.start('localhost') do |smtp|
smtp.send_message message, 'xxx@example.com',
                           recipients
end

上記のコードは、次のようなテーブルを含む電子メールを送信します。

Chef  (column1)      |  Check-in Report(column2)   (row 1)  

配列の内容を、電子メールで送信されるテーブルに配置します。行1の最初の列の最初の配列要素、行1の2番目の列の2番目の配列要素が必要です。行2の最初の列の3番目の配列要素、行2の2番目の列の4番目の配列要素というように続きます。

4

1 に答える 1

5

次のようなものを試してください。

custom_rows = ""
array.each_slice(2) do |row| 
  custom_rows << "<tr><td>#{row[0]}</td><td>#{row[1]}</td></tr>"
end

あなたのmessageプットの中より#{custom_rows}

message = <<MESSAGE_END
From: xxx@example.com <xxx@example.com>
#{recipients_formatted}

MIME-Version: 1.0
Content-type: text/html
Subject: Chef Report
<html>
<head>
<style type="text/css">
table {border-collapse:collapse;}
table, td, th {border:1px solid black;padding:5px;}
</style>
</head>
<body>
<h2>Chef Check-in Report</h2>
<p>
<p>
<table border=1>
<tr>
<th>Node</th>
<th>Time Since Last Check-in (Mins)</th>
</tr>
#{custom_rows}
</table>
</body>
</html>
于 2012-12-24T22:26:42.617 に答える