Aldrin Dela Cruz
Aldrin Dela Cruz

Reputation: 205

Rails 2 - sending pdf via emailer

I'm using ruby 1.8.7, rails 2.3.4, rubygems 1.3.6, windows 7 home basic.

Objective: to convert a html page into pdf and send it via email. (like an online receipt)

Here's what i used: prawn (for pdf) emailer (for email)

question: how to do i send an email to a real email address? All i got from the tutorial is sending an "email" that can be seen in command prompt. that's all. another question is how to generate pdf's on the fly, meaning there should be no file generated and attach it to an email? It's really hard and I have been working on this for weeks now. thanks.

Upvotes: 0

Views: 446

Answers (1)

Chris Bailey
Chris Bailey

Reputation: 4136

The precise answer to your question depends on precisely how you're generating your pdfs, but here's an example that should work:

1) In your controller file (as part of an action)

pdf_doc = Prawn::Document.new()
pdf.text "Hello, world" # etc, etc
data_string = pdf_doc.render
user = '[email protected]'

Emailer.deliver_email_with_attachment(user,data_string)

2) In your mailer file (e.g. app/models/emailer.rb)

class Emailer < ActionMailer::Base

  def email_with_attachment(user, data)
    # setup your email as normal using @from, @subject, etc
    @from = user

    # now attach the pdf
    attachment :content_type => "application/pdf", :body => data
  end

end

for more information on attachments in ActionMailer see The rails docs

EDIT: You should also make sure you've edited your config file(s) to make sure your rails app can send emails. As you're using windows you'll need to configure sending by SMTP:

config/environments/development.rb

config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
  :address              => "my.smtp-server.com",
  :port                 => 25,
  :domain               => 'iwantgreatcare.org',
  :user_name            => 'username',
  :password             => 'password',
  :authentication       => 'plain',
}    

For more information on configuring smtp setting see the Rails ActionMailer guide

Upvotes: 1

Related Questions