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
Related
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']
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
I am working on my first rails application and I have just pushed it to production through Heroku. The next step in my assignment is to create an account in production and have it send an email confirmation to that email.
config/environments/production.rb
config.action_mailer.default_url_options = { host: 'alex-bloccit.herokuapp.com' }
config/initializers/setup_mail.rb
if Rails.env.development?
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.smtp_settings = {
address: 'smtp.sendgrid.net',
port: '587',
authentication: :plain,
user_name: ENV['SENDGRID_USERNAME'],
password: ENV['SENDGRID_PASSWORD'],
domain: 'heroku.com',
enable_starttls_auto: true
}
end
config/initializers/setup_mail.rb
if Rails.env.development? || Rails.env.production?
config/initializers/devise.rb
config.mailer_sender = "alex.colbert1987#gmail.com"
The code updates to github and pushes to Heroku just fine, but when I go to the website and I attempt to create an email under the email. Hope this provides enough information to troubleshoot.
In config/initializers/setup_mail.rb, you have if Rails.env.development? before the ActionMailer configuration. This config won't be run in production because of the way the if statement is structured.
i am trying to set up confirmable with heroku devise for my app. here is my code: (followed a few tutorials)
config.action_mailer.default_url_options = { :host => 'myapp.herokuapp.com' }
Rails.application.routes.default_url_options[:host] = 'myapp.herokuapp.com'
config.action_mailer.perform_deliveries = true
config.action_mailer.delivery_method = :smtp
config.action_mailer.raise_delivery_errors = true
config.action_mailer.default :charset => "utf-8"
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've added the heroku variables for the password/domain/username. but when i try to sign-up on my live site, i get an error.
does anybody see any errors? or can someone point me on how I can debug this? thanks!
Heroku & Gmail have some authentication token problem while sending e-mail.
It is better to use sendgrid or Mandrill by MailChimp
They both are free & configuration is very easy
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.