Sending email from Ruby via Gmail - ruby-on-rails

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"

Related

actionmailer not sending emails even after setting all the possible configurations

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.

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!

Action Mailer not sending mails although the logs say it is

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 ?

Rails Mailboxer - unable to find the recipient domain on sending email via smtp

I am using mailboxer gem in my Rails application.
Following are the 2 methods in my User model, which is messageable -
def name
return :email
end
def mailboxer_email(object)
#Check if an email should be sent for that object
#if true
# mail(:to => :email, :subject => "Your Login credential")
return :email
#if false
#return nil
end
And following are my smtp settings in my development.rb file -
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
config.action_mailer.default :charset => "utf-8"
Net::SMTP.enable_tls(OpenSSL::SSL::VERIFY_NONE)
config.action_mailer.delivery_method = :smtp
ActionMailer::Base.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:authentication => :plain,
:domain => 'gmail.com',
:user_name => 'address#gmail.com',
:password => 'mypassword',
:authentication => 'plain',
:enable_starttls_auto => true
}
But on creating a message, i am getting the following error -
Completed 500 Internal Server Error in 22136ms
Net::SMTPFatalError (553-5.1.2 We weren't able to find the recipient domain. Please check for any
553-5.1.2 spelling errors, and make sure you didn't enter any spaces, periods,
553 5.1.2 or other punctuation after the recipient's email address. eg3sm14283691pac.1 - gsmtp
):
`app/controllers/conversations_controller.rb:12:in `create`'
I am sure I am sending the email to a proper email address. Also, when i put debugger in mailboxer_email method, the email I see is valid one.
Any idea where I am going wrong?
You are getting the error:- "We weren't able to find the recipient domain"
by setting up the domain => 'gmail.com'
You should specify the
:domain => 'your application name' ( :domain => 'foo.com' or 'http://localhost:3000' )
also setup
config.action_mailer.delivery_method = :smtp
you change according production, development environment.

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