preloader
軟體工程

Let Devise gem to send mail for changing password and registering | 讓Devise在Rails專案上線環境具備寄信的能力

Instructions for installing and configuring sendmail software with action_mailer of rails in Ubuntu. It might be some complicated. 在 Ubuntu 安裝與設定 sendmail 可能會令人覺得複雜,以下是安裝與設定指引:


Below statements are for development machines. 以下屬於開發環境機器的設定:

Check below option values at config/environements/production.rb file in your rails project, you need to add or modify them if there is no value from them. 在 config/environments/production.rb 中檢查以下選項值,若沒有請新增:

config.action_mailer.delivery_method = :sendmail
config.action_mailer.perform_deliveries = true
config.action_mailer.default_url_options = { host: your.mailer.address }
config.action_mailer.raise_delivery_errors = true
config.action_mailer.default_options = { from: account-you-want@your.mailer.address }

 

Check below option values at config/initializers/devise.rb file in your rails project, you need to add or modify them if there is no value from them. 在 config/initializers/devise.rb 檢查以下選項值,若沒有請新增:

config.mailer_sender = account-you-want-it-doesnot-need-to-same-with-above@your.mailer.address
config.mailer = 'Devise::Mailer'
config.allow_unconfirmed_access_for = 2.days # For example, 2.days
config.confirm_within = 3.days # For example, 3.days
config.reconfirmable = true
config.remember_for = 2.weeks
config.reset_password_keys = [:email]
config.reset_password_within = 6.hours

 

If you have generated ‘User’ model through Devise gem, content of the file app/models/user.rb might same as follows: 假設你已使用Devise產生User model,內容可能如下:

devise :database_authenticatable, :registerable , :rememberable, :recoverable, :reconfirmable

 

and please add :confirmable statement into file app/models/user.rb. The content might same as follows: ,請在 app/models/user.rb 新增 confirmable,如下:

devise devise :database_authenticatable, :registerable, :rememberable, :recoverable, :reconfirmable  
, :confirmable, 

Please enter following command under the directory of your rails app at terminal program such as iTerm2. 請在開發機終端機程式iTerm2內,Rails專案底下,輸入以下指令:

rails generate migration AddConfirmableToDB

And add below contents into file db/migrate/xxxx...add_confirmable_to_db.rb. 並在 db/migrate/xxxx..add_confirmable_to_db.rb 檔案內,輸入以下內容:

add_column :users, :confirmation_token, :string
add_column :users, :confirmed_at, :datetime
add_column :users, :confirmation_sent_at, :datetime
add_column :users, :unconfirmed_email, :string
add_index :users, :confirmation_token, unique: true
execute("UPDATE users SET confirmed_at = NOW()”)

I cite wiki pages of Devise gem to this paragraph. After you finishing above instructions, save the file and exit text editor. 本段引用Devise gem的wiki內容。存檔離開檔案。

Then, you should type this command at terminal for finishing migrations of database. 接著在同個終端機程式內,輸入以下指令以完成資料庫的結構變更:

rake db:migrate

 

——-
This section describes how to install and configure sendmail for production environment. 以下是上線環境安裝sendmail:

Please login production machine through root account or same privileged as root. 請以root帳號,或是同等權限登入上線環境。

Please install sendmail. Command as follows: 請安裝sendmail,指令如下:

apt-get install sendmail

 

Please change current directory to /etc/mail . 請切換到目錄 /etc/mail 之下。

Please adjust content of sendmail.mc, local-host-names and aliases. 請調整 sendmail.mc, local-host-names 和 aliases 檔案內容。

Adjust contents of sendmail.mc as follows(refer from linux.vbirds): 改變 sendmail.mc 的內容(參考鳥哥的教學網頁):

  • Change from _DAEMON_OPTIONS('Family=inet, Name=MTA-v4, Port=smtp, Addr=127.0.0.1')dnl to DAEMON_OPTIONS('Family=inet, Name=MTA-v4, Port=smtp, Addr=0.0.0.0')dnl
  • Define ALIAS_FILE:define('ALIAS_FILE',/etc/aliases')dnl
  • Undefine UUCP_RELAY:undefine('UUCP_RELAY')dnl
  • Use feature access_db:FEATURE('access_db')dnl

The following two statements must exist in sendmail.mc: 這兩行必須存在於 sendmail.mc 裡面:

  • MAILER('local')dnl
  • MAILER('smtp')dnl

 

Adjust content of sendmail.conf: 改變 sendmail.conf 的內容:

  • Change statement QUEUE_INTERVAL=“10m”;  to  QUEUE_INTERVAL="1h"; . This is used for changing period of resending mail from 10 minutes to 1 hour. It’s used for personal preference. 這是為了讓再次寄信時間從10分鐘變成1小時(個人偏好)

Adjust content of local-host-names: 改變 local-host-names 的內容:

  • your.domain.com # Change to your domain name. 調整成你要寄送機器的網域名

 

There is no need to change file content of access and access.db. access 和 access.db 不用變更內容。

 

Edit content of /etc/aliases for forwarding mails from this remote machine to other mail addresses you prefer. 編輯 /etc/aliases 檔案內容, 讓你能夠將遠端機器收到的信寄到其他你常用的信箱:

root:<TAB>someoneuseratthismachine
someoneuseratthismachine:<TAB>[email protected]

 

Edit content of /etc/hosts for shortening waiting period of resolving its domain name by itself . 編輯 /etc/hosts 檔案內容,讓遠端機器能夠因為自己正確解析自身域名而減少等待時間即能寄出信件:

your.ip.address.ofthismachine<WHITESPACE> your.domain.name # Add this statment below others of 127.0.0.1 or 127.0.0.0 .&nbsp;新增檔案內容在127.0.0.1/127.0.0.0的下一行。

 

Edit content of /etc/hostnames for enforcing domain name of remote machine by itself. 編輯 /etc/hostname 檔案內容,讓遠端機器強制指定自身的域名而減少等待時間即能寄出信件:

your.domain.name

 

Reboot your remote machine. 重開遠端的機器。

After finishing rebooting, enter the command at remote machine as root: 遠端機器重開機完成之後,以 root 帳戶或同等權限輸入以下指令:

  • sendmailconfig
    • Press ‘N’ for sendmail.mc and sendmail.conf 如果遇到詢問是否變更 sendmail.mc 和 sendmail.conf ,請選擇否。
    • Press ‘Y’ for other files different from sendmail.mc and sendmail.conf 如果遇到詢問是否變更不同於 sendmail.mc 和 sendmail.conf 以外的所有檔案,請選擇是。
  • service apache2 restart
  • service sendmail restart

 

Reference: 

1. https://gist.github.com/adamstac/7462202

  1. Sendmail configuration http://hiredavidbank.com/prac-send.html

  2. Sendmail installation on Ubuntu box http://www.seanbehan.com/using-sendmail-to-send-mail-on-ubuntu-box

  3. 鳥哥 sendmail 安裝與設定 http://linux.vbird.org/linux_server/0380sendmail.php