Rails ActionMailer problems on Mac - ruby-on-rails

I've been working on learning to use Rails the last couple days and I've run into something that I haven't been able to solve with Google.
So I'm just creating a basic contact form that sends an email. Everything seems to be working ok in testing, which tells me that the form is working, and ActionMailer was implemented correctly, however, I'm having trouble configuring ActionMailer. I'm running OSX 10.6.2. I have postfix running and have verified that it's running using telnet localhost 25. When I try to use the form I get a "Connection refused" error.
This is my current configuration:
config.action_mailer.smtp_settings = {
:address => 'localhost',
:port => 25
}
I thought I might need to set :domain but I'm kind of confused on what that should be set to in this situation.

My life has been roughly 100x easier running :sendmail on my mac as a delivery method on my mac rather than :smtp, you might want to try giving that a shot. In this answer, I am assuming that you just want mail delivery on your Mac and don't actually care how it works.
The other thing I do, if I'm going to be connected to the net all the time on a project, is to configure my outgoing ActionMailer-originated mail to go through gmail rather than my local mac.
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.smtp_settings = {
:enable_starttls_auto => true,
:address => "smtp.gmail.com",
:port => 587,
:domain => "example.com",
:authentication => :plain,
:user_name => "address#example.com",
:password => "secret"
}
This is for a custom domain called example.com that is set up on google apps for domains. You can also just create a gmail account and send things through there. (By changing all instances of example.com to gmail.com)

You need to start the postfix daemon. you can tell if your port is open by typing in a terminal
telnet localhost 25
which will try to connect to the 25 port. It wont connect if postfix isnt running, if it does, hit ctl-] to stop the connection and 'quit' to quit telnet.
If it doesn't connect, you need to start the postfix daemon:
sudo launchctl start org.postfix.master
and then try to connect with telnet. it should connect. Now you are ready to send emails from your ActionMailer class.
sudo launchctl stop org.postfix.master
stops the postfix daemon

I followed this guide and everything works correctly. Mainly:
sudo postfix start
then
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => "localhost",
:port => 25
}

Does it work if you substitute '127.0.0.1' for 'localhost' to see if this is an IPv4 vs. IPv6 thing or an issue with your resolver?

Another great way of sending and verifying mails when development is using enter link description here.
Quote from their website:
MailCatcher runs a super simple SMTP server which catches any message sent to it to display in a web interface. Run mailcatcher, set your favourite app to deliver to smtp://localhost:1025 instead of your default SMTP server, then check out http://localhost:1080 to see the mail that's arrived so far.
It intercepts mail to all recipients and hence makes it really easy to check all mail in one place.

Related

Authentication Error sending gmail message via ActionMailer

I am trying to generate a basic email using ActionMailer. I know that things are setup semi-correctly, as I received an email from Gmail saying an unsecure app tried to login to my account. I have done as others said in this post. Those steps are:
1) Enabled less-secure apps setting in Gmail settings.
2) Changed domain: to "gmail.com" from "mydomainname.com"
3) Changed authentication from "plain" to :login
4) In Gmail settings, enabled POP and IMAP.
Here is my development configuration setting for mail:
...config/environments/development.rb
config.action_mailer.raise_delivery_errors = true
#change mail delivery to smtp for Gmail addresses.
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: "smtp.gmail.com",
port: 587,
domain: "gmail.com",
authentication: "plain",
enable_starttls_auto: true,
user_name: ENV["GMAIL_USERNAME"],
password: ENV["GMAIL_PASSWORD"]
}
I've even set the environment variables like a good boy. Error persists even if I hard code the values.
And the exception I get is:
Net::SMTPAuthenticationError
534-5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbtj
Also, in development you can use local mail-catchers.
One of them is MailHog, it's similar to gmail.
Installation
brew update && brew install mailhog
development.rb
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = { :address => "localhost", :port => 1025 }
Run MailHog in terminal typing: MailHog and in a browser http://localhost:8025
Not really a solution to Gmail's configuration problem, but another option. I had the same problem and solved it. And the problem eventually appeared again. I'm not sure if I changed something or if it´s just Gmail who doesn't like less secure applications and blocks emails even if you configure everything correctly.
I read other people solved the problem (https://stackoverflow.com/a/48300220/3372172) using two steps authentication, but never tried it.
I finally changed to another ESP (as even Rails seems to recommend in their guides): http://guides.rubyonrails.org/action_mailer_basics.html#action-mailer-configuration-for-gmail
Note: As of July 15, 2014, Google increased its security measures and now blocks attempts from apps it deems less secure. You can change your gmail settings here to allow the attempts. If your Gmail account has 2-factor authentication enabled, then you will need to set an app password and use that instead of your regular password. Alternatively, you can use another ESP to send email by replacing 'smtp.gmail.com' above with the address of your provider.
I am using Sengrid because their free plan (100 daily emails) is enough for me right now and I get many interesting analytics. But if you need more volume, I don't think they are cheap.
What fixed it is playing around with the settings until I finally got:
config.action_mailer.raise_delivery_errors = true
#change mail delivery to smtp for Gmail addresses.
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: "smtp.gmail.com",
port: 587,
domain: "mydomainname.com",
authentication: "login",
enable_starttls_auto: true,
user_name: ENV["GMAIL_USERNAME"],
password: ENV["GMAIL_PASSWORD"]
}
But the real kicker was to make sure and restart the server with each change to the ...config/environments/development.rb file above.
Edit: Well, now I feel like i'm going crazy, because the above settings did not work when I tried to run it again...

