namespace :mail do namespace :server do task(:default) { list } desc <<-DESC List all VPS's acting as outgoing mail servers. DESC task :list do puts_title "Mail servers" puts_list MailServer.all, :id => false, :table => true do |t| t.col("Host", :host) t.col("Port", :port) end end desc <<-DESC Set up an outgoing (SMTP) mail server on a VPS. ATTENTION: Outgoing mail servers, like their name suggests, can only send e-mails out and cannot receive any. As a consequence, that they cannot be used as public Internet MX hosts (yet). After the setup has completed, you will be able to send out e-mails from the mail server VPS in addition to any VPS configured as mail client of that mail server (see #{qcommand "mail:client:setup"}). From these VPS's, e-mails can be sent with for example: * Out-of-the-box: - PHP's mail() function, - Rails mailers (ActionMailer subclasses), - the #{q "sendmail"} command, - any other way that eventually relies on sendmail. * Any method that relies on SMTP, after configuring SMTP connection details based on the output of #{qcommand 'mail:server:list'}. Environment variables: (optional) $ON: the hostname of the VPS to set up the mail server on. Default: the newest VPS. DESC task :setup do confirm <<-WARN Any previously installed MTA (Mail Transfer Agent) will be removed prior to configuring the new mail server. WARN vps = mail.fetch_target_vps mail_server = vps.build_mail_server try_save mail_server do puts_title "Request sent" puts "Mail server has been scheduled for setup on VPS #{q vps}." end end end namespace :client do desc <<-DESC Configure a mail client on a VPS. This will enable e-mail sending via a previously configured mail server on another VPS (see #{qcommand \ 'mail:server:setup'}. For example, given a VPS `foo' and a VPS `bar': $ #{command 'mail:server:setup'} ON=foo $ #{command 'mail:client:setup'} ON=bar Environment variables: (optional) $ON: the ID or hostname of the VPS to configure the mail client on. Default: the newest VPS. (optional) $OF or $SERVER: the ID or hostname of the mail server VPS that the client is to send e-mails through. Default: the mail server last set up. DESC task :setup do id = ENV["SERVER"] || ENV["OF"] unless server = id ? MailServer.find(id) : MailServer.last error! <<-MSG There is no mail server set up yet. To remedy this situation, execute the following command: $ #{command 'mail:server:setup'} MSG end server = server.vps client = mail.fetch_target_vps mail_client = client.build_mail_client(:server => server) try_save mail_client do puts_title "Request sent" puts_long <<-MSG Mail client of #{q server} successfully scheduled for setup on #{q client}. MSG end end end def fetch_target_vps ENV["ON"] ? VPS.find(ENV["ON"]) : VPS.last end end