localhost on rails - ruby-on-rails

I am sending an email that contains a link to my website. I want to be able to test it locally and be able to move the scripts around to different hosts easily.
In my email right now I use the following:
<%= url_for(:host => 'localhost:3000', :controller => "user_activations", :action => "show", :id=>#id, :confirm=>#passcode) %>
This works for when testing locally but will obviously fail for production. Is there an easy way to have rails (or ruby) detect what the current host is? I'm thinking something like $_SERVER of php.
I realize I can use some logic using my environment variable but I would like to avoid this.
Thanks

I define a constant 'HOST' in my environment.rb that sets my host. Alternatively you can use request.host or request.domain.

in environments/development.rb
config.action_mailer.default_url_options = { :host => "localhost", :port => 3000 }
in environments/production.rb
config.action_mailer.default_url_options = { :host => "www.xyu.at" }
and use tests with rspec-email :)

Related

EOF error in Rails utilizing the devise gem

I've recently set up the devise gem for authentication and all is working well excluding the forgot my password function. My smtp is currently pointed towards a yahoo email address. Yet, I get a EOF end of file extension error. In development it delivers message just fine ,but to no avail in production. Also, I initialized the ENV variables for username and password with Heroku Config:set .Addtionally, I tried port 587. Any help would be much appreciated! I'm fairly new to rails and searched for similar log issues, but most varied significantly in similarity to this issue. Thank you!
config.action_mailer.perform_caching = false
config.action_mailer.default_url_options = {:host => 'https:app.herokuapp.com' }
config.action_mailer.delivery_method = :smtp
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
ActionMailer::Base.smtp_settings = {
:user_name => ENV['example#yahoo.com'],
:password => ENV['examplepass'],
:domain => 'https://example.herokuapp.com/',
:address => 'smtp.mail.yahoo.com',
:port =>465,
:authentication => "plain",
:enable_starttls_auto => true,
}
My first think:
I think your file have non visible characters, try to clean your file. https://alvinalexander.com/blog/post/linux-unix/how-remove-non-printable-ascii-characters-file-unix
Erase the file and create a new one an rewrite the config, avoid the copy paste. maybe works if you do a copy paste without format to note bloc, word something like that and then copy that back to your file.
Also read more What is an EOFError in Ruby file I/O?
https://airbrake.io/blog/ruby-exception-handling/eof-error
Continue searching i found this
EOFError in Devise::PasswordsController#create
Devise mailer EOF error
a user comment this
Ah classic. My config/secrets.yml isn't tracked in git (naturally) and >looks like it was overwritten and lost those entries for the email >provider smtp/username/password at some point. Thanks
Another one Rails EOFError (end of file reached) when saving a devise user
The resolution to my issue was two fold. I didn't have the heroku config vars properly set up and initialized.My .gitignore file didn't have the exclusions as well to prevent uploading to repository. In conclusion, I added the figaro gem, bundle exec install figaro , and it created the application.yml needed to store that actual user/pass information. https://railsapps.github.io/rails-environment-variables.html ----> quite useful in explaining the options for setting the vars.
:user_name => ENV['SENDGRID_USERNAME'],
:password => ENV['SENDGRID_PASSWORD'],
:domain => 'heroku.com',
:address => 'smtp.sendgrid.net',
:port =>587,
:authentication => :plain,
:enable_starttls_auto => true,
Also in the above code , I didn't have domain specified to heroku.com but my application domain on Heroku. For production.rb you do need the default url set to your applications name. If you kept the default url "heroku.com" in production.rb it would 404 error when the emailed link was clicked on.
config.action_mailer.default_url_options = {:host => 'https://exampleapp.herokuapp.com/' }
Thanks for the help! I also hope this can be of help to other new rails devs.

What is ActionMailer default_url_options?

