ActionMailer SMTP working in production but not locally - ruby-on-rails

Seems to be the opposite problem to a lot of the questions on here. I've not been able to get the mailers working locally but then I deployed my app to Heroku and everything is fine.
No apparent errors when sending locally. Strangely though, I can have incorrect credentials locally and it will still appear to have sent ok. Almost as if it's not trying to deliver through smtp. I get a message like this in the console after sending:
UploadMailer#complete: processed outbound mail in 32.1ms Delivered mail 63ded40c98545_b9cbb90-440#Chriss-MacBook-Pro.local.mail (1238.6ms)
There's nothing in the SendGrid dashboard showing that it has reached their servers.
config/application.rb
...
config.action_mailer.delivery_method :smtp
ActionMailer::Base.smtp_settings = {
:user_name => 'apikey',
:password => ENV['SENDGRID_API_KEY'],
:address => 'smtp.sendgrid.net',
:domain => ENV['SENDGRID_DOMAIN'],
:port => 587,
:authentication => 'plain',
:enable_starttls_auto => true
}
Mailer and mailer view
class UploadMailer < ApplicationMailer
def complete
mail(to: 'me#myemail.com', subject: 'foo')
end
end
# app/views/upload_mailer/complete.html.erb
<h2>
Hello
</h2>
Environment
Ruby: 3.0.2 (rbenv)
Rails: 6.1.7.2
Tried on Mac and Windows(WSL)

Looks like I was just missing
config.action_mailer.perform_deliveries = true
in config/environments/development.rb

Related

Invalid delivery method error when trying to add :confirmable to Users in rails

