将PDF附加到Rails中的电子邮件2

时间:2013-09-09 11:02:01

标签: ruby-on-rails ruby ruby-on-rails-2

我正在我的Rails 2应用程序中发送电子邮件,经过一些黑客攻击并了解Rails中的电子邮件后,它现在可以正常工作。

现在我只想添加一个理论上可能应该直截了当的附件,但我似乎遇到了问题。

我正在使用mailcatcher预览开发环境中的电子邮件,我可以看到电子邮件来源中的附件标题,但邮件收集器中没有显示任何内容(文档说它支持附件)

emailer.rb

class Emailer < ActionMailer::Base
  def quotation_notification(q)
    @recipients  = q.recipient_email
    @from        = q.partner_name + "<#{q.partner_email}>"
    @subject     = "New Quotation from " + q.partner_name
    @sent_on     =  Time.now

    @quote_id         = q.quote_id
    @customer_id      = q.customer_id
    @customer_name    = q.customer_name
    @recipient_email  = q.recipient_email
    @partner_name     = q.partner_name
    @partner_email    = q.partner_email
    @partner_ref      = q.partner_ref
    @version_no       = q.version_no
    @line_items       = q.line_items
    @quotation_date   = q.quotation_date

    content_type    "multipart/alternative"

    part "text/html" do |p|
      p.body = render_message("quotation_notification.text.html.rhtml", :message => q)
    end

    attachment :content_type => "application/pdf",
        :body => File.read(RAILS_ROOT + '/pdfs/' + q.quote_id + '.pdf')

    @body[:q]    = q
  end

end

电子邮件正在这样的控制器中发送

q = QuotationEmail.new(quote_id, customer_id, customer_name, recipient_email, partner_name, partner_email, partner_ref, version_no, line_items, quotation_date)

# send email
Emailer.deliver_quotation_notification(q)

为了完整起见,我的观点为app/views/emailer/quotation_notification.text.html.rhtml

quotation_email.rb

class QuotationEmail

  attr_accessor :quote_id, :customer_id, :customer_name, :recipient_email, :partner_name, :partner_email, :partner_ref, :version_no, :line_items, :quotation_date

  def initialize(quote_id, customer_id, customer_name, recipient_email, partner_name, partner_email, partner_ref, version_no, line_items, quotation_date)
    @quote_id         = quote_id
    @customer_id      = customer_id
    @customer_name    = customer_name
    @recipient_email  = recipient_email
    @partner_name     = partner_name
    @partner_email    = partner_email
    @partner_ref      = partner_ref
    @version_no       = version_no
    @line_items       = line_items
    @quotation_date   = quotation_date
  end  
end

在电子邮件的来源中,在结束html标记后,我可以看到

--mimepart_522da7e7e5959_a0885cd9314396
Content-Type: application/pdf
Content-Transfer-Encoding: Base64
Content-Disposition: attachment

*base 64 encoded stuff*

--mimepart_522da7e7e5959_a0885cd9314396--

如果我做了一些疯狂的事情,那是因为我还没有在Rails中发送任何电子邮件,所以还在解决问题。

1 个答案:

答案 0 :(得分:0)

我只能假设这是mailcatcher的问题,通过设置发送电子邮件发送到gmail帐户,电子邮件已正确发送附件。

<强>环境/ development.rb

config.action_mailer.raise_delivery_errors = true
config.action_mailer.perform_deliveries = true
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
  :address => "smtp.gmail.com",
  :port => 587,
  :domain => "domain.co.uk",
  :user_name => "gmailaddress@domain.co.uk",
  :password => "accountpassword",
  :authentication => :plain,
  :enable_starttls_auto => true #This line is must to ensure the tls for Gmail
}

我使用了在Gmail上托管的现有应用电子邮件地址,因此不是严格意义上的username@gmail.com / username@googlemail.com,但我认为这些地址也适用于上述内容。