I must not understand something trivial about email but what does the host in defaul_url_options do? The need for the smtp settings make sense to me to configure how the email will be sent out but how is default_url_options relevant to that?
config.action_mailer.raise_delivery_errors = true
config.action_mailer.delivery_method = :smtp
host = '<your heroku app>.herokuapp.com'
config.action_mailer.default_url_options = { host: host }
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
}
The default_url_options setting is useful for constructing link URLs in email templates. Usually, the :host, i.e. the fully qualified name of the web server, is needed to be set up with this config option. It has nothing to do with sending emails, it only configures displaying links in the emails.
The need for setting this is nicely documented in the Rails Guides as well ActionMailer::Base sources:
URLs can be generated in mailer views using url_for or named routes. Unlike controllers from Action Pack, the mailer instance doesn't have any context about the incoming request, so you'll need to provide all of the details needed to generate a URL.
When using url_for you'll need to provide the :host, :controller, and :action:
<%= url_for(host: "example.com", controller: "welcome", action: "greeting") %>
When using named routes you only need to supply the :host
<%= users_url(host: "example.com") %>
So, to reword the docs, in web pages, the name of the current web server (to be used in absolute links) is taken from the incoming request info. But you don't have this information available when rendering an email (there's no request), that's why you have to provide it manually, so that links in emails work properly.
Have you ever tried to generate URLs within an ActionMailer template? If you did at least once, then you are probably familiar with the following error:
ActionView::TemplateError (Missing host to link to! Please provide :host parameter or set default_url_options[:host])
This happens because ActionMailer instance doesn't have any context about the incoming request so you'll need to provide the :host, :controller, and :action:. If you use a named route, ActionPack provides controller and action names for you. Otherwise, with the url_for helper you need to pass all the parameters.
<%= message_url %>
<%= url_for :controller => "messages", :action => "index" %>
Regardless your choice, you always need to provide the host option to generate an URL in ActionMailer. As shown by the ActionMailer guide, you basically have two ways to pass the host value to ActionMailer:
1. set a global value
2. pass the option each time you generate an URL
Defining default_url_options is better then passing URL everytime.
That's why we do.

Rails Devise Mailer: No Received Messages in Inbox

I'm using Rails 4 and Devise 3. I need to send confirmation e-mails for production. These are the SMTP configs for my config/environments/production.rb
config.action_mailer.default_url_options = { :host => 'smtp.gmail.com' }
config.action_mailer.raise_delivery_errors = false
config.action_mailer.perform_deliveries = true
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => "smtp.gmail.com",
:tls => true,
:port => '587',
:user_name => 'my_email#gmail.com',
:password => 'my_password',
:authentication => 'plain',
:enable_starttls_auto => true
}
Logs say that the e-mail's been sent. However, I don't see anything in the inbox. (yes, mailcatcher is off)
Another question, do the configs of the development file affect the production's environment's in any ways? They shouldn't, correct?
Another important question: Using the way above, how many e-mails can be handled? For an example, if I used a third party say, Mandrill, would be better because up for tens of thousands of e-mails can be handled. What about this way?
P.S I've already tried Mandrill and it worked just fine. I am requested not to use a third party though so I won't be able to use Mandrill.
Lastly, is there any other way of sending the confirmation e-mails from the Rails Devise that I'm unaware of yet? Or are there any other configurations that I need to do OUTSIDE OF RAILS to make this work since I won't be using a third party?
Please make sure you have enter correct host name .
config.action_mailer.default_url_options = { :host => 'your domain name' }
As Rails configuration standard if your application running on local machine it loads development env. file settings, if it is production then it load production env. file settings.
I prefer to use sendgrid or Mandrill by MailChimp, if you ave large application then it is better to use 3rd party addons, for small application you can use Gmail .
hope this will help you :)

Rails: generate a full URL in an ActionMailer view

