Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails: change default sender in action mailer

I am sending email using action mailer in my rails app. But it allows only one default sender. This is my UserMailer class:

class UserMailer < ActionMailer::Base
 default :from => "[email protected]"
 def welcome_email(user, order)
  @user = user
  @order = order
  mail(:to => user.email, :subject => "Your Order")
 end
 def signup_email(user)
  @user = user
  mail(:to => user.email, :subject => "Thank you.")
 end
 def invite_confirm(curuser,usemail,post)
  @greeting = "Hi"
  @user = curuser
  @post = post
  mail(:to => user.email, :subject => "Hello")
 end
end

I tried this:

class UserMailer < ActionMailer::Base
 def welcome_email(user, order)
@user = user
    @order = order
    mail(:to => user.email, :subject => "Your Order", :from => "[email protected]")
 end
 def signup_email(user)
   @user = user
   mail(:to => user.email, :subject => "Thank you.", :from => "[email protected]")
 end
 def invite_confirm(curuser,usemail,post)
  @greeting = "Hi"
  @user = curuser
  @post = post
  mail(:to => user.email, :subject => "Hello", :from => "[email protected]")
 end
end

But still it is sending email from "[email protected]"

Is there any way to change sender for each method written in UserMailer class? Am i supposed to change anywhere else?

In config/environments/development.rb and config/environments/production.rb i have this:

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

I guess, i should not change anything here.

like image 995
user2206724 Avatar asked May 31 '13 14:05

user2206724


2 Answers

You can pass it as a parameter to the mail method:

def new_mail
  mail from: "[email protected]", to: "[email protected]"
end
like image 128
mihai Avatar answered Oct 12 '22 23:10

mihai


I think you want to send mail with three different emails of the for-each action. Because you use gmail, you need Sending mail from a different address.

No single vendor is optimal for all three types of email; you likely will use several vendors.

For “company email,” that is, sending individual email to customers or business associates, you’ll probably use Gmail or Google Apps for Business. For a single address, you can set up a single Gmail account to receive and send email from a different address. More likely, you’ll want several email addresses for your company mail. For that, use Google Apps for Business.

Send Email with Rails

like image 23
rails_id Avatar answered Oct 12 '22 23:10

rails_id