Getting Devise 1.3.4 to send emails with Gmail in development - ruby-on-rails

I'm trying to setup devise 1.3.4 to send emails via gmail while in development mode. I should mention that I'm using Rails 3.0.4 and Ruby 1.9.2p136.
I've tried the following in config/environments/development.rb:
config.action_mailer.raise_delivery_errors = true
config.action_mailer.perform_deliveries = true
config.action_mailer.default_url_options = { :host => 'mydomain.com' }
ActionMailer::Base.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => "mydomain.com",
:user_name => "info",
:password => "secret",
:authentication => "plain",
:enable_starttls_auto => true
}
And in config/initializers/devise.rb I changed
config.mailer_sender = "please-change-me-at-config-initializers-devise#example.com"
To
config.mailer_sender = "info#mydomain.com"
Then I tried
http://yekmer.posterous.com/devise-gmail-smtp-configuration
It's still not working.
Is there a wiki page on how to get the mailer working? I see the email in my log and it looks great! The links work, etc ... I just want to see them in my email account.
Edit
I found the answer -
I used http://yekmer.posterous.com/devise-gmail-smtp-configuration - I had been putting that code in config/intializers/devise.rb when I should have been putting it in config/environments/development.rb.

Have you tried this?
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
ActionMailer::Base.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => "gmail.com",
:user_name => "myinfo#gmail.com",
:password => "secret",
:authentication => "plain"
# :enable_starttls_auto => true # I don't have this, but it should work anyway
}
--------- EDIT
it it's sent maybe you don't receive it because of the spam filter, first thing to check:
class UserMailer < ActionMailer::Base
default :from => "myinfo#gmail.com"
# ...
end

you should put that in devise initializer :
# Configure the class responsible to send e-mails.
config.mailer = "YourAppDeviseMailer"
Then create a class that extends Devise::Mailer :
class YourAppDeviseMailer < Devise::Mailer
default :from => 'your_email'
def self.mailer_name
"devise/mailer"
end
end

I think you can change it inside config/initializers/devise.rb. No need for a new class I think?
#config/initializers/devise.rb
config.mailer_sender = 'youremail#gmail.com'

Check if the value of ActionMailer::Base.delivery_method is :smtp

Related

Override ActionMailer::Base.smtp_settings on runtime, only for the current session?

In certain situations I'm overriding the ActionMailer::Base.smtp_settings on runtime:
class RegistrationsController < Devise::RegistrationsController
def create
ActionMailer::Base.smtp_settings = {
:address => "address.example.com",
:port => "123",
:domain => "example.com",
:authentication => "login",
:user_name => "foo",
:password => "bar"
}
super
end
end
It seems that I'm not just setting it for that particular session, but also for sessions to come. How can I avoid that? How can I assure that I'm only changing settings for this session?
I don't know if you can change ActionMailer settings for particular session as a whole, but you can change SMTP settings for one email delivery.
In your mailer you can do something like this:
class UserMailer < ApplicationMailer
def registration(params)
smtp_settings = {
:address => "address.example.com",
:port => "123",
:domain => "example.com",
:authentication => "login",
:user_name => "foo",
:password => "bar"
}
mail(
to: params[:to],
subject: "Welcome",
delivery_method: :smtp,
delivery_method_options: smtp_settings
)
end
end
delivery_method: :smtp param doesn't have to be included if your default email delivery method is SMTP.
This should work as expected. It will override smtp settings for just one mailer in this case. You can send the smtp settings through the params if you want to change it only for one particular email.

Mandrill + Rails not sending via controller but sending via console

