script_kiddie
script_kiddie

Reputation: 1187

How do I put array elements into a HTML table?

array = ["abc","mohasdan","321","324324"]
recipients = ["[email protected]"]
recipients_formatted = []
recipients.each {|s| recipients_formatted << "To: #{s}"}
message = <<MESSAGE_END
From: [email protected] <[email protected]>
#{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, '[email protected]',
                           recipients
end

The above code sends an email containing a table somthing like this:

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

I put the array content into the table which is to be sent through email. I want the first array element in the first column of row one, the second array element in the second column of row one. The third array element in the first column of row two, the fourth array element in the second column of row two, and so on.

Upvotes: 2

Views: 831

Answers (1)

Tolik Kukul
Tolik Kukul

Reputation: 2016

Please try something like this:

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

Than inside your message put #{custom_rows}:

message = <<MESSAGE_END
From: [email protected] <[email protected]>
#{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>

Upvotes: 5

Related Questions