How to setup Google Apps with ActionMailer with Rails 2.3.5? - ruby-on-rails

I've got Google Apps setup with email for my domain, and now I need to configure ActionMailer to use it. But the info I've found seems to be conflicting. Can anyone tell me how exactly to set it up with Rails 2.3.5?

I faced the same problems and got it working with this:
Step 1. Add the following to your development environment:
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.perform_deliveries = true
ActionMailer::Base.raise_delivery_errors = true
ActionMailer::Base.default_charset = "utf-8"
ActionMailer::Base.smtp_settings = {
:enable_starttls_auto => true,
:address => "smtp.gmail.com",
:port => "587",
:domain => "domain.com",
:authentication => :plain,
:user_name => "YOUR_USER_NAME", #should be you#domain.com
:password => "YOUR_PASSWORD_HERE"
}
The critical line is :enable_starttls_auto. You have to restart webrick after making changes here.
Step 2. Create a Model like project_mailer.rb
class ProjectMailer < ActionMailer::Base
def confirmation(project)
subject 'Your email subject'
recipients project.email
from 'you#domain.com'
body :project => "hi"
end
end
Step 3. Create a View like /views/project_mailer/confirmation.html.haml (or .erb). This is just a standard view file.
Step 4. Add a line in your controller like:
ProjectMailer.deliver_confirmation(#project)

Related

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.

Completed 500 Internal Server Error in Rails 4 Devise Signup process

When I go to signup a new user on the site I fill in the form, click submit, and it sits there for a few seconds, then dispays 'We're sorry, but something went wrong.'
The Log is below...
https://gist.github.com/th3cuda/225f1352a7cfeee3f8f8
This is deployed on digitalocean.
production.rb
# Use default logging formatter so that PID and timestamp are not suppressed.
config.log_formatter = ::Logger::Formatter.new
config.action_mailer.default_url_options = { :host => 'MY_HOST' }
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => 'smtp.MY_DOMAIN.com',
:port => 465,
:domain => 'MY_DOMAIN',
:user_name => 'MY_EMAIL',
:password => 'PASSWORD',
:authentication => 'plain',
:enable_starttls_auto => true
}
end
Since it's taking a while, it could be looking for sendmail (it uses /usr/sbin/sendmail by default) to send the email.
I would make sure that it's in place, that it can be used by the app, and that the config/(Dev, Prod, etc.) allows it to deliver email.
Is any of that helpful? Let me know what you find so I can give a better answer.
Sorry to add another answer, but formatting here is important.
config.action_mailer.default_url_options = { :host => 'MY_HOST' }
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => 'smtpout.secureserver.net',
:port => 80,
:domain => 'www.example.com',
:user_name => 'johndoe#example.com',
:password => 'yourpassword',
:authentication => 'plain'
}
end
Notice I removed the starttls option, and set port to 80. Obviously, replace with your real values. If you're using constants, please avoid using those until this gets resolved.

Load rails app config from database

I'd like to load some config settings from the database when my Rails (3.2.13) app starts:
class MyApp < Rails::Application
#...normal config here...
config.action_mailer.raise_delivery_errors = true
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => SystemSetting.system_smtp_host,
:port => SystemSetting.system_smtp_port,
:user_name => SystemSetting.system_smtp_username,
:password => SystemSetting.system_smtp_password,
:authentication => SystemSetting.system_smtp_authentication,
:enable_starttls_auto => SystemSetting.system_smtp_enable_starttls_auto }
end
But it appears that the database config has not been read at this point. I get an error:
ActiveRecord::ConnectionNotEstablished (ActiveRecord::ConnectionNotEstablished)
How can I accomplish this? Either make Rails initialize the database config first, or ... something else?
Regarding practices: Doing stuff like this can be problematic because your application relies on your database to be full when in fact it should support an empty database. Two examples that I can think of are when you try to create a database from scratch (e.g. rake db:setup), or in tests environments.
However, if have no other choice: I would move the mailer initialization to your system_setting.rb or an initializer (where you are guaranteed to have a connection).
config/initializers/load_system_settings.rb
MyApp::Application.config.action_mailer.raise_delivery_errors = true
MyApp::Application.config.action_mailer.delivery_method = :smtp
MyApp::Application.config.action_mailer.smtp_settings = {
:address => SystemSetting.system_smtp_host,
:port => SystemSetting.system_smtp_port,
:user_name => SystemSetting.system_smtp_username,
:password => SystemSetting.system_smtp_password,
:authentication => SystemSetting.system_smtp_authentication,
:enable_starttls_auto => SystemSetting.system_smtp_enable_starttls_auto
}
Manually connect and load values from database.
class Application < Rails::Application
# Set your app path
app_base = 'MY_APPLICATION_PATH'
# Load db configs
db_yml = YAML.load_file("#{app_base}/config/database.yml")["production"]
# Establish db connection
ActiveRecord::Base.establish_connection(:adapter=>db_yml['adapter'], :database=>db_yml['database'], :username=>db_yml['username'], :password=>db_yml['password'])
# load model if not
require "#{app_base}/app/model/system_setting.rb" unless defined?('SystemSetting')
#...normal config here...
config.action_mailer.raise_delivery_errors = true
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => SystemSetting.system_smtp_host,
:port => SystemSetting.system_smtp_port,
:user_name => SystemSetting.system_smtp_username,
:password => SystemSetting.system_smtp_password,
:authentication => SystemSetting.system_smtp_authentication,
:enable_starttls_auto => SystemSetting.system_smtp_enable_starttls_auto }
#close sql connection after loaded
ActiveRecord::Base.connection.close
end

