How to setup a mail interceptor in rails 3.0.3? - ruby

I am using rails 3.0.3, ruby 1.9.2-p180, mail (2.2.13). I m trying to setup a mail interceptor but I am getting the following error
/home/abhimanyu/Aptana_Studio_3_Workspace/delivery_health_dashboard_03/config/initializers/mailer_config.rb:16:in `<top (required)>': uninitialized constant DevelopmentMailInterceptor (NameError)
How do i fix it?
The code I am using is shown below:
config/initializer/mailer_config.rb
ActionMailer::Base.default_charset = "utf-8"
ActionMailer::Base.default_content_type = "text/html"
ActionMailer::Base.raise_delivery_errors = true
ActionMailer::Base.perform_deliveries = true
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.smtp_settings = {
:enable_starttls_auto => true,
:address => "secure.emailsrvr.com",
:port => '25',
:domain => "domain",
:user_name => "user_name",
:password => "password",
:authentication => :plain
}
ActionMailer::Base.register_interceptor(DevelopmentMailInterceptor) if Rails.env.development?
lib/development_mail_interceptor.rb
class DevelopmentMailInterceptor
def self.delivering_email(message)
message.to = "email"
end
end
Thanks in advance.

require 'development_mail_interceptor' #add this line
ActionMailer::Base.register_interceptor(DevelopmentMailInterceptor) if Rails.env.development?

I found it easier to install the mailcatcher gem. Then in development.rb:
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => "`localhost`",
:port => 1025
}
Then just run "mailcatcher" and hit http://localhost:1080/ in a browser. It runs in the background, but can be quit directly from the browser. Gives you text+html views, source, and analysis with fractal, if you swing that way. Super-clean.

Related

How to avoid execution expired errors while using 'mail' gem

I'm working on a webscraper that will send out a weekly CSV with new content with Ruby. For the mailing component I decided to use the Mail gem. After a great deal of tinkering I got it to send a few test emails. However, I frequently get this error:
...smtp.rb:541:in `initialize': execution expired (Net::OpenTimeout)...
I have a reasonable internet connection and haven't been able to detect any sort of pattern with the error. Here is my code for the mailer:
require 'mail'
def sendEmail(newEventCount, newEventArray)
if newEventArray.to_a.empty? == true
emailBodyText = "No new events were added this week."
else
newEventString = "The new events are: "
newEventArray.each do |event|
newEventString = newEventString + event + "\n"
end
emailBodyText = "#{newEventCount} events were added this week. #{newEventString}"
end
options = { :address => "smtp.gmail.com",
:port => 587,
:domain => '(my public ip address according to google)',
:user_name => '(my username)',
:password => '(my password)',
:authentication => 'plain',
:enable_starttls_auto => true }
Mail.defaults do
delivery_method :smtp, options
end
mail = Mail.new do
from '(my email)'
to '(recipient email)'
subject 'Weekly Scrape Results'
body emailBodyText
add_file './events.csv'
end
mail.deliver!
end

Getting email to work on openshift ruby application

I can't seem to get email for password recovery using devise to work on my openshift app. I'm using Rails 4.0.2 and Ruby 1.9.3. I've tried the following in production.rb :
config.action_mailer.default_url_options = { :host => 'mydomain.com' }
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:port => 25,
:address => 'smtp.mailgun.org',
:user_name => 'postmaster#domain.com',
:password => '[password]',
:domain => 'mydomain.com.mailgun.domain',
:authentication => :plain,
}
I've also done settings required for google use and both worked fine in development on local. Also tried ports 465, 587, as described here. Sending mail unfortunately still isn't performed on production. The app just throws an error with nothing in the logs.

message not delivered to email -ruby on rails-

