Jeevan Dongre
Jeevan Dongre

Reputation: 4649

Sending emails with attachments using Ruby on Rails via SendGrid

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 protected]", "[email protected]"],
       :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

Upvotes: 1

Views: 5905

Answers (2)

pablobm
pablobm

Reputation: 2066

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.

Upvotes: 11

Calvin Froedge
Calvin Froedge

Reputation: 16373

SendGrid is an SMTP service, and thus should function just as any other outbound SMTP service. Are you sure your syntax and filepaths are correct?

class ApplicationMailer < ActionMailer::Base
  def welcome(recipient)
    attachments['free_book.pdf'] = File.read('path/to/file.pdf')
    mail(:to => recipient, :subject => "New account information")
  end
end
  1. Verify correct syntax
  2. Verify correct filepath
  3. Verify permissions on file are set correctly
  4. Check your logs

Upvotes: 1

Related Questions