I follow the instruction in vs code after i run rails g devise:install
I added config.action_mailer.default_url_options = { host: 'localhost', port: 3000 } in development.rb
I also uncomment the confirmable in migration file .
I also added confirmable in user.rb
In environment.rb this is what i have
# Load the Rails application.
require_relative "application"
# Initialize the Rails application.
Rails.application.initialize!
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.perform_deliveries = true
ActionMailer::Base.smtp_settings = {
address: 'smtp.gmail.com',
port: 465,
domain: 'gmail.com',
user_name: ENV['test#gmail.com'],
password: ENV['123456'],
authentication: 'plain',
:ssl =>true,
:tsl => true,
enable_starttls_auto: true
}
Im not sure on how to put the correct username and password .
Im not getting any error message when i go to signup but when i go to the email that i use to sign up , im not getting any email .
I also enable less secure app in google settings for both email .
Please help
Thank you
The modern Rails approach (5.1+) is to use encrypted secrets. When you run rails credentials:edit Rails will open a basic credentials file in your editor of choice. You can then just add your Gmail password and email to the file:
gmail:
password: your_password_goes_here
user_name: your-email-address#gmail.com
Rails will then create the config/credentials.yml.enc file that can be checked into version control. The file is encrypted with your master.key file which should not be checked into version control.
You can then access the encrypted secrets with Rails.application.credentials:
ActionMailer::Base.smtp_settings = {
address: 'smtp.gmail.com',
port: 465,
domain: 'gmail.com',
user_name: Rails.application.credentials.gmail.fetch(:user_name),
password: Rails.application.credentials.gmail.fetch(:password),
authentication: 'plain',
ssl: true,
tsl: true,
enable_starttls_auto: true
}
Firstly you need to install a gem such as dotenv-rails.
This will allow you to use environment variables.
To do so create a .env file in the root of your project.
Inside of that file you can add environment variables such as:
EMAIL_USERNAME=someusername
EMAIL_PASSWORD=somepassword
And then in your environment.rb you can use it like this:
user_name: ENV['EMAIL_USERNAME'],
password: ENV['EMAIL_PASSWORD']
Related
I'm using Rails Mailer to send confirmation mails on my app, and I've set up my config>environment>development.rb like below:
config.action_mailer.smtp_settings = {
address: "smtp.gmail.com",
port: 587,
domain: 'smtp.gmail.com',
# user_name: ENV["MAIL_USERNAME"],
# password: ENV["MAIL_PASSWORD"],
user_name: "myemail#mydomain.com",
password: "myCorrectPassword",
authentication: :login,
enable_starttls_auto: true
}
Problem
When I hardcode the user_name and the password for "myemail#mydomain.com" and "myCorrectPassword", the mailer sends an email and it works fine. But when I use the ENV to make sure that my credentials are safe, I get the Net::SMTPAuthenticationError: 530-5.7.0 Authentication Required error.
On Terminal, I checked my ENV to make sure that I've entered correct values for each variable, and they were right.
What else can I try?
What do you get when you run this in rails console ENV["MAIL_USERNAME"] ??
Does your ~/.bash_profile has this
export MAIL_USERNAME="myemail#mydomain.com"
Try to source ~/.bash_profile once
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
I have a Rails application and I put all my important configurations, e.g. sendgrid, new relic, twilio, airbrake, etc, in a config/config.yml file. The file looks as follows:
development:
sendgrid:
username: username
password: password
test:
sendgrid:
username: username
password: password
production:
sendgrid:
username: username
password: password
Then in config/initializers/global_configuration.rb, I load the correct environment configuration:
APP_CONFIG = YAML.load_file("#{Rails.root}/config/config.yml")[Rails.env]
Now I want to be able to access this global constant in config/environments/development or config/environments/production, as so:
config.action_mailer.smtp_settings = {
address: 'smtp.sendgrid.net',
port: 587,
domain: APP_CONFIG['sendgrid']['domain'],
authentication: "plain",
enable_starttls_auto: true,
user_name: APP_CONFIG['sendgrid']['username'],
password: APP_CONFIG['sendgrid']['password']
}
Unfortunately, when Rails starts up, it throws the following error:
Uncaught exception: uninitialized constant APP_CONFIG
It appears that config/environments is loaded before config/initializers. How can I get around this so I can access my global constant in config/environments?
It appears config/application.rb is loaded before the config/environments/*.rb files, so I was able to hook into the before_configuration block and then create a global variable within it:
config.before_configuration do
::APP_CONFIG = YAML.load_file("#{Rails.root}/config/config.yml")[Rails.env]
end
If there is a better option (rather than using ENV), I will gladly delete this answer and upvote the better one.
Do this in all the environment file inside config/environment/
config.after_initialize do
config.action_mailer.smtp_settings = {
address: 'smtp.sendgrid.net',
port: 587,
domain: APP_CONFIG['sendgrid']['domain'],
authentication: "plain",
enable_starttls_auto: true,
user_name: APP_CONFIG['sendgrid']['username'],
password: APP_CONFIG['sendgrid']['password']
}
end
Below is my environment.rb file
require File.expand_path('../application', __FILE__)
Wiyo::Application.initialize!
config.action_mailer.smtp_settings = {
address: "smtp.gmail.com",
port: 587,
domain: "example.com",
authentication: "plain",
enable_starttls_auto: true,
user_name: 'atomambition#gmail.com',
password: '********'
}
I have put my gmail password in ** section. But when i do rake:db migrate, it shows error on environment.rb file telling me
7:in '>'.
Please help me. I am creating mailing function in my rails app. I am stuck.
This configuration code should not be included in environments.rb. The config global is localized to the environment specific configuration files found within config/environments. Try adding this code to the appropriate file there:
# config/environment/development.rb
Wiyo:::Application.configure do
config.action_mailer.smtp_settings = {
address: "smtp.gmail.com",
port: 587,
domain: "example.com",
authentication: "plain",
enable_starttls_auto: true,
user_name: 'atomambition#gmail.com',
password: '********'
}
# remainder of configuration
end
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.