i tried to send message to mail by using ruby on rails but it not delivered
Although 0 error in the code
mail(:to => user.email, :subject => "Welcome to My site")
con
ActionMailer::Base.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => "gmail.com",
:user_name => "username",
:password => "password",
:authentication => "plain",
:enable_starttls_auto => true
}
RegistedMailer.sendingmail(#profolio).deliver
note
the application buit on localhost
What is the setting for delivery method ? It should be:
config.action_mailer.delivery_method = :smtp
In development mode, Rails does not send out the email. Make sure you set this, per the Rails Guides Action Mailer Configuration for GMail page:
config.action_mailer.delivery_method = :smtp
If that's already set, can you post your log?
You may also want to investigate something like MailCatcher, which makes it easier to test emails in development mode.

Send emails with Padrino in Heroku

I'm trying to send emails via sendmail in Padrino. I did the configuration specified here (Configuration and Quick Usage)
But I always get the following error in the server log (on Heroku or localhost):
app[web.1]: sh: Illegal option -
app[web.1]: Errno::EPIPE - Broken pipe:
I installed the mail gem and I'm using Padrino 0.10.7
I'm using this, to send the email:
post :create do
email(:from => "tony#reyes.com", :to => "john#smith.com", :subject => "Welcome!", :body=>"Body")
end
That's practically all I have...
You should be using one of the parter addons for sending mail with Heroku.
A good option is Sendgrid
heroku addons:add sendgrid:starter --app=your_app_name
Then in your Padrino app in app.rb inside your App class:
set :delivery_method, :smtp => {
:address => "smtp.sendgrid.net",
:port => 587,
:domain => 'heroku.com',
:user_name => ENV['SENDGRID_USERNAME'],
:password => ENV['SENDGRID_PASSWORD'],
:authentication => :plain,
:enable_starttls_auto => true
}
You could substitute these for settings for another external SMTP server, or look at Mandrill for transactional emails.
I suspect the Errno::EPIPE error you were seeing was that it could not connect to a valid SMTP server, so your controller code should be fine as it is.
Pat is right, you don't need an add-on, just configure your app.rb like stef suggests and you're good to go. So, for example, we use gmail and our config looks something like this:
set :delivery_method, :smtp => {
:address => "smtp.domain.com",
:port => 587,
:domain => 'rails.domain.com',
:user_name => "rails#domain.com",
:password => "super-secret",
:authentication => "plain",
:enable_starttls_auto => true,
:openssl_verify_mode => OpenSSL::SSL::VERIFY_NONE
}

ActionMailer 3 without Rails

I'm writing a small Ruby program that will pull records from a database and send an HTML email daily. I'm attempting to use ActionMailer 3.0.3 for this, but I'm running in to issues. All the searching I've done so far on using ActionMailer outside of Rails applies to versions prior to version 3. Could someone point me in the right direction of where to find resources on how to do this? Here's where I am so far on my mailer file:
# lib/bug_mailer.rb
require 'action_mailer'
ActionMailer::Base.delivery_method = :file
class BugMailer < ActionMailer::Base
def daily_email
mail(
:to => "example#mail.com",
:from => "example#mail.com",
:subject => "testing mail"
)
end
end
BugMailer.daily_email.deliver
I'm definitely stuck on where to put my views. Every attempt I've made to tell ActionMailer where my templates are has failed.
I guess I should also ask if there's a different way to go about accomplishing this program. Basically, I'm doing everything from scratch at this point. Obviously what makes Rails awesome is it's convention, so is trying to use parts of Rails on their own a waste of time? Is there a way to get the Rails-like environment without creating a full-blown Rails app?
After some serious debugging, I found how to configure it.
file mailer.rb
require 'action_mailer'
ActionMailer::Base.raise_delivery_errors = true
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => "domain.com.ar",
:authentication => :plain,
:user_name => "test#domain.com.ar",
:password => "passw0rd",
:enable_starttls_auto => true
}
ActionMailer::Base.view_paths= File.dirname(__FILE__)
class Mailer < ActionMailer::Base
def daily_email
#var = "var"
mail( :to => "myemail#gmail.com",
:from => "test#domain.com.ar",
:subject => "testing mail") do |format|
format.text
format.html
end
end
end
email = Mailer.daily_email
puts email
email.deliver
file mailer/daily_email.html.erb
<p>this is an html email</p>
<p> and this is a variable <%= #var %> </p>
file mailer/daily_email.text.erb
this is a text email
and this is a variable <%= #var %>
Nice question! It helped me to understand a bit more how Rails 3 works :)
It took me a while to get this to work in (non-)Rails 4. I suspect it's just because I have ':require => false' all over my Gemfile, but I needed to add the following to make it work:
require 'action_view/record_identifier'
require 'action_view/helpers'
require 'action_mailer'
Without the above code, I kept getting a NoMethodError with undefined method 'assign_controller'.
After that, I configured ActionMailer as follows:
ActionMailer::Base.smtp_settings = {
address: 'localhost', port: '25', authentication: :plain
}
ActionMailer::Base.default from: 'noreply#example.com'
ActionMailer::Base.raise_delivery_errors = true
ActionMailer::Base.logger = Logger.new(STDOUT)
ActionMailer::Base.logger.level = Logger::DEBUG
ActionMailer::Base.view_paths = [
File.join(File.expand_path("../../", __FILE__), 'views', 'mailers')
# Note that this is an Array
]
The templates go in lib/<GEM_NAME>/views/mailers/<MAILER_CLASS_NAME>/<MAILER_ACTION_NAME>.erb (MAILER_ACTION_NAME is the public instance method of your mailer class that you call to send the email).
Lastly, don't forget to put this in your spec_helper:
ActionMailer::Base.delivery_method = :test

Resources