-
Notifications
You must be signed in to change notification settings - Fork 29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Features/pdf receipt #223
base: master
Are you sure you want to change the base?
Features/pdf receipt #223
Conversation
Thanks David, I'll take a closer look at this soon. @jordanbyron: I asked @ruanwz to work on this, so no need to review unless I ping you. 😄 |
After some tweaks and manual testing I was able to get this to work, but as-is this will produce a broken email. (The attachments are not encoded properly, so they come across as a plain text garbled email in base64 encoding) To fix this problem, we need to do something like this: def payment_created(user, payment)
@payment = payment
file = Tempfile.new('receipt.pdf')
begin
receipt = Prawn::Receipt.new :customer_email => user.contact_email,
:customer_name => user.name,
:amount_billed => @payment.amount,
:credit_card => "xxxx-xxxx-xxxx-#{@payment.credit_card_last_four}",
:transaction_id => @payment.id
receipt.render_file file.path
# verify the receipt.pdf by looking at the doc
receipt.render_file "/tmp/test_receipt.pdf" if ENV['RAILS_ENV'] == 'test'
contents = File.binread(file.path)
attachments["receipt.pdf"] =contents
mail(:to => user.contact_email,
:subject => "Receipt for your payment to practicingruby.com"
).deliver
ensure
file.close
file.unlink
end
@payment.update_attributes(:email_sent => true)
end @ruanwz: I'll follow up via email, but I think I can take care of fixing this patch up myself. Thanks! |
@sandal I didn't noticed the email encoding issue, because I have no idea how to test it. Thank you to let me know it. Last time, I only read the actionmailer api in http://api.rubyonrails.org/classes/ActionMailer/Base.html and follow the example: 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 Seems that they don't have special handling for the attachment file encoding, maybe there are some changes in difference rails versions? |
@ruanwz: I'm not sure why the encoding was messed up, but it appears that calling Practicing Ruby uses mailcatcher, so you can test email in development by running To get it to send an email, I basically copied and pasted the relevant portions of the test file into the Rails console. |
update the pullrequest, to fix the issues in the discussion. But remaining one: how to add private gem into Travis to make test passed? |
@ruanwz: I think we will probably open source the prawn-receipt gem. I will send you an email about that soon... there are a couple small changes I'd like to make before we release it. |
When sending the notification email for the payment, generate the pdf receipt and added as an attachment in the notification email