Rails 2 - sending pdf via emailer - ruby-on-rails

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.

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 = 'me#example.com'
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

Related

Rails, transactional email and sender validation

I would like to use Sendinblue to send transactional email from my Rails web application.
To configure my application to use Sendinblue I edited config/environments/production.rb as follows (NOTE: fireworks.com is just an example):
Rails.application.configure do
...
config.action_mailer.raise_delivery_errors = true
config.action_mailer.delivery_method = :smtp
host = 'fireworks.com'
config.action_mailer.default_url_options = { host: host }
ActionMailer::Base.smtp_settings = {
:address => 'smtp-relay.sendinblue.com',
:port => '587',
:authentication => :plain,
:user_name => ENV['SENDINBLUE_USERNAME'],
:password => ENV['SENDINBLUE_PASSWORD'],
:domain => 'fireworks.com',
:enable_starttls_auto => true
}
...
end
Do I need any gem, like sib-api-v3-sdk?
I suppose this gem is useful only for sending email using the Sendinblue API.
At Sendinblue I authenticated my domain 'fireworks.com' and created a sender, noreply#fireworks.com, which I would use for account activation.
app/mailers/application_mailer.rb
class ApplicationMailer < ActionMailer::Base
default from: "noreply#fireworks.com"
layout 'mailer'
end
In order for me to use this sender, Sendinblue wants me to verify (or validate) it.
Now the point is that noreply#fireworks.com does not exist. Michael Hartl in his tutorial uses noreply#example.com, I would use noreply#fireworks.com because I own the fireworks.com domain.
Since I would not be able to validate the sender, I thought that the only solution would be creating in my server the noreply user account, configuring Postfix to receive email from Internet and adding an MX record to my DNS zone.
I would maintain this configuration until I receive the validation email.
Is this solution necessary? Do I have any other choice?
Now the point is that noreply#fireworks.com does not exist.
Before continuing, I want to point out that this is ultimately not a good practice. You'll want some address to receive and recognize bounces.
Having said that, I just checked their documentation and it says here that instead of validating individual email addresses, you can just validate the whole domain via DNS entry, file upload or by sending them an email.

Sendgrid for an email feature within Ruby on Rails app

I would like to use Sendgrid to manage outgoing emails from a 3.2.2 version rails app I am developing with the help of a friend. She has email working from within the app using gmail, on her local/dev build. I need sendgrid up and running.
I cannot even get it to work locally.
From my development.rb file
config.action_mailer.default_url_options = { :host => 'localhost:3030' }
config.action_mailer.smtp_settings = {
:user_name => ENV['EMAIL_USERNAME'],
:password => ENV['EMAIL_PASSWORD'],
:domain => 'myapplicationdomain.com',
:address => 'smtp.sendgrid.net',
:port => 587,
:authentication => 'plain',
:enable_starttls_auto => true
}
config.action_mailer.delivery_method = :smtp
Then I have a variable file in the root of my application that includes the following:
export EMAIL_USERNAME=sendgridusername
export EMAIL_PASSWORD=sendgridpassword
export MAIL_TO=report#myapplicationdomain.com
Here is the code from my mailer
class StatusMailer < ActionMailer::Base
default from: "reports#myapplicationdomain.com"
def status_report(report)
#greeting = "Hello"
#report = report
if ENV['MAIL_TO']
email = ENV['MAIL_TO'] if ENV['MAIL_TO']
else
email = #report.user.email
end
#statuses = #report.statuses
#reviewers = #report.user.reviewers
bcc = []
#reviewers.each do |reviewer|
bcc.append(reviewer.email)
end
#bcc = bcc
mail(to: email, bcc: bcc, subject: 'Status Report')
end
end
Am I missing some other setting? What about the MAIL_TO field in the variable, I am not certain what that should be set to, or if it even needs to be declared.
Is there another file that I should be editing? I had this working several days ago, but functionality somehow slipped away :0
Rails server says that emails were sent, but sendgrid has no record; nor are the emails being received by addresses on the distribution list.
Thank you in advance for any assistance.
Do you have the following settings in your config/environments/development.rb?
config.action_mailer.raise_delivery_errors = true
config.action_mailer.perform_deliveries = true
If not, add them to your config file and restart your server.
Update:
This error suggests that you're not authenticated. Are you sure the values of your ENV['EMAIL_USERNAME'] and ENV['EMAIL_PASSWORD'] variables are present/correct?
This post:
Sendgrid / email sending issues in Ruby on Rails (hosted on Heroku)
Got me up and running. The key being putting the SMTP and sendgrid information in the environment.rb file.
I can't explain exactly why that made the difference, but it did.

Mandrill setup Ruby on Rails

