newBike
newBike

Reputation: 14992

Failed to send mail on Rails but works on simple ruby script

The following code can send mails perfectly,

But not works on Rails.

content = "sample_content"
smtp = Net::SMTP.new("mail.#{@domain}", 25 )
smtp.enable_starttls
smtp.start( @domain, @user_name, @passwd, :login) do |smtp|
  smtp.send_message content, @sender, [@receiver]
end

Here's my settings on development.rb

config.action_mailer.smtp_settings ={
    :address        => "mail.#{ENV['domain']}",
    :domain         => ENV['domain'],
    :port           => '25',
    :authentication => :login,
    :user_name      => 'ENV["username"]',
    :password       => 'ENV["passwd"]',
    :enable_starttls_auto => true
  }

Error message on console

Net::SMTPFatalError: 550 5.7.1 Client does not have permissions to send as this sender

Upvotes: 1

Views: 726

Answers (1)

Fer
Fer

Reputation: 3347

It looks like that you are using a different account in the from in your emails than the account that you are using in your smtp_settings

To have this clearer:

Imagine that your smtp_settings are

config.action_mailer.smtp_settings ={
    :address        => "mail.#{ENV['domain']}",
    :domain         => ENV['domain'],
    :port           => '25',
    :authentication => :login,
    :user_name      => '[email protected]',  ######## this is the important part of the explanation
    :password       => 'ENV['passwd']',
    :enable_starttls_auto => true
  }

and then in your mail class:

def greet
  mail(from: '[email protected]', and: 'other params')
end

Then the mail server will answer with that.

It is possible to setup you email server so that one account can send emails as a different one.

If this is not possible for you, may be you can put all you mail config into an yml file and load it on the fly before sending the emails.

Having different mail classes subclassing ActionMailer::Base is another option too.

Upvotes: 2

Related Questions