2017-02-14 555 views
1

自動化的郵件格式我想通過SMTP發送郵件自動像像 - daily_closing.html.erb如何設計紅寶石

<p>Dear Sir,</p> 

<p><b>Pending Indents :<%= @pending_indents %></b>&nbsp;&nbsp;<%= link_to '>>>', "http://www.industryprime.com/notifications/pending_indents" %> </p> 

<p><b>Overdue Deliveries :<%= @overdue_delivery %></b>&nbsp;&nbsp;<%= link_to '>>>', "http://www.industryprime.com/notifications/pending_purchase_orders" %></p> 
<p><b>Unapproved POs: <%= @unapproved_po_count %></b>&nbsp;&nbsp;<%= link_to '>>>', "http://www.industryprime.com/notifications/po_for_approval" %></p> 

<p>Yesterday's GRNs</p> 

<table border="1"> 
<thead> 
<tr> 
    <th>Warehouse</th> 
    <th>Date</th> 
    <th>Item</th> 
    <th>Make/Catno</th> 
    <th>UOM</th> 
    <th>Qty</th> 
    <th>Closing</th> 
    <th></th>  
    </tr> 
</thead> 


    <tbody> 
    <% @grns.each do |grn| %> 
    <tr> 

    <td><%= grn.warehouse.description %></td> 
    <td><%= grn.date.strftime("%d/%m/%y") %></td> 
    <td><%= grn.item.description %></td> 

    <% if grn.make_id != nil %> 
    <td><%= (grn.make.brand.name)+"/"+(grn.make.catno) %></td> 
    <%else %> 
    <td></td> 
    <% end%> 

    <td><%= grn.item.uom.shortform %></td> 

    <td><%= grn.quantity.round(2) %></td> 

    <td><%= grn.balance_quantity.round(2) %></td> 
    <% 
    if grn.make_id==nil 
    params = { 
     :item => grn.item_id, 
     :make => 0, 
     :date => Time.now, 
     :warehouse => grn.warehouse_id 
    } 
    else 
    params = { 
     :item => grn.item_id, 
     :make => grn.make_id, 
     :date => Time.now, 
     :warehouse => grn.warehouse_id 
    } 
    end 
    url = "http://www.industryprime.com/stockledgers?#{params.to_query}" 
    %> 

    <td><%= link_to '>>>', url %></td> 

    </tr> 
    <% end %> 
    </tbody> 
</table> 


<p>Yesterday's ISSUEs</p> 

<table border="1"> 
    <thead> 
    <tr> 
    <th>Warehouse</th> 
<th>Date</th> 
    <th>Item</th> 
    <th>Make/Catno</th> 
    <th>UOM</th> 
    <th>Qty</th> 
    <th>Closing</th> 
    <th>To</th> 
<th>Cost Centre</th>  
<th>CF</th> 
<th></th> 
</tr> 
    </thead> 


    <tbody> 
<% @issues.each do |issue| %> 
    <tr> 

    <td><%= issue.warehouse.description %></td> 
    <td><%= issue.date.strftime("%d/%m/%y") %></td> 
    <td><%= issue.item.description %></td> 


    <% if issue.make_id != nil %> 
    <td><%= (issue.make.brand.name)+"/"+(issue.make.catno) %></td> 
    <%else %> 
    <td></td> 
    <% end%> 

    <td><%= issue.item.uom.shortform %></td> 

    <td><%= issue.quantity.round(2) %></td> 

    <td><%= issue.balance_quantity.round(2) %></td> 

    <td><%= Issue.find(issue.ref_id).parentissue.worker.name %></td> 

    <% group = Costcentre.find(Issue.find(issue.ref_id).costcentre_id) 
    @leafgroup=Issue.find(issue.ref_id).costcentre.description 
    if group.predecessor !=nil 
     @pre=-1 
     @leafgroup="" 
     @group=group 
     until @pre==0 do 
     @leafgroup="." + @[email protected] 
     if @pre!=nil 
      @group=Costcentre.find_by_id(@group.predecessor) 
      [email protected]==nil 
      @pre=0 
      else 
      @[email protected] 
      end 
     else 
      @pre=0 
     end 
     end 
     @leafgroup[0]="" 
    end 
     %> 


    <td><%= @leafgroup %></td> 
    <td><%= Issue.find(issue.ref_id).costcentre_frequency %></td> 
    <% 
    if issue.make_id==nil 
    params = { 
     :item => issue.item_id, 
     :make => 0, 
     :date => Time.now, 
     :warehouse => issue.warehouse_id 
    } 
    else 
    params = { 
     :item => issue.item_id, 
     :make => issue.make_id, 
     :date => Time.now, 
     :warehouse => issue.warehouse_id 
    } 
    end 
    url = "http://www.industryprime.com/stockledgers?#{params.to_query}" 
    %> 

    <td><%= link_to '>>>', url %></td> 
    </tr> 
