This is the method inside ApplicationMailer
class CancelTrip < ApplicationMailer
default from: 'xyz#gmail.com'
def cancel_trip
#recvr= "ssdd#gmail.com"
mail(to: #recvr, subject: 'Your trip has been cancelled as per your request' )
end
end
And the environmental variables as follows:
SMTP_ADDRESS: 'smtp.gmail.com'
SMTP_PORT: 587
SMTP_DOMAIN: 'localhost:3000'
SMTP_USERNAME: 'xyz#gmail.com'
SMTP_PASSWORD: 'gggh2354'
And I am call the mailer method in my controller as follows:
def cancel
xxxxx
CancelTrip.cancel_trip.deliver_now
end
developement.rb has following
config.action_mailer.raise_delivery_errors = false
config.action_mailer.default_url_options = {host: 'localhost', port:3000}
config.action_mailer.perform_deliveries = true
Log shows the email being sent. But I dont see any email in inbox.
My rails version is 4.2.6.
Add following smtp setting on config/application.rb file:
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
config.action_mailer.delivery_method = :smtp
config.action_mailer.raise_delivery_errors = true
config.action_mailer.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => "gmail.com",
:user_name => "xxxxxxxxx#gmail.com",
:password => "xxxxxxxx",
:authentication => "plain",
:enable_starttls_auto => true
}
Check in junk mail... I had this problem a little while back
use letter opener in development for be sure your actionmailer delivered your mail. after that you can finding deliver error to gmail or other.
Make sure that your ApplicationMailer is inheriting from ActionMailer::Base.
class ApplicationMailer < ActionMailer::Base
default from: 'from#exmaple.com'
layout 'mailer'
end
One possibility may be your firewall not allows to send email. Try connect different network.
Related
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"
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 ;-)
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!
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 ?
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