2017-02-14 122 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文件。