1

I am sending an email in my Rails 2 app and after a bit of hacking and learning about emails in Rails it now works fine.

Now I am just trying to add an attachment which in theory should probably be straightforward but I seem to be having issues with it.

I am using mailcatcher to preview the emails in the development environment and I can see the attachment headers in the email source but nothing is shown in mailcatcher (docs say it supports attachments)

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

The email is being sent in a controller like so

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)

Just for completeness, my view is 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

In the source of the email, after the closing html tag I can see

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

*base 64 encoded stuff*

--mimepart_522da7e7e5959_a0885cd9314396--

If I have done anything bonkers then it is because I haven't sent any emails in Rails yet so still figuring things out.

4

1 回答 1

0

I can only assume this was a problem with mailcatcher, by setting up development emails to be sent to a gmail account, the emails were sent correctly with attachments.

environments/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
}

I used an existing app email address hosted on gmail, so not strictly an username@gmail.com / username@googlemail.com but I would think that those addresses would also work with the above.

于 2013-09-10T12:28:22.227 回答