Action Mailer not sending mails although the logs say it is - ruby-on-rails

I have the following code in my registration controller
def new
Rails.logger.debug "#{params[:email]}"
if !params[:email].blank? && !params[:invoke].blank? && params[:invoke].casecmp('Send') == 0
MyMailer.send_email(params[:email]).deliver
end
end
My params[:email] gets populated correctly.
in my_mailer.rb i have the following code
class MyMailer < ActionMailer::Base
default from: 'someaddress#yahoo.com'
def send_email(emailaddress)
#emailaddress=emailaddress
Rails.logger.debug "{#{emailaddress}}"
mail(to: #emailaddress ,subject: 'asdasd')
end
end
I have set the following properties in development.rb
config.action_mailer.raise_delivery_errors = true
config.action_mailer.delivery_method = :smtp
config.action_mailer.perform_deliveries = true
I have also done the smtp configurations in setup_mail.rb under the initializers
ActionMailer::Base.smtp_settings = {
:address => "smtp.yahoo.com",
:port => 587,
:domain => "yahoo.com",
:user_name => "someaddress#yahoo.com",
:password => "somepass",
:authentication => "plain",
:enable_starttls_auto => true
}
My logs say that the mail has been sent to the respective person but my gmail shows no mail has arrived . Not even in the spams. Is there any configuration settings i am missing out on ?

Related

Sending email from Ruby via Gmail

This code fails to send email (nothing happens at all - no error either). What am I doing wrong?
/app/mailers/user_mailer.rb
class UserMailer < ActionMailer::Base
default from: "sample#gmail.com"
def email_test()
#user = 'recipient#gmail.com'
mail(to: #user, subject: 'Welcome to My Awesome Site')
end
end
/config/development.rb:
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
config.action_mailer.default_options = {from: 'no-reply#example.com'}
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:enable_starttls_auto => true,
:address => "smtp.gmail.com",
:port => 587,
:domain => "gmail.com",
:authentication => :login,
:user_name => "sample#gmail.com",
:password => "password123",
}
I invoke the code in folder /app/mailers/. Here I run:
rails runner UserMailer.email_test
Is it because I am putting the gmail smtp settings in development.rb, but am running this code in command line?
I've already turned on 'access to less secure apps' in my gmail account. So confused, please advise thanks!
For Rails 3:
rails runner "UserMailer.email_test.deliver"
For Rails 4:
bin/rails runner "UserMailer.email_test.deliver_now"

ActionMailer not delivering mail when SMTP settings configured dynamically

I'm trying to set up ActionMailer's SMTP settings to be able to be configured at run time, but when that happens it doesn't seem to connect to the 3rd party service to deliver the mail. Below are 2 scenarios, the first of which the mail will send and deliver, the second of which nothing happens. I'm using the development environment for testing
This configuration is common to both scenarios
# config/environments/development.rb
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = false
config.action_mailer.default :charset => "utf-8"
config.action_mailer.delivery_method = :smtp
This works:
# config/environments/development.rb
config.action_mailer.default_url_options = { host: 'www.mysite.com' }
config.action_mailer.smtp_settings = {
:address => "smtp.thirdpartyservice.com",
:port => 587,
:domain => 'mysite.com',
:user_name => "me#mysite.com",
:password => "my-password",
:authentication => "plain",
:enable_starttls_auto => true
}
This delivers the mail, but also when tailing the logs, there is a slight delay in the request so I know the request is being made to the third party service.
This doesn't work:
This is the setup I want, using a custom mailer that I'm using.
class MyCustomMailer < Devise::Mailer
before_filter :use_smtp_settings
def example_mailer(record)
Rails.logger.warn self.smtp_settings
#resource = record
mail(to: record.email,
from: AppSettings.first.mailer_sender,
subject: "Example")
end
private
def use_smtp_settings
self.default_url_options[:host] = AppSettings.first.domain_address
self.smtp_settings = {
:address => AppSettings.first.smtp_address,
:port => AppSettings.first.smtp_port,
:domain => AppSettings.first.smtp_domain,
:user_name => AppSettings.first.smtp_username,
:password => AppSettings.first.smtp_password,
:authentication => "plain",
:enable_starttls_auto => true
}
end
end
The rails logger in the #example_mailer() method shows the same attributes that are used in the first example, albeit loaded from the app_settings table. However when tailing the logs this time, there is no delay so ActionMailer doesn't seem to even try making a request to the third party service.
This won't work because you're modifying the smtp settings on an instance of your mailer but the underlying mail gem reads it from the mailer class level attribute. The supported way to do this in Rails 4.0 and above is to pass a custom header called :delivery_method_options to the mail call, e.g:
class MyCustomMailer < Devise::Mailer
before_filter set_default_host
def example_mailer(record)
mail to: record.email,
from: app_mailer_sender,
subject: "Example",
delivery_method_options: app_smtp_settings
end
private
def app_settings
#app_settings || AppSettings.first
end
def app_domain_address
app_settings.domain_address
end
def app_mailer_sender
app_settings.mailer_sender
end
def app_smtp_settings
self.smtp_settings = {
address: app_settings.smtp_address,
port: app_settings.smtp_port,
domain: app_settings.smtp_domain,
user_name: app_settings.smtp_username,
password: app_settings.smtp_password,
authentication: "plain",
enable_starttls_auto: true
}
end
def set_default_host
default_url_options[:host] = app_settings.domain_address
end
end
end
One little tip - don't repeatedly call AppSettings.first since that re-queries the database (actually it'll be caught by the AR query cache but a new instance will be created every time). But you knew that right ;-)

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!

