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

1 Answer 1

5

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>
Sign up to request clarification or add additional context in comments.

4 Comments

Missing the interpolation operator #{}.
Thanks @AnatoliyKukuland .. But anything that i put after message = <<MESSAGE_END gets embedded as html .. I dont know how to differentiate ruby code from html , while building the email body
@AnatoliyKukul : "my_array.each_slice(2) do |row| " goes in as html content too . So it says undefined "row" .. if i put it above message = <<MESSAGE_END , it asks for end statement
@AnatoliyKukul : Thanks a ton Boss .. Your edited code helped me ... Cheers !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.