# = TITLE: # # Email DSL # # = COPYING: # # Copyright (c) 2007 Psi T Corp. # # This file is part of the ProUtils' Ratch program. # # Ratch is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # Ratch is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with Ratch. If not, see . begin require 'facets/net/smtp_tls' rescue LoadError require 'net/smtp' end module Ratch module Dsl module Email # Email function to easily send out an email. # # Settings: # # subject Subject of email message. # from Message FROM address [email]. # to Email address to send announcemnt. # server Email server to route message. # port Email server's port. # domain Email server's domain name. # account Email account name if needed. # password Password for login.. # login Login type: plain, cram_md5 or login [plain]. # secure Uses TLS security, true or false? [false] # message Mesage to send -or- # file File that contains message. def email(message, settings) settings ||= {} settings.rekey server = settings[:server] account = settings[:account] || ENV['EMAIL_ACCOUNT'] passwd = settings[:password] || ENV['EMAIL_PASSWORD'] login = settings[:login].to_sym subject = settings[:subject] mail_to = settings[:to] || settings[:mail_to] mail_from = settings[:from] || settings[:mail_from] secure = settings[:secure] domain = settings[:domain] || server port ||= (secure ? 465 : 25) account ||= mail_from login ||= :plain #mail_to = nil if mail_to.empty? raise ArgumentError, "missing email field -- server" unless server raise ArgumentError, "missing email field -- account" unless account raise ArgumentError, "missing email field -- subject" unless subject raise ArgumentError, "missing email field -- to" unless mail_to raise ArgumentError, "missing email field -- from" unless mail_from passwd ||= password(account) mail_to = [mail_to].flatten.compact msg = "" msg << "From: #{mail_from}\n" msg << "To: #{mail_to.join(';')}\n" msg << "Subject: #{subject}\n" msg << "" msg << message begin Net::SMTP.enable_tls if Net::SMTP.respond_to?(:enable_tls) and secure Net::SMTP.start(server, port, domain, account, passwd, login) do |s| s.send_message( msg, mail_from, mail_to ) end puts "Email sent successfully to #{mail_to.join(';')}." return true rescue => e if trace? raise e else abort "Email delivery failed." end end end end end end