Rails::Reply via Email::Using Postmark

Sending emails from rails is an easy process. But sometimes we need to reply to the email itself that was sent by the app.

Postmark is the easiest and most reliable way to be sure your important transactional emails get to the inbox. Postmark allows you to send your application’s emails with high delivery rates, including bounce/spam processing and detailed statistics. In addition, Postmark can parse incoming emails which are forwarded back to your application.

The process of replying via email is known to be inbound.

How to implement Postmark in rails?

  1. Add add-on postmark in your Heroku or  sign up using https://account.postmarkapp.com ( If you add postmark as an add-on in Heroku you do not need to sign up additionally).
  2. Create your server by adding the name of the server.
  3. You can create a signature or even skip this step.
  4. Since we are implementing inbound process, we will need our server’s inbound email address which can be found on the “Process inbound email” tab.                                         Screenshot from 2016-06-29 21:47:08.png
  5. Now you need to set `reply_to` (when sending email) with the given inbound email address (see example below).
  6. And at last you need to give the callback url. Postmark post JSON data each time an inbound event occurs (see screenshot below). In this callback portion, one can perform any necessary actions needed.

Example:

mail( to: <receiver's email>,
 subject: <subject>,
 reply_to: <server's inbound email address>)

Screenshot:

Screenshot from 2016-06-29 22:02:17

Postmark also keeps tracks of the emails whether it is inbound or outbound. So you can check whether the email are being successfully processed or not.

NOTE::

If we need to send some important information that is required when the receiver replies the email s/he received, then we can add that information after a `+` sign in the `server’s inbound email address`.

Example:

3e5317xxxx48ce3bxxxx1a7be74f665f+message78@inbound.postmarkapp.com

Let’s say I need the `message_id` . Hence, I will add `+message<MESSAGEID>` or just `+<MESSAGEID>` to the email address. This additional details will appear in the JSON data under `MailboxHash` in the callback url we set in the server.

References:

http://developer.postmarkapp.com/

https://devcenter.heroku.com/articles/postmark

https://github.com/wildbit/postmark-gem

 

Rails::Setting current time zone via cookies

Rails makes handling timezone easy. It stores times in UTC to the database and handles conversion to the current timezone that `Time.zone` is set to.

We can easily change the global timezone setting in `config/application.rb` by changing `config.time_zone`. But when we have different users in different timezones, we need to change the timezone, but on each request, for each user. We can handle this in an before_filter hook in the controller.

The Javascript `Date` object has a  `getTimezoneOffset()` method which returns an integer timezone offset.

In js file

$(document).ready(function(){
  if(!($.cookie('time_zone'))) {
    current_time = new Date();
    $.cookie('time_zone', current_time.getTimezoneOffset(), { path: '/', expires: days } );
   }
});

Here,` days` represent how long you want the cookies to be stored. And the `path` determines the value of the path attribute of the cookie.

Then in application_controller in Rails app, we can use `around_filter` to set the timezone.

around_filter :set_time_zone
private
 def set_time_zone
   time_zone_offset = cookies[:time_zone].to_i
   Time.zone = ActiveSupport::TimeZone[-time_zone_offset.minutes]
 end

What is Ruby Gem?

Ruby Gem is basically a package manager for ruby programming language. It provides a standard format for distributing libraries.

The `gem` command

The `gem` command is basically used to build, upload, download and install Gem packages.

When creating gems you need to make sure that the name of the gem is in lowercase. If there are many words then separate it with underscore’_’.

Gem can be anything. It could be some helper methods, or huge chunk of functionalities.

Gem can be created in multiple ways. One of the ways is ‘Using Bundler’.

Bundler

Firstly, Bundler is a gem itself; you can install it by running

$ gem install bundler

How to create a Gem?

$ bundle gem name_of_gem
Creating gem 'name_of_gem'...
MIT License enabled in config
Code of conduct enabled in config
create name_of_gem/Gemfile
create name_of_gem/.gitignore
create name_of_gem/lib/name_of_gem.rb
create name_of_gem/lib/name_of_gem/version.rb
create name_of_gem/name_of_gem.gemspec
create name_of_gem/Rakefile
create name_of_gem/README.md
create name_of_gem/bin/console
create name_of_gem/bin/setup
create name_of_gem/.travis.yml
create name_of_gem/.rspec
create name_of_gem/spec/spec_helper.rb
create name_of_gem/spec/name_of_gem_spec.rb
create name_of_gem/LICENSE.txt
create name_of_gem/CODE_OF_CONDUCT.md

You will have git initialized, readme, license, code of conduct and the most important is .gemspec file.

The name_of_gem.gemspec file consists of:

# coding: utf-8
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'name_of_gem/version'
Gem::Specification.new do |spec|
    spec.name = "name_of_gem"
    spec.version = NameOfGem::VERSION
    spec.authors = ["NEHA SUWAl"]
    spec.email = ["my@email.com"]
    spec.summary = %q{Name Of Gem}
    spec.description = %q{Name Of Gem}
    spec.homepage = ""
    spec.license = "MIT"
    spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(t    est|spec|features)/}) }
    spec.bindir = "exe"
    spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
    spec.require_paths = ["lib"]
    spec.add_development_dependency "bundler", "~> 1.11"
    spec.add_development_dependency "rake", "~> 10.0"
    spec.add_development_dependency "rspec", "~> 3.0"