I'm using ActionMailer to send a sign up confirmation email. The email needs to contain a link back to the site to verify the user, but I can't persuade Rails to generate a full URL (including the domain etc).
I'm using:
<%= url_for :controller => 'login', :action => 'verify', :guid => #user.new_user.guid, :only_path => false, :host => 'http://plantality.com' %>
in my view
Part b:
In development mode Rails gripes if I don't specify the host explicilty in the link above. But I don't want to do this in production. Any solutions?
To solve the problem to pass a host for generating URLs in ActionMailer, check out this plugin and the reason why I wrote it.
To solve the first issue, use named routes when applicable. Instead of
<%= url_for :controller => 'login', :action => 'verify', :guid => #user.new_user.guid, :only_path => false, :host => 'http://plantality.com' %>
assuming the route is called login, use
<%= login_url(:guid => #user.new_user.guid) %>
Note, I'm using login_url, not login_path.
I'm not sure if it is what you want but in config/environments/development.rb you can specify default options for mailer urls
config.action_mailer.default_url_options = {
:host => "your.host.org",
:port => 3000
}
you can do the same in config/environments/production.rb
I don't know why the previous solutions seem so complicated, but since I'm here why not give my 2 cents...
Go to /config/environments and add:
config.absolute_site_url = 'your site url'
for the respective environment (ie. in development.rb, test.rb, or production.rb). Restart web server.
This allows you to call Rails.application.config.absolute_site_url to get the desired URL. No need for plugins or weird cheat, just store the site url as an application wide variable.
I think its not 100% correct way but this can also be a solution :
See the Using asset hosts section in the documentation. You need to specify an asset_host. You can also construct it dynamically from the request chaining "#{request.protocol}#{request.host_with_port}/login/?guid=#{#user.new_user.guid}"
To generate url, try this
Rails.application.routes.url_helpers.user_url(User.first.id, host: 'yourhost.io')
this will generate url like this:
http://yourhost.io/users/1
As well you can pass some params
expires = Time.now + 2.days
params = {expires: expires}
u = User.first.id
Rails.application.routes.url_helpers.user_url(u, params, host: 'host.com')
will generate:
http://yourhost.io/users/1.expires=2018-08-12+15%253A52%253A15+%252B0300
so you can werifi in action if link is not expired

How do I configure the hostname for Rails ActionMailer?

I'm working on a fairly traditional forgot password email - I want to email the user a password change token embedded in a link that they can click on in order to change their password. I'm emailing via the traditional ActionMailer.
If I use a normal link_to tag
<%= link_to "click here", :controller => foo, :action => 'bar', :token => token %>
I get a relative link - rather useless from an email.
If I add in
:only_path => false, then it errors saying I need to set default_url_options[:host]. The ActionController docs imply that you do that by overriding the #default_url_options methods in your controller. Surely there's a configuration option to tell Rails what it's hostname is without adding my own config file, parsing it, etc?
default_url_options is available from config.action_mailer and should be set in your environment's configuration file.
For example, in config/environments/production.rb:
config.action_mailer.default_url_options = {
:host => 'www.yourdomain.com'
}
For local testing, modify config/environments/development.rb:
config.action_mailer.default_url_options = {
:host => '127.0.0.1',
:port => 3000
}
Then, assuming you have a named route called forgot_password_login, you can generate the login link URL in your mailer using something like this:
forgot_password_login_url(:token => 'a7s8q15sk2...')
You probably want to set :protocol => 'https' as well, btw.
config.action_mailer.default_url_options = {
:host => "portal.example.com",
:protocol => 'https'
}
There is another alternative, as described in http://pivotallabs.com/how-i-leaned-to-stop-hating-and-love-action-mailer/
This solution has the advantage that it doesn't require any configuration (so less of a hassle), and works fine as long as you send emails from within controllers.
But if you plan on sending email without going through a controller (e.g. from command line or in response to another email), you need the static configuration.
Setting default_url_options directly is deprecated in Rails 3.1. Use url_for instead.
Add parameter :protocol to override default value (http), :protocol => 'https://'. This will create url starting with "https://..." instead of default "http://"
Interestingly, I had the same issue as you did, but in unit tests (while following Michael Hartl's railstutorial). I had this line in my test.rb file, but that didn't help:
config.action_mailer.default_url_options = { host: 'example.com', protocol: 'http' }
I've also added another line like this to test.rb, and surprisingly this solved the issue
default_url_options = { host: 'example.com', protocol: 'http' }
Setting default_url_options directly is deprecated in Rails 3.1
Use the url_for helper to create it:
<%= link_to "click here", url_for(:controller => foo, :action => 'bar', :token => token, :host => 'www.yourdomain.com') %>
Can you just do
<%="click here", :controller => foo, :action => 'bar', :token => token, :host=>request.host -%>

Resources