Customize the From address in Rails application

I have mailer action in my application, the mailer is configured with gmail smtp. The following is my config details under environment.rb file
require "smtp_tls"
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.perform_deliveries = true
ActionMailer::Base.raise_delivery_errors = true
ActionMailer::Base.default_charset = "utf-8"
ActionMailer::Base.default_content_type = "text/html"
ActionMailer::Base.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => 'gmail.com',
:user_name => "info#example.com",
:password => "password",
:authentication => :plain
The think i want to implement is, when ever the application generating email the from address shows "info#example.com".
Is possible to customize the from address. In different places i want to use different From address instead of "info#example.com"
I tried with my mailer model:
#from = "#{user.email}"
In development server log it shows the customized id correctly. if go my email inbox it shows the from address as "info#example.com"
Can any one please guide on this. thanks in advance.
It's not #from. It's the method from.
def my_email_sender_method
from "nobody <noreply#example.com>"
end

Rails: Runtime configuration of ActionMailer?

I would like to send a small amount of email from my app through Gmail. Now, the SMTP settings will be determined at runtime (ie: from the db), can this be done?
--- edit ---
I can set the ActionMailer subclass (named Notifier) smtp settings in one of the class' methods. This way I can set the username and password for sending the email dynamically. The only thing is that you have to set ALL the smtp_settings. Is it possible to set just the username & password settings in the class method?
This is the code I'm using right now, it is sending:
class Notifier < ActionMailer::Base
def call(user)
Notifier.smtp_settings = {
:enable_starttls_auto => true,
:address => "smtp.gmail.com",
:port => "587",
:domain => "mydomain.com",
:authentication => :plain,
:user_name => "fabian#mydomain.com",
:password => "password"
}
recipients user.email
subject "Test test"
body "Test"
end
end
I would like to just set the username and pw here.
(Rails 3)
Since I call mailer like this:
CustomerMailer.customer_auto_inform(#form).deliver
In CustomerMailer class I have private method:
def init_email_account(shop_mail)
ActionMailer::Base.raise_delivery_errors = true
ActionMailer::Base.smtp_settings = {
:address => shop_mail.address,
:port => shop_mail.port,
:domain => shop_mail.domain,
:user_name => shop_mail.user_name,
:password => shop_mail.password,
:authentication => shop_mail.authentication.name,
:enable_starttls_auto => shop_mail.enable_starttls_auto
}
end
Before calling mail() which sends email you need to call private method init_email_account to populate smtp_settings from database. shop_mail is model which stores the data about mail account settings.
HTH
Since the configuration files are all Ruby, then the settings can easily be fetched from a configuration file or the like at runtime.
Here's a post I wrote a while back on getting ActionMailer working with GMail SMTP.
NOTE: If you're using rails 2.3 and Ruby 1.87, you don't need the plugin and can simply use the settings in this comment
Assuming you've configured the smtp_settings in your environment or initializers, you can just set the username like so:
Notifier.smpt_settings.merge!({:user_name => "x", :password=> "y"})
You can do that in the controller:
class ApplicationController < ActionController::Base
...
private
def set_mailer_settings
ActionMailer::Base.smtp_settings.merge!({
username: 'username',
password: 'yoursupersecretpassword'
})
end
end

Resources