end

 

Your made code goes into lib/name_of_gem.rb. You can also specify versions of your gem(which is situated in lib/name_of_gem/version.rb

To install the gem, you need to write a line of command in the terminal.

$ rake -T

It gives the list of rake task that you can run

rake build # Build name_of_gem-1.0.1.gem into the pkg directory
rake clean # Remove any temporary products
rake clobber # Remove any generated files
rake install # Build and install name_of_gem-1.0.1.gem into system gems
rake install:local # Build and install name_of_gem-1.0.1.gem into system gems without network access
rake release[remote] # Create tag v1.0.1 and build and push name_of_gem-1.0.1.gem to Rubygems
rake spec # Run RSpec code examples

To install you gem, you need to build first. When you build, .gem file will be added.

$ rake build
name_of_gem 1.0.1 built to pkg/name_of_gem-1.0.1.gem.

It consists of all the information about your gem. When you open the newly created file in your text editor, you will see that it consits of numbers only.

After building, you need to install and then release your gem.

$ rake install
name_of_gem 1.0.1 built to pkg/name_of_gem-1.0.1.gem.
gem name_of_gem (1.0.1) installed.

To release your gem you need to

  • Have an account in https://rubygems.org/
  • Sign in
  • $ gem push pkg/name_of_gem-1.0.1.gem

Your gem will be published. Now you can search your gem https://rubygems.org/

To install and use the gem

# Add to your gem file
gem 'name_of_gem'
OR
$ gem install name_of_gem

 

 

 

Simple form

Simple Form allows to get any kind of data of the form. Mainly it uses two API tokens, form_api_token and api_token. The form_api_token is a public token. And api_token which is a private token and only visible to the owner. This token is used while retrieving the data of the form.

To implement it in your app, first you need to visit https://getsimpleform.com/ and enter the email where you want your data to be sent.

A flash notice displays an e-mail has been sent to you. You will receive an e-mail from GetSimpleForm which will be have the form API token.This token is needed later so you need copy and save it.

For implementing in your code, in the form part, add an action inside form tag and in the action add the full url with the token that you copied earlier.

And your are done!

When you submit any data through the form, you will receive an email with the data that you entered in the form. This has a special feature of redirecting after submit. By default you don’t have to put any redirect value. It will redirect you to the same form’s page. If you want to redirect your user to another page after submitting the form, you can add a redirect url.

Simpleform also allows you to format the messages you receive when a user submits the form in your website.For this there is a button for form setting in their website, where you can modify as per your requirement.

You can also use HTML tags to format the body of the email so that it is more visually appealing.

For more details :: Presentation link of Simple Form