Readmine doesn`t send mail - ruby-on-rails

I add to configuration.yml email settings. But notifications not come to email. I try to send test mail from settings page, but also I doesn`t recieve any emails.
configuration.yml:
default:
email_delivery:
smtp_settings:
address: mail.mysite.ru
port: 587
authentication: :login
user_name: "redmine#mysite.ru"
password: "mypass"
Production.log (https://yadi.sk/d/31jli2yOiraHn1) doesn`t contains mail errors, redmine.error.log is empty.

I don't know if you're missing smtp settings for development/production environment. If so, try
default: &default
email_delivery:
smtp_settings:
# Your settings
development:
<<: *default
production:
<<: *default
# Override default settings if necessary
user_name: "redmine#othersite.ru"
password: "otherpass"
Since you're using port 587, I suppose your smtp server uses StartTLS to secure mail exchanges. So you have to add the following line in your smtp_settings:
enable_starttls_auto: true
And if the smtp server is using untrusted SSL certification, then add this line too:
openssl_verify_mode: 'none'
Try sending email again. If still not working, then try changing authentication: :login to authentication: :plain.

Related

email confirmation using devise

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']

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

Email configuration in Redmine - undefined method `address='

I'm not an expert in Ruby and Redmine, but I'm trying to configure email notifications in redmine.
Redmine version 3.4
ruby 2.3.3p222
Phusion Passenger 5.1.7
In configuration.yml I have
production:
email_delivery:
delivery_method: :smtp
smtp_settings:
address: "xxxx.pl"
port: 587
authentication: :plain
domain: 'redmine.xxx.pl'
user_name: '.....#redmine.xxx.pl'
password: '......'
But I get this error:
Error ID: 54731089
Error details saved to: /tmp/passenger-error-WqNBmN.html
Message from application: undefined method `address=' for ActionMailer::Base:Class (NoMethodError)
I search little bit and figure out, that I have missing methods in application.rb file.
So in Redmine config/application.rb I add something like that:
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: "xxx.pl",
port: 587,
user_name: 'powiadomienia#redmine.xxx.pl',
password: '.....',
authentication: :plain
}
but still have the same error. Unfortunately I can't find any examples in Redmine configuration.
Can anyone help me?
Make sure your configuration.yml is properly indented (use 2 spaces per level of indentation):
production:
email_delivery:
delivery_method: :smtp
smtp_settings:
address: 'example.org'
port: 587
The smtp settings (address, port etc) need to be keys in the smtp_settings hash. From the error you get I'd say they are keys on the same level as smtp_settings, which is wrong.

loading the initializer before the environment

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

Redmine email configuration with environment variables

I've configured email on redmine according to the instructions in the redmine wiki, and it all works fine. Here are the contents of my config/configuration.yml file:
production:
delivery_method: :smtp
smtp_settings:
address: "smtp.sendgrid.net"
port: 587
authentication: :plain
domain: "heroku.com"
user_name: my_email#gmail.com
password: my_password
However I am trying to use environment variables in place of my_email#gmail.com and my_password like so:
production:
delivery_method: :smtp
smtp_settings:
address: "smtp.sendgrid.net"
port: 587
authentication: :plain
domain: "heroku.com"
user_name: <%= ENV['SENDGRID_USERNAME'] %>
password: <%= ENV['SENDGRID_PASSWORD'] %>
When I try to send a test email, with the environment variables in the config file, redmine give this error:
'An error occurred while sending mail (535 Authentication failed: Bad username / password )'.
So I guess the erb snippet is not being evaluated (I have double checked the values of the environment variables).
I've searched for a solution and come up empty, does anyone have any suggestions as to how I should configure email in redmine so that I don't expose my sendgrid credentials in the config file?
Alternatively if someone can tell me that it's not a security risk to use the credentials directly, without environment variables, that would also solve my problem.
I had answered this question 2 weeks ago, and it was deleted by someone else right away.
So I'm not sure if someone will delete this answer again to prevent other guys knowing how to solve this problem. Good luck!
I got the same issue and this article
Set up mailing in redmine on heroku solved my problem.
The idea is moving the settings from config/configuration.yml to config/environments/production.rb and using Ruby code to set it up. Since ENV['key'] is handled by erb, I guess configuration.yml is not handled that way. Maybe there is some security issue?
So in your case, you should add these codes to config/environments/production.rb as follows,
ActionMailer::Base.smtp_settings = {
:address => 'smtp.sendgrid.net',
:port => '587',
:authentication => :plain,
:user_name => ENV['SENDGRID_USERNAME'],
:password => ENV['SENDGRID_PASSWORD'],
:domain => 'heroku.com'
}
ActionMailer::Base.delivery_method = :smtp
And remove codes from config/configuration.yml and make it looks like this,
default:
# Outgoing emails configuration (see examples above)
email_delivery:
delivery_method: :smtp

Resources