ActionMailer failed while using SMTP in production mode - ruby-on-rails

Rails 7.0.1
I have tried and succeeded using ActionMailer in development environment, however it doesn't work in production environment. It seems that I am not able to receive the mail that I sent.
Here is the success(able to send mail) code of ActionMailer in config/environments/development.rb
config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
config.action_mailer.raise_delivery_errors = false
config.action_mailer.perform_caching = false
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: '157.xxx.xxx.xxx',
port: 25,
authentication: :plain,
enable_starttls_auto: false
}
config.action_mailer.perform_deliveries = true
config.action_mailer.default_options = {from: 'hogehoge#example.com'}
Here is the failed(not able to send mail) code of ActionMailer in config/environment/production.rb
after adding sidekiq and redis
config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
config.action_mailer.raise_delivery_errors = true
config.action_mailer.perform_caching = false
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
port: 25,
address: '157.xxx.xxx.xxx', #same as development one
authentication: :plain,
enable_starttls_auto: false
}
config.action_mailer.perform_deliveries = true
config.action_mailer.default_options = {from: 'hogehoge#example.com'}
Except raise_delivery_errors, everything remains in config/environment between development and production. Is there any place needs to be fix except config while turning into production?

Related

Send email from localhost via gmail account in ruby on rails 6

I want to send an email from localhost via my gmail account in ruby on rails 6
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.default_url_options = { host: 'localhost', port: 3000 }
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: 'smtp.gmail.com',
port: 587,
domain: 'example.com',
user_name: 'mymail#gmail.com',
password: '<password>',
authentication: 'plain',
enable_starttls_auto: true }
Can anybody please help me? it is showing bad username /password SMTPAuthenticationError

Rails devise not sending an email confirmation in development

I'm configuring email confirmation to be sent out after the user signs up using devise. I did everything what this says (https://github.com/plataformatec/devise/wiki/How-To:-Add-:confirmable-to-Users) but it still does not work.
Here are some codes:
//development.rb
config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
//devise.rb
config.mailer_sender = 'myEmail#gmail.com'
# Configure the class responsible to send e-mails.
config.mailer = "Mailer"
//Mailer.rb
class Mailer < Devise::Mailer
helper :application
include Devise::Controllers::UrlHelpers
default template_path: 'devise/mailer'
end
Do I need to configure something more in order to send email confirmation letter in development environment??
Yes, You have to configure smtp settings for emails to be sent from like :
require 'tlsmail'
Net::SMTP.enable_tls(OpenSSL::SSL::VERIFY_NONE)
ActionMailer::Base.delivery_method = :smtp
config.action_mailer.perform_deliveries = true
config.action_mailer.default :charset => "utf-8"
ActionMailer::Base.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:user_name => "YOUR_EMAIL",
:password => 'PASSWORD',
:authentication => "plain",
:enable_starttls_auto => true
}
Add above code to your development.rb in order to configure smtp settings. Do add your email and password in the code where required.
Hopefully It will work fine!
In addition to #Muhammad's answer, also include these line of codes on your development.rb
config.action_mailer.default_url_options = {host: 'your server' } # ex. localhost:3000
config.action_mailer.raise_delivery_errors = true # to raise error if smtp has error on setup
config.action_mailer.default :charset => "utf-8"
In initializers/devise.rb
config.mailer_sender = 'your email'
config.action_mailer.default_url_options = { host: "localhost", port: 3000 }
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: 'smtp.gmail.com',
port: 465,
domain: 'gmail.com',
user_name: 'your email',
password: 'your password',
authentication: 'plain',
enable_starttls_auto: true,
ssl: true
}
You need to add email settings to development.rb
config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: 'smtp.gmail.com',
port: 465,
domain: 'gmail.com',
user_name: '--your-gmail-id--#gmail.com',
password: '--your-password--',
authentication: 'plain',
enable_starttls_auto: true,
ssl: true
}
then also you need to change your gmail setting.
google account settings > security > less secure app access > ON

How to change mailling url for staging environment

