Why Mail Engine
- Don't need to export and import your system data to any other mail systems(like mailchimp, mad_mimi).
- Allow user to edit all email templates directly in system admin, save the time of developer to participate in.
- Allow user to add mail sending schedule directly in system admin, don't need developer to add them into code or deploy crontab at server each time when client have new request.
- Fully take advantage of the convenience of loading any system data to mail template. In the past, like using mailchimp, you have to store everything into user list, then you are able to load that into mail template, it increased the complexity of maintaining information in another system.
- If you want to trace your mail, like open rate, click rate and unsubscribe rate, we provide Sendgrid integration configuration to take advantage that system. (More information at http://sendgrid.com)
- Allow logging all sent mails, you can check which mail been sent to which user.
- You can know send the email to the active users, so that you don't need to waste your credits of sendgrid or amazon.
Problem when using third party mail system
We are using mailchimp mostly for the newsletter solution, but it's has many problems that we have to make a new mail engine to fit our needs in real project:
- It is only one newsletter or marketing mail solution, so for system mail, it can do nothing, so for system mail we still go the original way (put them in the rails project.)
- Mailchimp don't support other encode for mail, when sending japanese or chinese mail it will be failed.
- It doesn't support localization, so different language is different campaign, you need to set a lot config for each of them.
- Manage user list is awkward, it even can't create list by API, so you have to create list one by one by hand.
- It can't load other user personal or customized data from system into mail, like latest unchecked news or messages, it's very limited to load info from user list record.
- It doesn't support reusable partials, which like you hope to put a fixed content of footer or header.
- It doesn't support recurring mail sending.
Serveral places you need developer's help without Mail Engine:
- Change text in system email.
- Localize mail templates in system.
- Prepare user list for marketing mail if you want to use mailchimp.
- Analyse system mail send log.
- Add multipart for email, like text version email or html version email.
- Find untranslated mail templates.
Installation
** Step 1: **
gem install mail_engine
** Step 2: **
Add management page needed javascript lib and css files, migration files and mail_engine_config.yml under config directory.
rails g mail_engine:install
** Step 3: **
Run migration.
rake db:migrate
** Step 4: **
Add acts_as_mail_receiver definition to "Your User" model.
class User < AR
acts_as_mail_receiver :payload_columns => %w{firstname lastname},
:groups => %w{all english_users chinese_users}
end
- User model must have 'email' and payload specified columns, or else you can delegate to other model.
- "acts_as_mail_receiver" statement has to place below the scope statements.
** Step 5: **
Add sendgrid smtp api configure:
class UserMailer < ActionMailer::Base
sendgrid_header do
category "xxx"
filters {
opentrack "enable" => 1
clicktrack "enable" => 1
subscriptiontrack "enable" => 0
template "enable" => 0
footer "enable" => 0
}
end
end
For the detail information please check sendgrid.com.
** Step 6: **
Add below line to production.rb or development.rb for specifing the host. it will used when generate resource url when upload zip file.
config.action_mailer.default_url_options = { :host => "lvh.me:3000" }
** Step 7: **
Add below line to the crontab list of your sever:
### check mail schedule for every 15 minutes ###
*/4 * * * * sh -c "cd /path/to/your/system && rake mail_engine:sendmail RAILS_ENV=production"
Configuration
Below is the config for development env.
development:
log_mail: false # if you want to enable the mail log, set true.
user_class_name: "User" # Specify the User model in your system, which will used for mail schedule feature to find user groups and payloads.
user_locale_column: "locale" # Specify the locale column of user table, this will help sending correct language of mail to user.
mount_at: "/admin/mail_engine" # set the url path for mail engine.
access_check_method: "logged_in?" # set a method name which will execute before each page action in mail engine.
intercept_email: "dev@youdomain.com" # if you want to send all mail to one email, like on staging server or development environment, you can set this, or else don't set it.
sendgrid: # Below you need to set the sendgrid account info.
sendgrid_user: "you send grid username" #
sendgrid_key: "password" #
sendgrid_category: "my sendgrid category" #
Usage
** For System Mail: **
Due to the sender of system mail is ActionMailer, so you just need to set template physical store path (e.g. user_mailer/notify) as the mailer template load path, system will load it from database. (Notice, database template will override your file template.)
Mail Engine use Liquid template engine, so you can read the Liquid Reference at http://www.liquidmarkup.org/
Steps:
- Create a template in mail engine.
** For Marketing Mail: **
For normal marketing mails are quite simple in logic, so don't need logic code in the mail template. So we provide a mail dispatcher which allows user can send marketing mail only by providing the template and select the data pre-defined in system to the mail template, don't need developer to do anything like adding mailer file.
Steps:
- Create a mail tempate in mail engine.
- Create a sending schedule record. (You can specify the users to receive this mail, total sending count, sending period, when start to deliver the first mail and load which data into this template, You also can set condition for the scheduler, like if no specified data in collection, don't send the mail.)
You also can send mail in your program by using: MailEngine::MailDispatcher.{your marketing mail template name}(:to => "Michael he hlxwell@gmail.com", :values => {:username => "Michael He"}).deliver
Notes
Currently only support above rails3.
Tips
-
Use below rake task, you can export local mail engine data, then import to production environment.
rake mail_engine:export_mail_engine_database
-
You can setup postfix or sendmail MTA on the server then use it to queue the mail locally.
http://support.sendgrid.com/entries/142079-getting-started-configuring-postfix
then you can set delivery_method as:
config.action_mailer.delivery_method = :sendmail
Sendgrid Whitelabel Wizard
http://wiki.sendgrid.com/doku.php?id=whitelabel
http://support.sendgrid.com/forums/142139-faq-whitelabel-and-dns
Screenshots
Dashboard
Shows the latest report.
Template Management
You can manage the template used in your system, including:
- System mail, has it's own action mailer file.
- Marketing mail, simple logic and without it's own action mailer.
- Partials, you can reused it in any other templates.
Template editing Page
You can see here are many details you can change:
- Template Path, system action mailer can find it when lookup mail template.
- Locale
- Format
- Layout
- Files will used in the template.
- Zip File when you used at creating this template.
- Preview template in time.
Upload html template with image in a zip file
You can create a template by uplaoding a zip file including one html and other images or css files.
Partial and Layout selection
Mail Schedule
You can set a mail sending schedule, here are some useful setting items:
- Marketing Mail Template.
- User Group will be received this mail.
- Total send count.
- Already send count.
- Sending Period.
- Payloads which load from user model and send to template when sending.
- First send date time, this date will indicate when this mail will be send first time, and according to the seding period it will resend at the same time after one period.
- You can control if run the schedule or not.
- Users with different locale setting will receive different language of email. (if not found that language, you can set default template or not send.)
Mail Logs
It will log all mails' content been sent, you can turn it off in config file.
Report
Shows detail chart and data list of mail sending stats.