Rails email with gmail smtp error "Errno::ECONNREFUSED - No connection could be made because the target machine actively refused it."

When attempting to send email from within my Rails 3 app in development mode on my local machine, I received the following error:
Errno::ECONNREFUSED in RemindersController#create
No connection could be made because the target machine actively
refused it. - connect(2)
After spending several hours sifting through similar questions on SO and blog posts with none of the proposed solutions working, here is the solution I finally arrived at:
tl;dr
Change your smtp port setting to 25, instead of 587 which is in all the tutorials & docs.
Detailed answer (for humans):
Step 1. Go watch railscast #206 then come back. I'll wait.
Step 2. Get extra aggravated at how easy he makes it seem even though you're getting ridiculous errors.
Step 3. Go to app/config/environments/development.rb and change line 17 from false to true. This will show you where things are getting messed up.
# config.action_mailer.raise_delivery_errors = false
config.action_mailer.raise_delivery_errors = true
Step 4. Still in app/config/environments/development.rb add the following code before the last end statement. (You no longer need the app/config/initializers/setup_mail.rb file created in the screencast when you are in development mode and can safely delete it.)
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => "smtp.gmail.com",
:port => 25,
:user_name => "<your username>#gmail.com",
:password => "<your password>",
:authentication => "plain",
:enable_starttls_auto => true
}
Notes:
Notice we did not specify the :domain setting like in the tutorials
and it works fine.
Notice also that we used your_username #gmail.com as the
:user_name setting.
If you use 2-step authentication for your gmail account you will have
to generate a special app-specific
password. disclaimer: I did not try this.
AND THE BIG FAT ELEPHANT IN THE ROOM, is that we used :port => 25
NOT 587 despite the fact that nearly every single tutorial, SO question, and the rails guide only suggest 587. Granted, this could
possibly be because my work blocks port 587? I have no idea. But
this was my key problem no one solved for me. The way to find out if
this is your problem is to use telnet. If you are on windows you
have to first enable telnet by following these
instructions.
Then go to your command prompt and type telnet smtp.gmail.com 587
if it works you will get a response like 220 mx.google.com ESMTP
pi6sm33274849wic.3 - gsmtp. Otherwise it will tell you it could not
connect, in which case you can try it on a different port eg. telnet
smtp.gmail.com 25.
I hope this helps some lost soul.
I encountered this issue whilst reading through Agile Web Development with Rails 4 chapter 13, and it turned out that any email config stuff added to config/environment.rb was being ignored, despite the book indicating that "shared" email settings for all environments could be added there.
Moving the settings into config/environments/development.rb (and restarting the Rails server) fixed the issue, allowing emails to be sent on either port 25 or port 587 without any problems:
# development.rb
Rails.application.configure do
# ...blah blah blah...
config.action_mailer.raise_delivery_errors = true
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: 'smtp.gmail.com',
port: 587,
user_name: 'yourusername#gmail.com',
password: 'yourpassword',
authentication: 'plain',
enable_starttls_auto: true
}
end
NOTE: development.rb didn't contain any email-related settings originally, so I don't think that the values in environment.rb were being superseded - it's more like they simply had no effect. The I18n.default_locale = 'en-GB' line in environment.rb was working, though, so it's not like the file was being completely disregarded...

