Ruby-on-rails – Sending emails with attachments using Ruby on Rails via SendGrid

emailrubyruby-on-railsruby-on-rails-3

I am trying to send email with attachments using Ruby on Rails.
I followed the instructions from the ActionMailer site.

def welcome(recipient)
  @account = recipient
  attachments['file.csv'] = File.read('/path/to/users.csv')
  mail(:to => recipient,    
       :bcc => ["email@example.com", "email2@example.com"],
       :subject => "Sending attachment")
end

I am able to receive emails but without the attachment, I am trying to attach csv file but I am getting a file called "noname" as attachment

Best Answer

I just had this problem. I was not providing a body for the email, and was sending the attachment only. Unfortunately, it appears that SendGrid gets confused by this, sending an empty email (which is expected) with an empty attachment (which is neither expected nor desired).

Therefore, the solution: provide a body for the email. In your specific case, an /app/views/application_mailer/welcome.text.erb template with a simple text, saying "See attached" or whatever appropriate.