2012-05-02 78 views
4

我有困難就註冊設立的ActionMailer:的Rails 3.2.1的ActionMailer設置

config/initializers/setup_mail.rb

ActionMailer::Base.smtp_settings = { 
:address    => "smtp.gmail.com", 
:port     => "587", 
:domain    => "mail.gmail.com", 
:user_name   => "my_gmail_user_name", 
:password    => "my_gmail_pw", 
:authentication  => "plain", 
:enable_starttls_auto => true 
} 

ActionMailer::Base.default_url_options[:host] = "localhost:3000" 
Mail.register_interceptor(DevelopmentMailInterceptor) if Rails.env.development? 

users_controller.rb

def create 
    @user = User.new(params[:user]) 

    respond_to do |format| 
    if @user.save 
     UserMailer.welcome_email(@user).deliver 

     format.html{ redirect_to(@user, :notice => 'Account successfully created.') } 
     format.json{ render :xml => @user, :status => :created, :location => @user } 
    else 
     format.html { render :action => "new" } 
     format.xml { render :xml => @user.errors, :status => :unprocessable_entity } 
    end 
end 

app/mailers/user_mailer.rb

class UserMailer < ActionMailer::Base 
    default :from "[email protected]" 

    def welcome_email(user) 
    @user = user 
    @url = "http://localhost:3000/login" 
    mail(:to => user.email, :subject => 'Welcome to app') 
    end 
end 

lib/development_mail_interceptor.rb

class DevelopmentMailInterceptor 
    def self.delivering_email(message) 
    message.subject = "#{message.to} #{message.subject}" 
    message.to = "[email protected]" 
    end 
end 

我沒有收到在DevelopmentMailInterceptor發送到我的電子郵件轉儲任何電子郵件。

回答

1

gmail是肛門!無論如何,你可以使用不同的smtp主機?我認爲,由於過於積極的垃圾郵件檢測,他們會默默拒絕他們認爲是「潛在垃圾郵件」的smpt請求。您的案例中的潛在垃圾郵件意味着電子郵件被定向爲來自「mail.gmail.com」以外的地址,您的情況是「email.com」。

我認爲使用Gmail作爲Rails應用程序的SMTP主機是一個鬆動的命題,我使用godaddy.com(smtpout.secureserver.net)爲我的rails應用程序,並沒有問題。