Hi I am having a very strange problem. I setup mandrill as per the docs and I am now unable to send mail via my controller. Even devise mails do not seem to be going.... But I am able to send it via the console!
My Mail Config
config.action_mailer.default_url_options = { :host => 'smtp.mandrillapp.com' }
config.action_mailer.perform_deliveries = true
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => "smtp.mandrillapp.com",
:port => 587,
:enable_starttls_auto => true,
:user_name => ENV["MANDRILL_USERNAME"],
:password => ENV["MANDRILL_APIKEY"],
:authentication => 'plain',
:domain => 'yim.mydomain.org',
}
My Mailer
class YimMailer < ActionMailer::Base
default from: "notifier#yim.mydomain.org"
def welcome_email(user, data)
#user = user
#data = data
mail(to: #user.email, subject: 'Welcome!')
end
end
In my controller:
YimMailer.welcome_email(current_user, dummydata).deliver
When i execute the same line via the console, it delivers. What could be the issue?
I am using rails 4
I had the same issue and restarted the server (accidentally), it started working via controller too!

sendgrid addon on heroku and devise

Please help! I can't seem the get the sendgrid addon to work, and I have devise. Is there a way just to use a devise as a sender of e-mails, if so, then how? Or what am I doing wrong:
Here is my significant config/initializers/devise.rb code:
Devise.setup do |config|
# ==> Mailer Configuration
#config.mailer_sender = "myapp.herokuapp.com"
# Configure the class responsible to send e-mails.
#config.mailer = "Devise::Mailer"
Do I have to uncomment this if i want to use sendgrid?
In enviroment/production.rb I have:
config.action_mailer.default_url_options = { :host => 'myapp.herokuapp.com' }
Have you done the basic Sendgrid configuration?
https://devcenter.heroku.com/articles/sendgrid
ActionMailer::Base.smtp_settings = {
:address => 'smtp.sendgrid.net',
:port => '587',
:authentication => :plain,
:user_name => ENV['SENDGRID_USERNAME'],
:password => ENV['SENDGRID_PASSWORD'],
:domain => 'heroku.com',
:enable_starttls_auto => true
}

Action Mailer not sending mails although the logs say it is

I have the following code in my registration controller
def new
Rails.logger.debug "#{params[:email]}"
if !params[:email].blank? && !params[:invoke].blank? && params[:invoke].casecmp('Send') == 0
MyMailer.send_email(params[:email]).deliver
end
end
My params[:email] gets populated correctly.
in my_mailer.rb i have the following code
class MyMailer < ActionMailer::Base
default from: 'someaddress#yahoo.com'
def send_email(emailaddress)
#emailaddress=emailaddress
Rails.logger.debug "{#{emailaddress}}"
mail(to: #emailaddress ,subject: 'asdasd')
end
end
I have set the following properties in development.rb
config.action_mailer.raise_delivery_errors = true
config.action_mailer.delivery_method = :smtp
config.action_mailer.perform_deliveries = true
I have also done the smtp configurations in setup_mail.rb under the initializers
ActionMailer::Base.smtp_settings = {
:address => "smtp.yahoo.com",
:port => 587,
:domain => "yahoo.com",
:user_name => "someaddress#yahoo.com",
:password => "somepass",
:authentication => "plain",
:enable_starttls_auto => true
}
My logs say that the mail has been sent to the respective person but my gmail shows no mail has arrived . Not even in the spams. Is there any configuration settings i am missing out on ?

rails 3 email sending problem

I am using Rails 3 and implementing the email sending feature. I am not sure if my configuration is correct, but here are my codes:
mailers/user_mailer.rb
class UserMailer < ActionMailer::Base
default :from => "user#gmail.com"
def send_to(user)
#user = user
subject='welcome !'
mail(:to=>'y.lan#gmail.com', :subject=>subject, :content_type => "text/html")
mail.deliver
end
end
controller:
def CarsController < BaseController
...
def register_finish
UserMailer.send_to(user)
end
end
config/enviroment.rb
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => "smtp.googlemail.com",
:port => 532,
:arguments => '-i'
:enable_starttls_auto => true
}
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
When my controller invoke 'register_finish' function and try to send email to a user, I always get Timeout::Error (execution expired) error message, what could be the reason??
I saw some people define the configuration in config/initializers/setup_email.rb and use
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.smtp_settings = { ...}
while I configure it in config/enviroment.rb and use:
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {...}
I also saw some people invoke "deliver" method inside controller while I invoke it inside 'UserMailer'.
My questions:
What's the difference between my implementation and the above different way of implementations I found from internet.
Why I got timeout errors?
I'm also using gmail as my smtp server and I've adder setup_email.rb to initiliazers containing this code
ActionMailer::Base.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => "domain.pl",
:user_name => "username",
:password => "password",
:authentication => "plain",
:enable_starttls_auto => true
}
and it works for me :)
EDIT
I've just notice we are using different servers, maybe try with my config?
Timeout errors mean that there is some authentication errors.
This line is no longer needed:
ActionMailer::Base.delivery_method = :smtp
While it is adviceable to set the smtp_settings in an initializer.
If you are using it on a development machine this configuration should work with gmail:
ActionMailer::Base.smtp_settings = {
:enable_starttls_auto => true,
:address => 'smtp.gmail.com',
:port => 587,
:domain => 'your_domain',
:authentication => :plain,
:user_name => 'your_gmail_username',
:password => 'your_gmail_password'
}
EDIT
you can add for a development machine:
ActionMailer::Base.default_url_options[:host] = "localhost:3000"
Very good railscast on subject
Have a look at this:
http://lindsaar.net/2010/3/15/how_to_use_mail_and_actionmailer_3_with_gmail_smtp
Works nicely for me

Resources