Getting Devise 1.3.4 to send emails with Gmail in development

I'm trying to setup devise 1.3.4 to send emails via gmail while in development mode. I should mention that I'm using Rails 3.0.4 and Ruby 1.9.2p136.
I've tried the following in config/environments/development.rb:
config.action_mailer.raise_delivery_errors = true
config.action_mailer.perform_deliveries = true
config.action_mailer.default_url_options = { :host => 'mydomain.com' }
ActionMailer::Base.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => "mydomain.com",
:user_name => "info",
:password => "secret",
:authentication => "plain",
:enable_starttls_auto => true
}
And in config/initializers/devise.rb I changed
config.mailer_sender = "please-change-me-at-config-initializers-devise#example.com"
To
config.mailer_sender = "info#mydomain.com"
Then I tried
http://yekmer.posterous.com/devise-gmail-smtp-configuration
It's still not working.
Is there a wiki page on how to get the mailer working? I see the email in my log and it looks great! The links work, etc ... I just want to see them in my email account.
Edit
I found the answer -
I used http://yekmer.posterous.com/devise-gmail-smtp-configuration - I had been putting that code in config/intializers/devise.rb when I should have been putting it in config/environments/development.rb.
Have you tried this?
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
ActionMailer::Base.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => "gmail.com",
:user_name => "myinfo#gmail.com",
:password => "secret",
:authentication => "plain"
# :enable_starttls_auto => true # I don't have this, but it should work anyway
}
--------- EDIT
it it's sent maybe you don't receive it because of the spam filter, first thing to check:
class UserMailer < ActionMailer::Base
default :from => "myinfo#gmail.com"
# ...
end
you should put that in devise initializer :
# Configure the class responsible to send e-mails.
config.mailer = "YourAppDeviseMailer"
Then create a class that extends Devise::Mailer :
class YourAppDeviseMailer < Devise::Mailer
default :from => 'your_email'
def self.mailer_name
"devise/mailer"
end
end
I think you can change it inside config/initializers/devise.rb. No need for a new class I think?
#config/initializers/devise.rb
config.mailer_sender = 'youremail#gmail.com'
Check if the value of ActionMailer::Base.delivery_method is :smtp

rails 3 email sending problem

I am using Rails 3 and implementing the email sending feature. I am not sure if my configuration is correct, but here are my codes:
mailers/user_mailer.rb
class UserMailer < ActionMailer::Base
default :from => "user#gmail.com"
def send_to(user)
#user = user
subject='welcome !'
mail(:to=>'y.lan#gmail.com', :subject=>subject, :content_type => "text/html")
mail.deliver
end
end
controller:
def CarsController < BaseController
...
def register_finish
UserMailer.send_to(user)
end
end
config/enviroment.rb
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => "smtp.googlemail.com",
:port => 532,
:arguments => '-i'
:enable_starttls_auto => true
}
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
When my controller invoke 'register_finish' function and try to send email to a user, I always get Timeout::Error (execution expired) error message, what could be the reason??
I saw some people define the configuration in config/initializers/setup_email.rb and use
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.smtp_settings = { ...}
while I configure it in config/enviroment.rb and use:
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {...}
I also saw some people invoke "deliver" method inside controller while I invoke it inside 'UserMailer'.
My questions:
What's the difference between my implementation and the above different way of implementations I found from internet.
Why I got timeout errors?
I'm also using gmail as my smtp server and I've adder setup_email.rb to initiliazers containing this code
ActionMailer::Base.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => "domain.pl",
:user_name => "username",
:password => "password",
:authentication => "plain",
:enable_starttls_auto => true
}
and it works for me :)
EDIT
I've just notice we are using different servers, maybe try with my config?
Timeout errors mean that there is some authentication errors.
This line is no longer needed:
ActionMailer::Base.delivery_method = :smtp
While it is adviceable to set the smtp_settings in an initializer.
If you are using it on a development machine this configuration should work with gmail:
ActionMailer::Base.smtp_settings = {
:enable_starttls_auto => true,
:address => 'smtp.gmail.com',
:port => 587,
:domain => 'your_domain',
:authentication => :plain,
:user_name => 'your_gmail_username',
:password => 'your_gmail_password'
}
EDIT
you can add for a development machine:
ActionMailer::Base.default_url_options[:host] = "localhost:3000"
Very good railscast on subject
Have a look at this:
http://lindsaar.net/2010/3/15/how_to_use_mail_and_actionmailer_3_with_gmail_smtp
Works nicely for me

Resources