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

I am working on a basic app called PhotoApp, where I am learning how to set up SMTP with rails. I have used basic Ruby with SMTP sometime ago, and I know my email works, just the emails sent gets to spam folder.
But when I am trying to configure SMTP on Rails, I am getting ArgumentError (SMTP-AUTH requested but missing user name):, and that's the primary reason why the app, when pushed to heroku, crashed every time a user signs up.
Here's my config/environments/development.rb:
config.action_mailer.default_url_options = { host: 'localhost:8080' }
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: 'smtp.gmail.com',
port: 587,
domain: 'example.net',
authentication: 'plain',
enable_starttls_auto: true,
user_name: 'gmail_email',
password: '...'
}
config.action_mailer.raise_delivery_errors = true
config.action_mailer.perform_deliveries = true
I am running the server on localhost:8080, in development mode, so my host is set to localhost:8080.
Apart from that I am not able to get my app working with my gmail username with SMTP in rails.
Edit, this ruby code works:
#!/usr/bin/ruby -w
require 'net/smtp'
message = <<~EOF
From: mygmail#gmail.com
To: someone#protonmail.com
Subject: Hello!
Hello!
#{"HELLO ".*(50).delete_suffix(?\s)}
EOF
Net::SMTP.new('smtp.gmail.com', 587).tap(&:enable_starttls_auto).start('gmail.com', 'mygmail#gmail.com', 'mygmailpass', :plain) do |smtp|
smtp.send_message message, 'mygmail#gmail.com', 'someone#protonmail.com'
end
But as a newbie to rails, I can't get actionmailer working when the same email is used in the config file...

Related

Send email through devise is failing using Gmail account

Rails 4.2.1
Devise 3.4.1
I inherited an application, that, up until about a year ago, was sending notifications, without a problem. Then it was down for about a year. I am trying to restart it, and was testing a password reset action, but I am not receiving the emails.
ActionMailer (production environment), is set up as follows:
config.action_mailer.smtp_settings = {
address: Rails.application.secrets.email_provider_smtp_server,
port: Rails.application.secrets.email_provider_smtp_port,
domain: "gmail.com",
authentication: "plain",
enable_starttls_auto: true,
user_name: Rails.application.secrets.email_provider_username,
password: Rails.application.secrets.email_provider_password
}
config.action_mailer.default_url_options = { :host => Rails.application.secrets.domain_name }
config.action_mailer.delivery_method = :smtp
config.action_mailer.perform_deliveries = true
I tested the gmail account, and password, and it's all working fine. This is an individual freebie Gmail account, not a paid account.
I noticed that the "Less secure app access" was switched off, so I switched it off, and tried again, but I was not receiving the notification email, even though production log said it was sending email:
Sent mail to xxx#xxxxxxxx.net (539.2ms)
So, I added the following:
config.action_mailer.raise_delivery_errors = true
And when I ran it again, I saw the "Something went wrong" dialog, and now in production log, I have the following:
F, [2020-01-29T18:42:39.172134 #16986] FATAL -- :
Net::SMTPAuthenticationError (534-5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbs
):
app/models/user.rb:38:in `send_reset_password_instructions'
Any ideas?

Sending mail for devise

I am trying to send mail through sendgrid from my rails app. I have setup sendgrid previously. I was able to send and receive mail in the development and the production, through heroku. Everything worked for a day. I came back a couple of days later and nothing worked.
There are no errors. In dev it says the mail is sent but the email doesn't send and same for production.
Here is my setup:
development.rb
config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
config.action_mailer.delivery_method = :smtp
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
production.rb
config.action_mailer.default_url_options = { host: 'herokuapp.com' }
Rails.application.routes.default_url_options[:host] = 'herokuapp.com'
config.action_mailer.delivery_method = :smtp
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = false
config.action_mailer.default :charset => "utf-8"
setup_mail.rb
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.smtp_settings = {
address: 'smtp.sendgrid.net',
port: '587',
:authentication => :plain,
user_name: 'user',
password: 'pass',
domain: 'herokuapp.com',
enable_starttls_auto: true
}
I have tried resetting my pass to sendgrid and removing then re-adding it but then I run into an error saying "user banned" and I am unable to re add it.
I have also tried switching to postmark with similar results where the mail is never received but is sent.
You should check on Sendgrid if they are receiving your email requests.
From the documentation:
Search for the email in Email Activity. If we successfully delivered the message you will see a delivered event with the time of delivery. If you do not see the email come up, you can click on search options and make sure processed and deferred are checked. A processed event indicates that we actually got the request from you. Deferred events show if there are issues or delays with the receiving server accepting the message.

ArgumentError in Devise::RegistrationsController#create SMTP-AUTH requested but missing user name

I have problem with sending emails from localhost.
it was working on linux and now I'm trying to run my app on mac and it doesn't work already.
I have an error:
ArgumentError in Devise::RegistrationsController#create
SMTP-AUTH requested but missing user name
in config/environments/development.rb:
#Action Mailer config
config.action_mailer.raise_delivery_errors = true
config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
config.action_mailer.delivery_method = :smtp
# Send email in development mode.
config.action_mailer.perform_deliveries = true
config.action_mailer.smtp_settings = {
address: "smtp.gmail.com",
port: "587",
domain: "mail.google.com",
authentication: "plain",
enable_starttls_auto: true,
user_name: ENV["GMAIL_USER_ID"],
password: ENV["GMAIL_PASSWORD"]
}
My env variables are good, I tried put my credential into code as well and it still doesn't work :/
I checked similiar topics but didn't found the solution.
What can I try?
I read:
I tried put my credential into code as well and it still doesn't work :/
But have you tried replacing your var env with your gmail id/password like this ?
#Action Mailer config
**********
user_name: "my_username",
password: "my_password"
}
Just to be sure.
Ok, solved.
credentials didn't work because I havn't restarted my server (stupid laziness)
my env vars didn't work because I've puted them into ~.profile but it work in ~.bash_profile, that was helpful (I use OS X Yosemite).

Actionmailer cannot send using Gmail

With the same config/application.rb (see below), I can send with one Gmail account, but not for a second Gmail account. The second Gmail account is preferred, I verified the login credentials with a browser and have ensured it accepts "less secure apps".
config.action_mailer.delivery_method = :smtp
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
config.action_mailer.smtp_settings = {
address: 'smtp.gmail.com',
port: 587,
user_name: 'accountname',
password: 'secret',
authentication: 'plain',
enable_starttls_auto: true
Also with the second Gmail, I got NET::SMTPAUthenticationError in production, and it just does not send in development mode without complaining.
Finally, I am using Rails console, and my objective is to use a rake task to send email instead of in the web environment.

Rails action mailer didn't work on production

I am trying to send emails via action mailer. I have the same config for production and for development:
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: 'ee.ex.my_domain.net',
port: 465,
domain: 'my_domain',
user_name: 'email#my_mail.com',
password: 'my_pass',
authentication: :ntlm,
enable_starttls_auto: true
}
config.action_mailer.default_url_options = { :host => "localhost:3000" }
The only difference between development and production config is the host. For production I use an existing url, on my local machine it works ok but when I try it on the server I got this error:
Net::SMTPSyntaxError (504 5.7.4 Unrecognized authentication type)
I have had this error before on my local machine but I changed the authentication to :ntml and it works fine.
UPDATE
tried in server console (code from ntlm gem github page)
require 'ntlm/smtp'
smtp = Net::SMTP.new('smtp.example.com')
smtp.start('localhost.localdomain', 'Domain\\User', 'Password', :ntlm) do |smtp|
smtp.send_mail(mail_body, from_addr, to_addr)
end
but have the same error

Resources