Rails: Runtime configuration of ActionMailer? - ruby-on-rails

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

Related

Dot missing(on domain name) of password reset email link send by SendGrid

I am using SendGrid mailer on Ruby and Rails framework. In password reset email template we are sending a password reset link which looks like the following format (https://subdomain.domainname.com/password_reset/token/?some_other_params). Most of the time the password reset link is emailed to recipient in correct format but for some customer it is not sending the proper link. The issue we noticed is "the dot is missing between (subdomain and domainname) or (domainname and com) randomly and the resulting password reset link to customer looks like (https://subdomaindomainname.com/password_reset/token/?some_other_params) which is a wrong link. This issue is happening only on production and occur very random.
I verified our variable which have domain name in our source code and verified the code which is generating the url and also thoroughly tested this and their is no issue on our source code. On google i see this question. I did't understand how to programatically solve this on SendGrid email client.
mailer.rb
class Mailer < ActionMailer::Base
sendgrid_category Rails.env
default_url_options[:host] = APP_URL
end
config/environments/production.rb
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => "smtp.sendgrid.net",
:user_name => ENV['SENDGRID_USERNAME'],
:password => ENV['SENDGRID_PASSWORD'],
:domain => "subdomain.domainname.com",
:port => 587,
:authentication => "plain",
:enable_starttls_auto => true
}
APP_URL = "subdomain.domainname.com"
mailer_helper.rb
def link_to_with_ga(*args)
args[1] = append_ga args[1], 'html'
link_to *args
end
def append_ga(link, utm_content)
link << "#{link.include?('?') ? '&' : '?' }#{#ga_tag}&utm_content=#{utm_content}"
end
_reset_password_view.rb
<%= link_to_with_ga(t("users.reset_password"), reset_password_url(:token => #token, :locale=>#user_locale,:protocol => ( Rails.env.eql?('development') ? 'http' : 'https' )),:id => 'reset_link') %>
Please help me in solve this issue.
Thank you

Is it possible to send emails from the current user email?

I'm developing an internal operations manager for a company, They need to send automatic emails to their costumers, but the email has to be sent by the email address of the company operator that is taking care of that specific costumer.
So basically, I need to configure my mailer to work with N different emails (I have access to their emails passwords too).
config.action_mailer.smtp_settings = {
:address => 'smtp.office365.com',
:port => '587',
:authentication => :login,
:user_name => ENV['SMTP_USERNAME'],
:password => ENV['SMTP_PASSWORD'],
:domain => 'interworldfreight.com',
:enable_starttls_auto => true
}
I've seen that it is possible to send the email address to the mailer config this way:
def new_mail
mail from: "example#example.com", to: "user#example.com"
end
But what about the password?, is this even possible through only rails or should I consider other tool like MailGun ?
Thanks a lot for taking a moment to read this.
Apparently you can send a delivery_options object with the username, host and password.
class FclExwOperationMailer < ApplicationMailer
def info_request (representative, shipper)
#representative = representative
#shipper = shipper
delivery_options = { user_name: representative.smtp_user,
password: representative.smtp_password,
address: representative.smtp_host }
mail(to: shipper.email,
subject: "Please see the Terms and Conditions attached",
delivery_method_options: delivery_options)
end
end

Rails - using multiple email providers in single app

Anyway I can use multiple email providers within the same Rails 3 app ?
Context
1. Im using postmark for sending out mails currently (using delayed job)
2. Our app also needs to send out some mass emails - for which we will be using a separate provider.
Now I dont want to separate out and create a new app for the mass emailing part. How can I use/choose different email providers at the point of sending email ?
Thanks in advance
You can override ActionMailer settings on a per mailer basis, for example
class BulkMailer < ActionMailer::Base
self.smtp_settings = {...}
end
will cause BulkMailer and its subclasses to use those settings.
The one thing to be wary of is not to change smtp_settings in place, i.e. do not do something like self.smtp_settings[:user_name] = 'blah' as this would be acting on the shared settings rather than creating new settings private to BulkMailer
I'm using mailserver fallback in my application, so when one mail server is down it switches mailserver. Your problem is similar, except you don't need to alias the old Mail::Message.deliver and use Mail::Message.mass_deliver for instance.
This is how you do it:
Mail::Message.class_eval do
def mass_deliver
self.delivery_method.settings = {
:address => "smtp.massdeliverserver.com",
:port => 587,
:domain => 'yourdomain.com',
:user_name => 'mass-email#quadnode.com',
:password => 'yourpassword',
:authentication => 'plain',
:enable_starttls_auto => true
}
deliver
end
end
Then you could use YourMailer.your_method.deliver to use defalt settings you provided in environment.rb for config.action_mailer.smtp_settings and YourMailer.your_method.mass_deliver to use the other server settings.
Put the code inside some file in config/initializers and mass_deliver method will be available for any Mail::Message instance in your application.
You have a mass email list to which you need to send out from say mass-email#email.com and some other emails for other purpose from otheremail#email.com
You need to do these steps if i am getting the question correct ::
Remove the default from default :from if you have written it.
Create an action mailer for mass-email and put up the :from => "mass-email#email.com"
Go to your environment.rb file and fill up the details like this
config.action_mailer.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => 'yourdomain.com',
:user_name => 'mass-email#quadnode.com',
:password => 'yourpassword',
:authentication => 'plain',
:enable_starttls_auto => true
}
You can create it for as many files as you wish.
Hope it helps.

rails 3: how can my app send from multiple email accounts?

In my environment.rb file I have:
ActionMailer::Base.smtp_settings = {
:address => "smtp.example_host.com",
:port => '25',
:domain => "example_send_from.com",
:authentication => :plain,
:user_name => "send_account_name",
:password => ENV['MY_EMAIL_SEND_PWD']
}
It works fine.
However, now my app has a new class of emails that need to be sent via a different email account... perhaps gmail in some low-volume cases, sendgrid in other high volume cases.
I'm sure it's pretty simple -- but how do I override the default Base.smtp_settings setting on an email-by-email basis?
do you have separate actionmailers for each email account? then you could set the smtp settings per action mailer-class:
class Mailer1 < ....
self.smtp_settings = { .... }
end
etc.

How to setup Google Apps with ActionMailer with Rails 2.3.5?

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)

Resources