views:

678

answers:

3

So I have action_mailer_optional_tls (http://svn.douglasfshearer.com/rails/plugins/action_mailer_optional_tls) and this in my enviroment.rb

ActionMailer::Base.server_settings = {
  :tls => true,
  :address => "smtp.gmail.com",
  :port => "587",
  :domain => "www.somedomain.com",
  :authentication => :plain,
  :user_name => "someusername",
  :password => "somepassword"
}

But now what If I want to send emails from different email accounts? How do I override the user_name and password fields on the fly?

What Im looking for is a solution which allows dynamic switching between accounts. Example the following scenario: 10 "Admins" can send out notices to our customers. Each has their own gmail account, when they fill out a form on the site rails connects using their account and sends the mail.

Thanks in advance!

Ali

A: 

If you want to use a different email address for the replies from the target receivers, you could specify Reply-To: [email protected] for the SMTP protocol and still using the existing account of google smtp service.

However you need to go to Gmail settings to add [email protected] to the list of "send email as", that also requires you to confirm [email protected] is yours by sending a link to that inbox.

codemeit
+1  A: 

I can't verify that this works right now, but you should try just modifying these settings on the fly. i.e. set the username / password from the users account right before sending an email. You could even setup a before filter on your controller to load that info.

before_filter :load_email_settings

def load_email_settings
  ActionMailer::Base.server_settings.merge!(:user_name => current_user.email, :password => current_user.email_password)
end

def current_user
   @current_user ||= User.find(session[:user_id])
end

Note that storing the users email password as plaintext is pretty dangerous, I don't know if there is any way to do what you want using Googles Account's third party authentication scheme but you might want to check that out.

Daniel Beardsley
A: 

As of Rails 2.3, the action_mailer_optional_tls plugin is not required. Instead, you should put the following in your environment.rb:

config.action_mailer.delivery_method = :smtp 
config.action_mailer.smtp_settings = { 
  :enable_starttls_auto => :true, 
  :address => "smtp.gmail.com", 
  :port => 587, 
  :domain => "mydomain.example.com", 
  :authentication => :plain, 
  :user_name => "[email protected]", 
  :password => "xxxxxxx", 
  :tls => :true 
} 
config.action_mailer.perform_deliveries = :true 
config.action_mailer.raise_delivery_errors = :true 
config.action_mailer.default_charset = "utf-8"
uv
The answer was copied from a comment by Ivan at http://douglasfshearer.com/blog/gmail-smtp-with-ruby-on-rails-and-actionmailer
uv
This still doesn't answer the main question. I think the smtp_settings should be changed in the actual mailer methods, but since these are not directly called I am not sure. Can someone confirm?
uv