How to use Rails mailer settings when creating a custom Mail? - ruby-on-rails

I'm creating a Mail object in a Rails app and want it to pick the mailer settings:
original = UserMailer.new_registration
original.deliver# Does the job
custom = Mail.new(original.to_s)
custom.deliver # Fails: OpenSSL::SSL::SSLError: hostname does not match the server certificate
Apparently the custom Mail object isn't picking up the Rails settings.

Looking at the code, we can pick up the config from the mailer the following way:
custom = ::Mail.new(raw_email)
key = Rails.application.config.action_mailer.delivery_method
delivery_method = ActionMailer::Base.delivery_methods.fetch(key)
delivery_settings = ActionMailer::Base.send("#{key}_settings")
custom.delivery_method(delivery_method, delivery_settings)
custom.deliver

To send custom mails using rails please read this.
http://mdushyanth.wordpress.com/2011/08/06/custom-mail-delivery-method-in-rails-3/

Related

integrating sms-easy to my mailer

i already know the mailer function in the rails and succeed in sending email to my customer. now im interested in the smseasy gem. i have been looking in the internet on how to setup it but i cant find any tutorial. i did read the readme in the https://github.com/preston/sms-easy but i didnt understand it.
# Override the default "from" address with config/initializers/sms-easy.rb
SMSEasy::Client.config['from_address'] = "noreply#example.com"
# Or, you can completely copy sms-easy.yml to your app (http://github.com/preston/sms-easy/blob/master/templates/sms-easy.yml), change it to your liking, and override the default configuration with:
SMSEasy::Client.configure(YAML.load(...))
# Your apps existing ActionMailer configuration will be used. :)
# Create the client
easy = SMSEasy::Client.new
# Deliver a simple message.
easy.deliver("5551234567", "verizon", "Hey!")
where does i wrote this code.
if any of you can help to teach me how to setup it. it would be nice.
The following code belongs in one of your controller actions that you want to trigger sending out the SMS:
# Create the client
easy = SMSEasy::Client.new
# Deliver a simple message.
easy.deliver("5551234567", "verizon", "Hey!")
under config/initializers directory you can create a file called sms-easy.rb and put the following line of code to set a from field which is supposed to override the default that you've set up with your mailer settings but it doesn't work for me currently, if i figure it out I can update the answer:
# Override the default "from" address with config/initializers/sms-easy.rb
SMSEasy::Client.config['from_address'] = "noreply#example.com"

Send email from my domain using SendGrid on Heroku

I have a Rails 4 app which is being hosted by Heroku on my_domain.com. I'm using free SendGrid to send emails, but no matter what settings I use all the emails are sent from my_app#heroku.com. How can set the settings so that emails are sent from automated#my_domain.com?
Thanks.
Try control-shift-F (which will pull up a search for your entire codebase). Search for "my_app#heroku.com" and you may be able to find where this variable is being set. Let me know if this doesn't work and I will try something else.
Go to app/mailers/your_mailer.rb and add the following line under class YourMailer < ActionMailer::Base
default from: "automated#my_domain.com"
Set in your production.rb,
config.action_mailer.default_options = {from: 'no-reply#example.com'}

Dynamic domain name in Rails action mailer while using sidekiq

In my requirement ,there are different domain names for single project.I would like to send reset password link with a domain name where the user requested for the reset password.For that I have done the follows in application_controller.rb and it works well.
before_filter :set_mailer_host
def set_mailer_host
ActionMailer::Base.default_url_options[:host] = request.host_with_port
$url_host = request.host_with_port
end
Later,I have used sidekiq for delayed mailer and updated like follows on entire application
Notifier.delay.password_reset(user.id)
As the mailer was configured with sidekiq it was using default domain name provided in configuration only and it was not using the dynamic domain name provided from request on application_controller.rb
Any suggestion to get the dynamic domain on mailers running by sidekiq would be greatly appreciated.
Rails - 3.2.8
Ruby - 1.9.3
sidekiq - 2.13.1
Thanks!!!
One of the options would be to associate each user with a domain and use this information when sending the email in sidekiq.
You'll need to construct the url yourself rather than rely on AM to do it for you.

sending emails using templates in Mandrill

I've read through the docs and still am unclear on how to actually use these templates in mandrill.
Currently I have a rails app with the standard Rails mailers (located in: App > views > welcome_mailer > welcome_email.html.erb) being sent through the Mandrill SMTP setup. This is working fine.
Now, I have a template in Mandrill ready to go, now what?
How do I actually use this template, do I need to adjust the code on my app to make a different call, or do I need to do something on the mandrill dashboard to tell it to use the new template instead of the rails version being sent now.
How do I actually use this template?
Thank you in advance.
You can use mandrill_mailer gem, inherit your mailer from MandrillMailer::TemplateMailer and then send it as usual InvitationMailer.invite(invitation).deliver.
Without any gems :
To use mandrill template you first need to create one in your mandrill account and then in your mailer add a correct header which tells the name of the template. Then mandrill will by magic automatically call that template.
Example:
# app/mailers
class CardMailer < ActionMailer::Base
default from: "admin#domain.ch"
def welcome(card)
mail to: card.responsable.email,
from: "\"Andrey\" <admin#domain.ch>",
subject: 'Welcome in my website'
headers['X-MC-MergeVars'] = "{\"TYPE\":\"#{card.card_type.name}\"}" # variables
headers['X-MC-Template'] = "welcome" # template
headers['X-MC-AutoText'] = 1 # generate text version
headers['X-MC-InlineCSS'] = "true" # inline css
end
end
In my case, it uses my "welcome" template. Just use the name of your mandrill template.
As you can see, there are many other headers available. See the full-list here.
Note : even if you don't use rails template any more, you still need one in your view.

Devise and mailers - How to get current URL

When sending emails (for example for 'Reset password instructions') using Devise, i need to know current domain URL, and set that value into mailer's template.
request.url doesn't work for me.
Assuming, this Rails application is accessibly from multiple URLs.
Any ideas?
Request object is not available in the mailers. You will need to set up the host in the environment configuration file with something like:
ActionMailer::Base.default_url_options[:host] = 'myhost.com'

Resources