Actionmailer cannot send using Gmail - ruby-on-rails

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.

Related

ArgumentError (SMTP-AUTH requested but missing user name)

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

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?

sendgrid username and password environment variables are not read when action mailer is executed

expected:
telnet smtp.sendgrid.net 587
EHLO
AUTH LOGIN
Enter username in Base64
Enter password in Base64
235 Authentication successful
Actual:
Via localhost
config/environments/development.rb
config.action_mailer.raise_delivery_errors = true
config/environment.rb
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.smtp_settings = {
address: 'smtp.sendgrid.net',
port: 587,
domain: 'example.com',
user_name: ENV["SENDGRID_USERNAME"],
password: ENV["SENDGRID_PASSWORD"],
authentication: 'plain',
enable_starttls_auto: true
}
controller
NotificationMailer.notification_email(#admin_email, #item).deliver
sendgrid.env
export SENDGRID_USERNAME='apikey'
export SENDGRID_PASSWORD='the api key that was provided during the smtp setup'
now I do
source ./sendgrid.env
I check the ENV, it's shows the username/password.
rails c
NotificationMailer.notification_email(#admin_email, #item).deliver
I see the email gets logged, but I get
Net::SMTPFatalError: 550 Unauthenticated senders not allowed
If I hardcode the env variables, the authentication does work. So I don't understand why the ENV variables are not getting into the process.
Please advise
I've used the dontenv-rails gem to solve this issue: https://github.com/bkeepers/dotenv

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.

Rails mailer Net::SMTPAuthenticationError (530-5.5.1 Authentication Required) with Gmail using ENV[USERNAME] and ENV[PASSWORD]

I'm getting an error when sending out mail from the Production environment, specifically:
Net::SMTPAuthenticationError (530-5.5.1 Authentication Required)
On my local machine and development environment I'm declaring the passwords using ENV[GMAIL_USERNAME] and ENV[GMAIL_PASSWORD] and it's working fine, where the declared ENV is stored in my .bash_profile
# Development Environment for Action Mailer config
config.action_mailer.default_url_options = { host: '0.0.0.0:3000' }
config.action_mailer.perform_deliveries = true
config.action_mailer.default charset: 'utf-8'
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: 'smtp.gmail.com',
port: 587,
domain: 'mydomain.com',
authentication: "plain",
enable_starttls_auto: true,
user_name: ENV["GMAIL_USERNAME"],
password: ENV["GMAIL_PASSWORD"]
}
# Production Environment for Action Mailer config
config.action_mailer.default_url_options = { host: 'mydomain.com' }
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
config.action_mailer.smtp_settings = {
address: "smtp.gmail.com",
port: 587,
domain: "mydomain.com",
authentication: "plain",
enable_starttls_auto: true,
user_name: ENV["GMAIL_USERNAME"],
password: ENV["GMAIL_PASSWORD"]
}
Unfortunately, in Production I get an error when sending mail. However, if I hardcode the username and password in environments/production.rb it works. The ENV["GMAIL_USERNAME"] and `ENV["GMAIL_PASSWORD"] is set in the .bash_profile:
export GMAIL_USERNAME="hello#mydomain.com"
export GMAIL_PASSWORD="mypassword"
I thought it would it was the app unable to call ENV["GMAIL_USERNAME"] and ENV["GMAIL_PASSWORD"] but when I jump into the production rails console and puts ENV["GMAIL_USERNAME"] it outputs the correct credentials.
I have restarted Apache and restarted my app multiple times but I'm puzzled what to do next.
Any help is much appreciated.
Thank you in advanced.
Whose bash profile? Your users? Apache won't read those profiles on startup as it doesn't run bash to start the server.. Your profile gets sourced when you login to the server. Which is why it works when you test.
You need to make sure that environment variables are defined in the script or the environment that apache runs under. Depending upon your server this could be in /etc/apache2/envvars or added to the startup script for apache.

Resources