Errno::ECONNREFUSED: Connection refused - connect(2) for action mailer

I have been working with rails since a long. Now I am facing a small issue in the ActionMailer. I want to send an email when user gets registered to confirm his registration.
I am able to send email in the development mode but where as not in the production mode.
the exception Errno::ECONNREFUSED: Connection refused - connect(2) is coming everytime when deliver method is called.
I have written the following code.
My SMTP config looks:
config.action_mailer.default_url_options = { :host => "localhost:3000" }
config.action_mailer.delivery_method = :smtp
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
config.action_mailer.smtp_settings = {
:openssl_verify_mode => OpenSSL::SSL::VERIFY_NONE,
:ssl => true,
:enable_starttls_auto => true, #this is the important stuff!
:address => 'smtp.xxxx.xxx',
:port => xxx,
:domain => 'xxxxxx',
:authentication => :plain,
:user_name => 'xxxxxxx#xxx.xxx',
:password => 'xxxxxxxxx'
}
In the controller, I have written the following:
def confirm_registration_in_c
#user = User.find_by_email(asdf123#gmail.com)
if #user
UserMailer.confirm_registration(#user).deliver
end
end
In my user_mailer.rb :
class UserMailer < ActionMailer::Base
default from: "from#example.com"
def confirm_registration(user)
#user = user
#user_name = #user.name
email = #user.email
mail(:to => email, :subject => "Reset your password")
end
end
I am able to send email in the development mode in my local host, but I am not able to send the email in the dedicated server.
Can anybody help me please?
In my situation, I encountered similar problems when I was trying to making through a sending-email Rails app tutorial, the Heroku logs kept telling my that
......
Errno::ECONNREFUSED (Connection refused - connect(2) for "localhost" port 25):
......
After I compared my code with author's code, I got to find out that I had not setup my ActionMailer configurations in the config/environments/production.rb file.
Then I came to realized that I just had my config/environments/development.rb configured for sending-email, but I had not done it for my config/environments/production.rb.
So you may check it when your app's behavior difers between development and production.
Be sure you have configured your port correctly. I switched from gmail in development (port 587) to sending from my local server in production and was getting this error till I corrected the port to the one my server uses (port 25).
My problem is not identical to this question, but I feel many would found this thread via google.
If you use external SMTP service like sendgrid and has set up ActionMailer accordingly, yet it still gives this error:
Errno::ECONNREFUSED: Connection refused - connect(2) for "localhost" port 25
You may be passing config hash with String key, which are ignored. Keys must be symbols!
This may happen if it is de-serialized, what I did is to ensure keys are symbols:
config.action_mailer.smtp_settings = get_smtp_setting.symbolize_keys
for production you cant write
config.action_mailer.default_url_options = { :host => "localhost:3000" }
add production url for host, like,
config.action_mailer.default_url_options = { :host => "http://www.yourdomain.com" }
There is another reason for this error:
Errno::ECONNREFUSED: Connection refused - connect(2) for "localhost" port 25
It should be looked at SENDMAIL service on your server:
Is SENDMAIL installed?
Is SENDMAIL running?
I had this error due to the stopped SENDMAIL.
Good luck!
I had a similar issue with SendGrid and repeated Errno::ECONNREFUSED: Connection refused - connect(2) ) Finally I modified the way smtp settings were set by changing the definition within /config/environments/production.rb
from:
ActionMailer::Base.smtp_settings = {
to:
config.action_mailer.smtp_settings = {
this did the trick.
I just tracked down a similar problem while trying to deploy wordpress with Capistrano.
cap aborted!
Errno::ECONNREFUSED: Connection refused - connect(2) for "{my-ip-address}" port {my-ssh-port}
I would also get this similar error:
Tasks: TOP => git:create_release
(See full trace by running task with --trace)
The deploy has failed with an error: #<Errno::ECONNREFUSED: Connection refused - connect(2) for "my-ip-address" port {my-port}>
It turns out it was an issue with concurrent SSH sessions as my server runs Fail2Ban. To solve that I simply did the following:
Edit the jail that contains SSH configurations
$ sudo nano /etc/fail2ban/jail.local
look for [SSH] and set enabled = false
then find [ssh-ddos] and set enabled = false
Remember to restart Fail2Ban after your changes and open-ssh (if thats what your using)
$ sudo service fail2ban reload
$ sudo /etc/init.d/ssh reload
Its worth noting that the connection would be refused at different steps (tasks) in the deployment. For example after a reboot and a quick bundle exec cap production deploy:check everything appeared fine. Then I tried to deploy and received the same error, but during the execution of a different task. I also use UFW which I disabled and reenabled without issues. UFW was not the cause of the above problem.
I had a similar problem after I solved this. It was an issue with the current directory permissions. That's over here.
You may see a similar error message when using turbo_stream in rails 7 (or while doing the rails 7 demo).
If you do, run this and everything should work:
redis-server
More info here.

Why am I unable to send an email with pow?

I have a rails project which sends an email using an ActionMailer. This seems to work fine with 'rails server' on localhost:3000 but when I use pow, I get authentication error messages from the smtp server. I'm guessing this has something to do with environment variables. Here is the config code
config.action_mailer.smtp_settings = {
address: "smtp.gmail.com",
port: 587,
domain: "railscasts.com",
authentication: "plain",
enable_starttls_auto: true,
user_name: ENV["GMAIL_USERNAME"],
password: ENV["GMAIL_PASSWORD"]
}
I'm on Mountain Lion.
Thanks
I have a different suggestion:
In dev, just use letter_opener. It's more useful in that context than actually sending emails, anyway.
In production, use SendGrid and not GMail. SendGrid is awesome and really easy to set up.
Pow loads environment variables from checking two files in the application root
.powrc
.powenv
I created the .powrc file using the touch command, then added my environment variables
export GMAIL_USERNAME=username
export GMAIL_PASSWORD="my password"
I then restarted the worker for the app this way:
touch tmp/restart.txt
E-mails now work!
As blamattina has suggested, it may have something to do with your domain in your configuration file! According to the example given by Ruby on Rails Guides:
The correct, Gmail compatible configuration looks like this:
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => 'baci.lindsaar.net',
:user_name => '<username>',
:password => '<password>',
:authentication => 'plain',
:enable_starttls_auto => true }
Your domain is set to railscasts.com. Unless that's your website, my guess is that this domain is not correct. In fact, it is apparently optional as well.
This StackOverflow details that a plugin was once necessary, but is no longer needed if you're using Rails 3.2 or higher. A comment below the top answer in the article details that the domain is optional.
Update: Based on the error you described, it looks like you're hitting an authentication error! This may be because of your login credentials not properly registering.
This user is asking in the context of using Heroku, but the error is the same. If your app works properly with the Rails server, but not on Pow, it's a server-side setup issue. The solution involves needing to properly set your ENV (environment) variables to work with your server.

Rails ActionMailer with SendGrid

I'm using SendGrid to send emails on Heroku...
The problem so far is while it works great on Heroku, on my local host it fails.
Right now I have SendGrig install here, config/setup_mail.rb:
ActionMailer::Base.smtp_settings = {
:address => "smtp.sendgrid.net",
:port => "25",
:authentication => :plain,
:user_name => ENV['SENDGRID_USERNAME'],
:password => ENV['SENDGRID_PASSWORD'],
:domain => ENV['SENDGRID_DOMAIN']
}
What's a Heroku/SendGrid way to allow me to make sure my mailers work in DEV. Is this setup_mail.rb file a good thing? Should it be in the env file? Any other thoughts?
Thanks
Using config/environments/[development.rb | production.rb] as tfe mentioned above sounds like its the way to go. Just put the ActionMailer configuration in either of those files and change it to suit the development|production environment.
You can also find your SendGrid credentials used by Heroku by issuing the following command:
heroku config --long
These credentials are used for all SendGrid authentication (SMTP Auth, Website login to view stats, etc., API access)
-- Joe
SendGrid
Just set environment variables on your development environment for SENDGRID_USERNAME, SENDGRID_PASSWORD, and SENDGRID_DOMAIN. Then it will work.
You can get the correct values for these from your Heroku app. Open heroku console and get the values of ENV['SENDGRID_USERNAME'] and so on.
Or just use a different set of SMTP settings locally. Or use sendmail or something.

Resources