Sasha
Sasha

Reputation: 3281

rails, action mailer not sending though logs say it is

im using devise and im trying to send a welcome email after someone signs up using actionmailer.

i overwrote the Registration controller on devise with...

  def create
    build_resource

    if resource.save
      if resource.active_for_authentication?
        set_flash_message :notice, :signed_up if is_navigational_format?


        UserMailer.welcome_email(resource).deliver


        sign_in(resource_name, resource)
        respond_with resource, :location => after_sign_up_path_for(resource)
      else
        set_flash_message :notice, :"signed_up_but_#{resource.inactive_message}" if is_navigational_format?
        expire_session_data_after_sign_in!
        respond_with resource, :location => after_inactive_sign_up_path_for(resource)
      end
    else
      clean_up_passwords resource
      respond_with resource
    end
  end

in that line

UserMailer.welcome_email(resource).deliver

i call my user_mailer.rb

  default :from => "[email protected]"

  def welcome_email(user)
    @user = user
    @url = "http://example.com/login"
    mail(:to => user.email, :submit => "Welcome YEAH!")     
  end

i have a view under app/views/user_mailer/welcome_email.text.erb

in my initializers folder i have a setup_mail.rb

ActionMailer::Base.smtp_settings = {
    :address                => "smtp.gmail.com",
    :port                   => 587,
    :domain                 => "gmail.com",
    :user_name              => "[email protected]",
    :password               => "example",
    :authentication     => "plain",
    :enable_starttls_auto   => true
}

in my development.rb i have...

config.action_mailer.delivery_method = :sendmail
config.action_mailer.perform_deliveries = true

i am super stumped why this hasn't been working. ive written an older project and i got the action mailer to work. the code right now is nearly identitcal to my old project. the exception is that im using devise now.

in my terminal window, when i run 'rails server', on that window it also says...

Sent mail to [email protected] (140ms)
Date: Thu, 12 Apr 2012 12:32:48 -0700
From: [email protected]
To: [email protected]
Message-ID: <[email protected]>
Subject: Welcome email
Mime-Version: 1.0
Content-Type: multipart/alternative;
boundary="--==_mimepart_4f872de0883d4_1805f3fdba4834cd49281a";
charset=UTF-8
Content-Transfer-Encoding: 7bit
submit: Welcome YEAH!

after a user signs up. i dont think its really sending though. my gmail never gets it and its not in my spam. could it be something with devise's mailer/views? but im explicitly using UserMailer in the controller, which is why i overwrote it

ive been stuck for days! help would be greatly appreciated. thanks a bunch

Upvotes: 1

Views: 3854

Answers (2)

Tiago
Tiago

Reputation: 1337

First of all, you must ensure that you tell devise were to look for your controller (since you've subclassed it, I assume). You can do this by editing config/routes.rb like this:

# config/routes.rb
devise_for :users, :controllers => {:registrations => "registrations"}

If you've done this, ensure that your controller is the one being used. Furthermore, regarding sendmail specifically, I haven't tried using it yet, but assuming that sendmail uses your host OS's internal mail agent, and that you are under a dynamic IP address (which ISPs give to its users upon connection), I believe that mail providers like GMail will block your message, as they do not allow dynamic IPs to serve as mail providers. They do this for security purposes mainly. Perhaps you can check this by editing config/environments/development.rb:

# config/environments/development.rb
config.action_mailer.raise_delivery_errors = true

to see if there is something going on under the hood. If that doesn't help that much (and since what you are effectively trying to do is sending email using a valid mail account - given your ActionMailer settings), you can use SMTP delivery instead of sendmail, along with the configuration you have, editing this line:

# config/environment/development.rb
config.action_mailer.delivery_method = :smtp

in your initializer which will, in fact, send the email through the account specified to the designated recipients.

As a final (off topic) suggestion I suggest you to use Rails Observers, in which case you don't need to overwrite Devise's registrations controller. This would make your life much simpler, DRY and follow convention over configuration's philosophy. You could then just create an Observer like this:

# app/observers/user_observer.rb
class UserObserver < ActiveRecord::Observer
  def after_create(user)
    UserMailer.welcome_email(user).deliver
  end
end

And edit config/application.rb:

config.active_record.observers = :user_observer

Upvotes: 3

John Plummer
John Plummer

Reputation: 1044

Have you tried

config.action_mailer.delivery_method = :smtp

And have a look at mailcatcher for testing email in development, I find it much easier than actually sending the mail.

Upvotes: 3

Related Questions