I'm trying to setup the Mandrill API to send transactional emails. To start with I just want to get a welcome email sent to new users that have just successfully signed up. Once I've completed that I should be able to figure out any other additional emails. I've already coded a few bits, but to be honest I'm not entirely sure I'm doing it right! The code doesn't currently work during testing, which is really annoying.
All the code is included below, all input is welcome :)
I have both the following gems installed.
'mandrill-api'
'mandrill_mailer'
I'm unsure where to place the following code, the documentation for the gem says mail.rb in config/initializers https://github.com/renz45/mandrill_mailer (Look under 'Usage')
ActionMailer::Base.smtp_settings = {
:address => "smtp.mandrillapp.com",
:port => 587,
:user_name => ENV['MANDRILL_USERNAME'],
:password => ENV['MANDRILL_API_KEY'],
:domain => 'heroku.com'
}
ActionMailer::Base.delivery_method = :smtp
MandrillMailer.configure do |config|
config.api_key = ENV['MANDRILL_API_KEY']
end
I won't be using Action Mailer so I guess I'll just change
ActionMailer::Base.smtp_settings
to
MandrillMailer::TemplateMailer.smtp_settings
???
I have a transactional_mailer.rb file under app/mailers
class TransactionalMailer < MandrillMailer::TemplateMailer
default from: 'hello#randomemail.com'
def welcome(user)
mandrill_mail template: 'welcome_template',
subject: "hello",
to: {email: user.email, name: user.name},
vars: {
'USER_IDENTIFIER' => user.email,
'LIST_COMPANY' => 'Your Company'
},
important: true,
inline_css: true,
async: true
end
end
The app currently calls the method in the registration controller
MandrillMailer.welcome(resource).deliver
Should it be
TransactionalMailer.welcome(#user).deliver
???
It's not working during testing on the local server (rails s) I'm new to coding and trying to work out why.
All help is appreciated!
Thanks :)

Using SMTP through gmail to email via sendgrid using ruby on rails

Im doing a ruby on rails project for work and they would like to use sendgrid, but they also like gmail. With gmail it allows you to send an email from the web browser under a different alias but now also supports sending that through another smtp server instead of their own.
I was wondering if then, it would be possible to send an email from the RoR project through to gmail (so management gets to keep their nice interface and sent box), but then it would forward it through to the sendgrid SMTP servers. Just to clarify I know how to and currently can send an email through gmail as a different alias, but this is specifically to forward it through to sendgrid after it gets to gmail.
I currently have a standard setup of:
Myapp::Application.configure do
config.action_mailer.default_url_options = { :host => 'www.mygenericwebsite.com' }
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:enable_starttls_auto => true,
:address => 'smtp.gmail.com',
:port => 587,
:tls => true,
:authentication => :plain,
:domain => 'mygenericwebsite.com',
:user_name => "user#mygenericwebsite.com",
:password => "pA55w0RD"
}
class UserMailer < ActionMailer::Base
default :from => "HappyAdmin <user#mygenericwebsite.com>"
You could send via Sendgrid and BCC the Gmail address in your emails, and then apply a label to emails from the app based on the From address. Not sure if you can apply the Sent label, but another label would probably be all right. I think this would be simpler and more robust than sending each email twice.
Just wanted to point out that our product, PostageApp, will allow you to send via the Google SMTP if you are so inclined. All you have to do is add the SMTP details to your project and you're good to go.
I just checked with the personal project I have hooked up with Postage and all the emails sent out appear in the Sent Mail folder.
Let me know if that's what you're looking for, or if you have any other questions!

Rails 3 - sendgrid setup to support devise

I'm trying to get sendgrid running on my Rails 3 App with Devise, so devise can send out registration emails etc..
I added the following, config/setup_mail.rb:
ActionMailer::Base.smtp_settings = {
:address => "smtp.sendgrid.net",
:port => '25',
:domain => "XXXXXXXXX.com",
:authentication => :plain,
:user_name => "XXXXXXXXXXX#gmail.com",
:password => "XXXXXXXXXX"
}
Shouldn't that be enough for Rails + Devise to send out registration emails? Or do I need something else or a gem of some kind?
The logs show the email being generated but I don't see anything in the log about MAIL being sent successfully or erroring. And my sendgrid account still says 0/200 emails sent.
Is there a better way in Rails to see what's going on when it trys to send the email?
Thanks
You can erase the setup that you have.
heroku addons:create sendgrid:free
That is the only pieces of code you need to get email configured with heroku.
Make sure you have your host link setup which I think you did because it will cause it to crash but if you haven't:
config.action_mailer.default_url_options = { :host => 'myapp.heroku.com' }
Actually this last lien is different on rails3 so watch out for that :)
The "config" line needs to be added to your "production.rb" file.
I'm searching for the same answer myself. In intializers/devise.rb I read:
# Configure the class responsible to send e-mails.
# config.mailer = "Devise::Mailer"
I wonder if Devise has to be told to use Actionmailer.

Resources