I added the confirmable attribute to User by following this github wiki
https://github.com/plataformatec/devise/wiki/How-To:-Add-:confirmable-to-Users
After that I want sendgrid server to send email confirmation link for that I added an addon in my heroku and do these changes to configure my environment.
In my config/environment.rb file I added the following code
ActionMailer::Base.smtp_settings = {
:user_name => ENV['SENDGRID_USERNAME'],
:password => ENV['SENDGRID_PASSWORD'],
:domain => 'heroku.com',
:address => 'smtp.sendgrid.net',
:port => '587',
:authentication => :plain,
:enable_starttls_auto => true
}
In my development.rb file I added these two lines
config.action_mailer.delivery_method = :text
config.action_mailer.default_url_options ={:host => 'http://localhost:3000'}
In my production.rb file I added these two lines
config.action_mailer.delivery_method = :smtp
config.action_mailer.default_url_options ={:host => 'finance-tracker-6.herokuapp.com', :protocol => 'https'}
After doing all this.When I am signing up I am getting below error
RuntimeError in Devise::RegistrationsController#create
Invalid delivery method :text
There's no such delivery method as text. If you don't want to send SMTP emails in development, you can either use the file delivery method to get the emails saved in a file (it's also possible to preview emails), or use something like the letter_opener gem and set the delivery method to letter_opener. With this, emails get displayed in your default browser instead of actually getting sent.

Why can't I see any emails in my inbox sent from my Heroku Rails app using SendGrid SMTP Relay in development mode?

I'm trying to switch over to SendGrid from Mandrill in my Rails 4.2 app through SendGrid's SMTP Relay. I have set the 'To Email' to be my personal email address so that I can view the emails that have been sent, however none of the emails actually appear in my inbox despite the rails console claiming to have processed and sent the email.
I am fairly certain all my mailers have the appropriate smtp settings as I have mostly followed the instructions provided on the SendGrid website: https://sendgrid.com/docs/Integrate/Frameworks/rubyonrails.html
I have also tested my connectivity to SendGrid's SMTP Relay through telnet and the connection is succesful.
My SendGrid dashboard indicated that 0 emails have been sent. None of my emails appear under the Suppressions tab either so it's not like they have bounced or have been blocked.
This is in my config/environment.rb:
ActionMailer::Base.smtp_settings = {
:user_name => 'apikey',
:password => ENV['SENDGRID_API_KEY'],
:domain => 'heroku.com',
:address => 'smtp.sendgrid.net',
:port => 587,
:authentication => :plain,
:enable_starttls_auto => true
}
This is in my config/environments/development.rb:
config.action_mailer.delivery_method = :smtp
config.action_mailer.perform_deliveries = true
config.action_mailer.default_url_options = { :host => 'smtp.sendgrid.net' }
This is the line in my controller that calls my ApplicationMailer:
ApplicationMailer.send_email(user, 'mypersonalemail#email.com', 'Test Subject').deliver
And this is what gets printed in the console when the mailer method is executed:
ApplicationMailer#send_email: processed outbound mail in 789.9ms
Sent mail to mypersonalemail#email.com (103.4ms)
But I still don't get any emails in my inbox or spam folder. Does anyone know how I can solve this? Thanks in advance.
Your domain and host options are wrong. Use localhost:3000 (unless you're using docker or something at which point replace localhost:3000 with 0.0.0.0:8000)
#/environments/development.rb
#Mailer Options
config.action_mailer.delivery_method = :smtp
config.action_mailer.perform_deliveries = true
ActionMailer::Base.smtp_settings = {
:address => 'smtp.sendgrid.net',
:port => '587',
:authentication => :plain,
:user_name => ENV['SENDGRID_USERNAME'],
:password => ENV['SENDGRID_PASSWORD'],
:domain => 'localhost:3000',
:enable_starttls_auto => true
}
config.action_mailer.default_url_options = { host: 'http://localhost:3000' }
config.action_mailer.asset_host = 'http://localhost:3000'
Make sure to add the sendgrid credentials to your local machine as environment vars. To get them, go to your heroku app and click on settings, then "reveal config vars". Then add those sendgrid credentials to your local machine as env. vars and you're done.

Send Devise E-mails using Mandrill in Production

I'm trying to send the Devise e-mails from my Rails 4 app. I don't want to change the templates or anything, I just want to use Mandrill to deliver the messages in the production environment.
My production.rb is like this:
config.action_mailer.raise_delivery_errors = true
config.action_mailer.perform_deliveries = true
config.action_mailer.default_url_options = {:host => 'sociedadeavalia.com.br'}
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => "smtp.mandrilapp.com",
:port => 587,
:user_name => ENV['MANDRILL_USERNAME'],
:password => ENV['MANDRILL_APIKEY'],
:authentication => 'login',
:enable_starttls_auto => true,
:domain => 'sociedadeavalia.com.br'
}
I have this in my devise.rb
config.mailer_sender = 'no-reply#sociedadeavalia.com.br'
config.mailer = 'MyDeviseMailer'
And I created the following mailer:
class MyDeviseMailer < Devise::Mailer
helper :application
include Devise::Controllers::UrlHelpers
default template_path: 'devise/mailer'
end
Now Whenever i try to send an email in production (the confirmation email for creating a new user account) my app just crashes and this appears in the log:
31 <190>1 2014-12-03T22:17:07.987969+00:00 app web.1 - - Net::OpenTimeout (execution expired):
What should I do in this case?
It looks like you might be having an issue with the port being blocked. We'd recommend the troubleshooting steps here. More specifically, try changing the port you're using - 2525 is often not blocked, or use 465 with SSL enabled. Many hosts block 25 and 587 to help prevent spam.
Try changing the authentication to 'plain'. That is what is working for me.
:authentication => 'plain',

Why is my Heroku app not sending emails in production with Sendgrid?

My devise emails work fine in development.
But now that I have pushed to Heroku and am using the sendgrid add-on, they don't get sent. I don't get an error. It seems like it is sent just fine, it is just that it never actually reaches my inbox.
This is my config/environment/production.rb file:
config.action_mailer.delivery_method = :smtp
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = false
config.action_mailer.default :charset => "utf-8"
config.action_mailer.default_url_options = { :host => 'http://myapp.herokuapp.com' }
config.action_mailer.smtp_settings = {
:user_name => ENV["SENDGRID_USERNAME"],
:password => ENV["SENDGRID_PASSWORD"],
:address => 'smtp.sendgrid.net',
:domain => 'myapp.herokuapp.com',
:port => 587,
:authentication => :plain,
:enable_starttls_auto => true
}
I checked those config vars on Heroku, and they return valid results.
Any ideas?
P.S. I don't have my domain pointing to the Heroku App yet, I just want to test it with the stock Heroku settings first.
Everything look good, did you check if the sendgrid account is provisioned?
If you are on heroku, go to the sendgrid panel to finish the setup, just visit the app profile and under resources(which is the default) you should see the sendgrid add-on, click it and just make sure you have everything setup. In this case it will ask you to setup a template, but you can 'skip' this.
Hope this helps!
This was happening to me too, or something like it. I was using the emails to only send to one email address, for my personal notifications.
It turns out that the email address "bounced" the first time and was being "suppressed" at Sendgrid for future sends. I would check the "suppressions" tab to make sure that's not happening to you.

Sendgrid set up on Rails 4

I have a rails 4 app. I set up ActionMailer and I can send order confirmation emails via localhost and gmail.
I installed Sendgrid on Heroku and followed the set up instructions. I get a Net::SMTPSyntaxError (501 Syntax error
my environment.rb (i have sendgrid user/pwd in application.yml)
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
}
in production.rb - the only actionamailer setting i have is this. I have this as a placeholder to put the real domain in later. I'm currently using herokuapp.com.
config.action_mailer.default_url_options = { host: 'localhost:3000' }
in my orders_controller within the order create method, I call the below.
AutoNotifier.orderconf_email(current_user, #order).deliver
auto_notifier.rb
class AutoNotifier < ActionMailer::Base
default from: "Test Email"
def orderconf_email(current_user, order)
#buyer = current_user
#order = order
mail(to: #buyer.email, subject: 'Thank you for your order.')
end
end
What am I missing? It works on localhost with gmail so I'm missing something in the sendgrid settings or in the default_url in production.rb file.
For posterity, here's a working setup for external SMTP in Rails on Heroku:
#config/environments/production.rb
config.action_mailer.smtp_settings = {
:address => "smtp.sendgrid.net",
:port => 587, # ports 587 and 2525 are also supported with STARTTLS
:enable_starttls_auto => true, # detects and uses STARTTLS
:user_name => ENV["SENDGRID_USERNAME"],
:password => ENV["SENDGRID_PASSWORD"], # SMTP password is any valid API key, when user_name is "apikey".
:authentication => 'login',
:domain => 'yourdomain.com', # your domain to identify your server when connecting
}
Change default from: "Test Email" to valid email address, even example#example.com.
I would just like to point out, this is for sending emails via SMTP. While this method is totally ok, you should also consider sending via the API.
To do this, you need to specify an interceptor. Luckily, there's a Gem that helps with that. Here's a good article showing how to use it.
https://rubyplus.com/articles/561-Sending-Emails-using-SendGrid-API-in-Rails-4-1
It took us a long time to resolve the issue when we tried to deploy the SMTP relay on heroku. It worked perfectly fine on local but when pushed we received socket errors and time out issues. Eventually got it working.
Important note: Make sure not to use starttls_auto and SSL/or TLS this causes open SSL issue.

Resources