2012-12-24 69 views
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 

上面的代碼發送包含表的somthing像這樣的電子郵件:如何將數組元素放入HTML表格中?

Chef (column1)  | Check-in Report(column2) (row 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: [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> 
+0

缺少插值算#{}。 – user2398029

+0

是的。謝謝! –

+0

謝謝@AnatoliyKukuland ..但是我把任何東西放在message = << MESSAGE_END後嵌入爲html ..我不知道如何區分ruby代碼和html,同時構建郵件正文 –

相關問題