I created new staging environment by using fork command to my production server. Now for user registration i sent mail to user for authentication , now for staging server how can i change that mailing url. In my case on staging server mailing address is still of my production server.
try this,
config.action_mailer.default_url_options = { host: "example.com" }
Set a default host that will be used in all mailers by setting the :host option as a configuration option in config/application.rb refer this link (http://api.rubyonrails.org/classes/ActionMailer/Base.html)
You can create the file: config/initializers/action_mailer.rb with following contents:
# config/initializers/action_mailer.rb
if Rails.env.development?
# Settings for mailcatcher on dev enviroment
Rails.application.config.action_mailer.tap do |action_mailer|
action_mailer.default_url_options = {
host: 'dev-domain.dev',
port: 3000
}
action_mailer.delivery_method = :smtp
action_mailer.perform_deliveries = true
action_mailer.raise_delivery_errors = false
action_mailer.smtp_settings = { address: "localhost", port: 1025 }
end
end
if Rails.env.production?
# Define settings for Production SMTP Server
Rails.application.config.action_mailer.tap do |action_mailer|
action_mailer.default_url_options = {
host: 'production-domain.com'
}
action_mailer.delivery_method = :smtp
action_mailer.perform_deliveries = true
action_mailer.raise_delivery_errors = true
action_mailer.smtp_settings = {
address: 'mail.server.com',
port: '465',
authentication: :plain,
user_name: 'noreply#production-domain.com',
password: '',
domain: 'production-domain.com',
enable_starttls_auto: false,
ssl: true
}
end
end
if Rails.env.staging?
# Define settings for Staging SMTP Server
Rails.application.config.action_mailer.tap do |action_mailer|
action_mailer.default_url_options = {
host: 'staging-domain.com'
}
action_mailer.delivery_method = :smtp
action_mailer.perform_deliveries = true
action_mailer.raise_delivery_errors = true
action_mailer.smtp_settings = {
address: 'mail.staging-server.com',
port: '465',
authentication: :plain,
user_name: 'noreply#staging-domain.com',
password: '',
domain: 'staging-domain.com',
enable_starttls_auto: false,
ssl: true
}
end
end
You could use an environment variable for the application's URL as explained here, e.g. APPLICATION_URL = 'http://foo.herokuapp.com' in production environment and APPLICATION_URL = 'http://foo-staging.herokuapp.com' in staging environment. You can then use different URLs in different environments, by using these environment variables in your code.

Sending mail with Rails 4 in development environment

I'm new in Rails. I'm trying to make a email notification about new messages of contact form using ActionMailer. I followed different tutorials, tried to apply different advices, but it didnt help.
My setup_mail.rb file is:
config.action_mailer.delivery_method = :sendmail
ActionMailer::Base.smtp_settings = {
address: 'smtp.gmail.com',
port: 587,
domain: 'gmail.com',
user_name: 'my_user_name',
password: 'my_password',
authentication: 'plain',
enable_starttls_auto: true
}
I tried tp add this code to the development.rb file, my development.rb also has next code:
config.action_mailer.raise_delivery_errors = true
config.action_mailer.perform_deliveries = true
My contact_forms_controllers.rb has next code:
def create
#contact_form = ContactForm.new contact_form_params
#contact_form.save
redirect_to root_path
AdminMailer.notification(#contact_form).deliver
end
My admin_mailer.rb file is:
class AdminMailer < ApplicationMailer
default from: "my_address#gmail.com"
def notification(contact_form)
mail(to: "my_address#gmail.com", subject: 'New message on your web-site')
end
end
Everywhere my_address#gmail.com is my actual mail and my_password is my actual password.
I can create a message by help contact form without any errors.
Has anybody idea, where can be a problem?
You are almost there but your development.rb should look like this :
config.action_mailer.perform_deliveries = true
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: 'smtp.gmail.com',
port: '587',
domain: 'gmail.com',
user_name: 'xxx#gmail.com',
password: 'xxxxxx',
authentication: 'login',
enable_starttls_auto: true
}
Have you added this in development.rb:-
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
And change sendmail to smtp:-
config.action_mailer.delivery_method = :smtp

Action Mailer NameError: Undefined local variable or method `“smtp'

I'm attempting to set up Action Mailer to send reset password emails for Devise in my development environment. I'm receiving the following error when starting my local server: undefined local variable or method `“smtp', referring to the "address: “smtp.gmail.com”" line in my code. Here is the Action Mailer code I have added in my development.rb file:
config.action_mailer.raise_delivery_errors = true
config.action_mailer.perform_deliveries = true
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: “smtp.gmail.com”,
port: 587,
domain: ENV["GMAIL_DOMAIN"],
authentication: “plain”,
enable_starttls_auto: true,
user_name: ENV["GMAIL_USERNAME"],
password: ENV["GMAIL_PASSWORD"]
}
I have the environment variables set up in a .env file in the root directory.
Thanks!
It's because you're using smart quotes, “ ” instead of " ", probably from copy/pasting. Replace these with standard quotes:
config.action_mailer.raise_delivery_errors = true
config.action_mailer.perform_deliveries = true
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: "smtp.gmail.com",
port: 587,
domain: ENV["GMAIL_DOMAIN"],
authentication: "plain",
enable_starttls_auto: true,
user_name: ENV["GMAIL_USERNAME"],
password: ENV["GMAIL_PASSWORD"]
}

Resources