Sending Email with Ruby on Rails and OAuth2 (Office 365) - ruby-on-rails

Our application is built on
ruby 2.2.1p85
Rails 4.2.5
We use ActionMailer to send notification emails. My old code that rely on ActionMailer and SMTP is:
ActionMailer::Base.smtp_settings = {
:address => "smtp.office365.com",
:port => 587,
:domain => "smtp.office365.com",
:authentication => :login,
:user_name => "MY_EMAIL_ADDRESS",
:password => "MY_PASSWORD",
:enable_starttls_auto => true
}
......
def general_mail (to, subject, content, attachmts = [] )
attachmts.each do |attachment|
attachments[File.basename(attachment)] = File.read(attachment, mode: 'rb')
end
mail(to: to, subject: subject) do |format|
format.text { render :text => content }
end
end
The code now is not working because the old authentication method will be deprecated and switch to OAuth2.
Can someone help me to make the notification email code work again under Oauth2?
Thank you so much!

Related

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
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 => 'me#example.com', ######## 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: 'you#example.com', 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.

Mandrill + Rails not sending via controller but sending via console

Hi I am having a very strange problem. I setup mandrill as per the docs and I am now unable to send mail via my controller. Even devise mails do not seem to be going.... But I am able to send it via the console!
My Mail Config
config.action_mailer.default_url_options = { :host => 'smtp.mandrillapp.com' }
config.action_mailer.perform_deliveries = true
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => "smtp.mandrillapp.com",
:port => 587,
:enable_starttls_auto => true,
:user_name => ENV["MANDRILL_USERNAME"],
:password => ENV["MANDRILL_APIKEY"],
:authentication => 'plain',
:domain => 'yim.mydomain.org',
}
My Mailer
class YimMailer < ActionMailer::Base
default from: "notifier#yim.mydomain.org"
def welcome_email(user, data)
#user = user
#data = data
mail(to: #user.email, subject: 'Welcome!')
end
end
In my controller:
YimMailer.welcome_email(current_user, dummydata).deliver
When i execute the same line via the console, it delivers. What could be the issue?
I am using rails 4
I had the same issue and restarted the server (accidentally), it started working via controller too!

Rails Action Mailer not sending email

I am a complete beginner in Rails and I'm trying to send an email after someone signs up using Action Mailer.
My logs say that the email is sending, but Gmail never gets it.
config/initializers/setup_mail.rb
ActionMailer::Base.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => "asciicasts.com",
:user_name => "asciicasts",
:password => "secret",
:authentication => "plain",
:enable_starttls_auto => true
}
mailers/user_mailer.rb
class UserMailer < ActionMailer::Base
default :from => "eifion#asciicasts.com"
def registration_confirmation(user)
mail(:to => user.email, :subject => "Registered")
end
end
controllers/users_controller.rb
...
def create
#user = User.new(params[:user])
if #user.save
UserMailer.registration_confirmation(#user).deliver
sign_in #user
flash[:success] = "Welcome to the Sample App!"
redirect_to #user
else
render 'new'
end
end
...
Thanks!
Make sure you have this option set in your config/environments/development.rb :
config.action_mailer.delivery_method = :smtp
Also, in ActionMailer::Base.smtp_settings you need to specify a valid gmail account. Copy-pasting (asciicasts) is not gonna cut it here.
See this question for reference: Sending mail with Rails 3 in development environment
Instead of 'smtp' you can use 'sendmail'
ActionMailer::Base.delivery_method = :sendmail
ActionMailer::Base.sendmail_settings = { :address => "smtp.gmail.com",
:port => "587", :domain => "gmail.com", :user_name => "xxx#gmail.com",
:password => "yyy", :authentication => "plain", :enable_starttls_auto => true }
I ran into this same problem for a new mailer I had setup. I couldn't figure out for the life of me why this new mailer couldn't send emails, or even get to the method in the mailer when I stepped through it.
Solution
It ended up being that if you put the deliver_now or deliver* code within the mailer, it does not send the email.
Example Broken
def email_message()
message = mail(to: User.first, subject: 'test', body: "body text for mail")
message.deliver_now
end
Corrected
#Different class; in my case a service
def caller
message = MyMailer.email_message
message.deliver_now
end
def email_message()
mail(to: User.first, subject: 'test', body: "body text for mail")
end
This solved the problem for me, I hope it solves it for someone else.

ActionMailer doesn't use default from, or any "from" for that matter

I'm trying to send out "Welcome Emails" to my users, however, the default :from is not working. It is using the user_name I specify in the config/application.rb file.
This is the code I currently have.
config/application.rb
ActionMailer::Base.smtp_settings = {
:address => "smtp.gmail.com",
:port => "587",
:domain => "domain.com",
:user_name => "myemail#gmail.com",
:password => "password",
:authentication => "plain",
:enable_starttls_auto => true
}
user_mailer.rb
class UserMailer < ActionMailer::Base
default :from => 'email_i_want_to_send_from#gmail.com'
def welcome_email
mail(:to => "myemail#gmail.com", :subject => "welcome")
end
end
Instead of receiving an email from email_i_want_to_send_from#gmail.com, a user would receive an email from myemail#gmail.com. (Yes, I am using actual email addresses and passwords when testing).
Anyone have any ideas as to why this is happening? Thanks.
Google doesn't allow you to "spoof" a from address. I used Send Grid to send out my emails.

Why can't I set the FROM name correctly using Gmail in my Rails app?

I am trying to get the emails I send through Gmail from my Ruby on Rails app to be from Ben.
Currently when the email arrives in a (seperate, non-related) Gmail account, in the Inbox it has ben in the from section.
Here are my settings:
setup_mail.rb
# my domain is my_example.com, and the email address I am sending from is ben#my_example.com
ActionMailer::Base.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => "my_example.com",
:user_name => "ben#my_example.com",
:password => "my_password",
:authentication => "plain",
:enable_starttls_auto => true
}
if Rails.env.development?
ActionMailer::Base.default_url_options[:host] = "localhost:3000"
else
ActionMailer::Base.default_url_options[:host] = "my_example.com"
end
report_mailer.rb
def send_notification_email(notification_details)
subject = "testing_email"
mail(:to => notification_details[:email], :subject => subject, :from => "Ben")
end
And here are the email settings in Gmail:
In the :from part you can actually specify the email address this way :
:from => 'Ben <ben#my_example.com>'
The :from key there is actually the email you're sending it from. GMail does not allow this to be overriden, as it can lead to abuse.

Resources