<% end %> 
</tbody> 
    </table> 

<p>Warm regards,</p> 

當我將這個內容在郵件的shows- enter image description here

但我想設計一些CSS顏色..ie,行bgcolor..and其他一些顏色..不支持郵件內容.. 我該怎麼辦?

+0

您需要將相關的樣式表或樣式添加到您的郵件發行版 – Iceman

+0

是的,請在電子郵件的html頭部定義您的css – Fallenhero

回答

1

您可以添加內聯CSS,但對於每個元素來說,這可能是乏味且艱苦的工作。您可以通過在腳本中包含外部樣式表來設置您的郵件發件人的樣式。我在我的項目中使用的是roadie-rails寶石。當您安裝視Roadie護欄的寶石,它會隨着使用的兩個主要手段:「自動」和「手動」就可以自動啓動由包括自動模塊到你的郵件(見documentation):

include Roadie::Rails::Automatic 

在樣式表文件夾添加例如mailer.css併爲您的表添加一些類。

然後你就可以在佈局應用程序/視圖/佈局/ mailer.html.erb插入mailer.css

<!DOCTYPE html> 
<html> 
    <head> 
    <link rel="stylesheet" type="text/css" href="/assets/mailer.css"> 
    </head> 

    <body> 
    <%= yield %> 
    </body> 
</html> 

當你準備好生產去配置/初始化/資產.RB並加入這行代碼預先編譯mailer.css

Rails.application.config.assets.precompile += %w(mailer.css) 

下面是步驟,使其在本地開發工作與電子郵件預覽:

# Generate mailer 
rails g mailer newsletter_mailer daily 

安裝2個寶石:

gem 'roadie-rails' 

    # Preview email in the default browser instead of sending it 
    group :development do 
    gem 'letter_opener' 
    end 

配置/環境/ development.rb添加此下面的代碼,所以您可以在瀏覽器中預覽電子郵件,而不是發送:

config.action_mailer.delivery_method = :letter_opener 

我們的郵件代碼應用程序/郵寄/ newsletter_mailer.rb

class NewsletterMailer < ApplicationMailer 

    include Roadie::Rails::Automatic 

    def daily(email) 
    mail(to: email, subject: "Welcome to our website!") 
    end 
end 

創建郵件。CSSPP /資產/樣式文件夾並添加例如一些造型:

body { 
    padding: 0; 
    background: #f7f7f7; 
    text-align: center; 
} 

.email-table { 
    width: 538px; 
    padding: 20px; 
    background: #fff; 
    margin: 40px auto; 
    text-align: left; 
    font-family: 'Helvetica Neue'; 
} 

h1 { 
    font-size: 20px; 
} 

p { 
    margin-top: 0; 
    font-size: 15px; 
} 

現在添加類我們應用程序/視圖/ newsletter_mailer/daily.html.erb文件:

<h1>Newsletter#daily</h1> 

<table class="email-table"> 
    <tbody> 
    <tr class="header-row"> 
     <td> 
     <h1>Welcome User!</h1> 
     </td> 
    </tr> 
    <tr class="body-row"> 
     <td> 
     <p>Thanks for your registering! We appreciate your interest.</p> 
     </td> 
    </tr> 
    </tbody> 
</table> 

納入佈局應用/視圖/佈局/ mailer.html.erb外部樣式表

<!DOCTYPE html> 
<html> 
    <head> 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
    <link rel="stylesheet" type="text/css" href="/assets/mailer.css"> 
    </head> 

    <body> 
    <%= yield %> 
    </body> 
</html> 

最後一步火了控制檯和發送電子郵件:rails c

NewsletterMailer.daily("[email protected]").deliver 

這將打開新的瀏覽器窗口風格的電子郵件。

我希望它能幫助

+0

感謝您的先生......完成了...更好 – Debasish

+0

您歡迎!我很高興聽到它有效4 U。乾杯! – Blackcoat77

+0

嗨,先生,有一個CSS風格的問題........開信刀工作正常,但我不能風格與內聯CSS .....你可以儘快幫助我... – Debasish

0

您需要內聯CSS添加到電子郵件,像這樣:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
    <head> 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
    <title>My Mailer</title> 
    <style type="text/css"> 
     body, td { color:#000000; font-size:14px; font-family:Verdana,Helvetica,Arial; } 
     #footer { font-size:11px; color:#555555; } 
     #footer a { color:#555555;} 
    </style> 
    </head> 
    <body> 
    // Your content here 
    </body> 
</html> 

爲了方便,你也可以使用寶石,像this之一。

0

Premailer Rails是讓你的郵件裝飾非常有用!

添加下面一行到你的Gemfile

gem 'premailer-rails'

閱讀完整的文件,包括配置here。它的工作原理與普通的html非常相似。您只需要使用stylesheet_link_tag或簡單地<link>標記明確包含css文件。