Rails 6 ArgumentError (SMTP-AUTH requested but missing user name) - ruby-on-rails

I built a basic rails app only for contact form using - gem 'mail_form'. It works perfectly in the development environment. However, I deploy the app in Heroku and only got an error in the production environment.
config/environments/production.rb
config.action_mailer_default_url_options = { host: "https://myapp.herokuapp.com" }
# Rails.application.routes.default_url_options[:host] = 'https://gmail.com'
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_EMAIL'],
password: ENV['GMAIL_PASSWORD']
}
enter image description here
Any helps would be appreciated.

There is no issue in your configuration, but fact is on Heroku they have following information
Gmail have protections in place to avoid their service being used to deliver spam, however these are not published for security reasons. However we often see issues where customer applications attempt to use a Gmail account for email delivery which is subsequently blocked. So it mean there is some sort of block.
You are suggested to use email add-ons from email add-ons

Related

How to permanently validate google captcha for sending email smtp in rails

I have created Rails 6 application with ruby 2.6.5. Using SMTP for sending emails.
Added configurations in config/environment/*rb file.
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
user_name: ENV['SENDMAIL_USERNAME'],
password: ENV['SENDMAIL_PASSWORD'],
domain: ENV['MAIL_HOST'],
address: 'smtp.gmail.com',
port: '587',
authentication: :plain,
enable_starttls_auto: true
}
Getting Error: Net::SMTPAuthenticationError: 534-5.7.14 <https://accounts.google.com/ContinueSignIn?sarp=1&scc=1&plt=AKgnsbtOS
After Verifiying captcha from the below link
[https://accounts.google.com/b/1/DisplayUnlockCaptcha][1]
It work for some time than again it start throwing the same error.
I have enabled less secure apps also for my email.
It is common issue i used to get when using action mailer with google SMTP configuration. I used to fellow below few step to solve it.
Try to login to google account manually with same location where rails server is there and check if captcha is showing.
Once Mail working from rails sever never login manually to goole account again.
Try changing authentication: :plain to :login

ActionMailer authenticate with gmail to send email without using unsecure apps

Hello i am trying to send emails (through gmails gsuite with a custom domain) through a rails 5 app using action mailer but while using the configuration recommended here:
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: 'smtp.gmail.com',
port: 587,
domain: 'example.com',
user_name: '<username>',
password: '<password>',
authentication: 'plain',
enable_starttls_auto: true }
it also says to setup unsecure apps but we use two factor auth and it wont let us setup unsecure apps here is what it shows:
enter image description here
I have also gone down the path of setting up an app password and used that with the result of:
Error performing ActionMailer::Parameterized::DeliveryJob (Job ID: <job id> from Async(mailers) in 1543.41ms: Net::SMTPAuthenticationError (535-5.7.8 Username and Password not accepted. Learn more at
i have followed all the instructions in this section of the documentation with no success.
https://guides.rubyonrails.org/action_mailer_basics.html#action-mailer-configuration-for-gmail
So my question is this.
how do i configure the config.action_mailer.smtp_settings so that i am able to send emails throught my gsuite email address that uses a custom domain name

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...

Configure two gmail servers in one Rails 3.2 app?

Is it possible to configure two gmail servers in one rails app? I did a web search and didn't come up with anything. I would prefer not to get involved in using postfix instead of gmail unless that is the only way.
In my production.rb file:
config.action_mailer.smtp_settings = {
address: 'smtp.gmail.com',
port: 587,
domain: 'domain1.com',
user_name: 'serv#domain1.com',
password: '************',
authentication: 'plain',
enable_starttls_auto: true }
I'd like to configure a second server for my gmail server at serv#domain2.com. Is it possible? If yes, then how?

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.

Resources