Class: Sendmail
Constant Summary collapse
- MAIL_FILE =
Pathname.new(',mail')
Instance Method Summary collapse
- #check_mail_options(options) ⇒ Object
- #parse_mail_options(*args) ⇒ Object
-
#sendmail(*args) ⇒ Object
Mail.
- #sign_wrap(opts, body) ⇒ Object
Instance Method Details
#check_mail_options(options) ⇒ Object
110 111 112 113 114 |
# File 'lib/sendmail.rb', line 110 def ( ) raise ArgumentError, 'No recipents' if .to.empty? raise ArgumentError, 'No mail server' if .server.nil? raise ArgumentError, 'No mail subject' if .header[:Subject].nil? end |
#parse_mail_options(*args) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/sendmail.rb', line 16 def ( *args ) = OpenStruct.new( :from => (defined? EMAIL)? EMAIL : ENV['EMAIL'], :from_name => (defined? FULLNAME)? FULLNAME : ENV['FULLNAME'], :to => [], :server => ENV['SMTPSERVER'] || 'localhost:25', :header => {}, :body => STDIN, :charset => 'us-ascii', :encoding => '7bit' ) string_args, hash_args = args.partition { |x| x.is_a?(String) } hash_args.each do |hash| raise ArgumentError, "Bad argument #{hash}" unless hash.is_a?(Hash) if hash[:header].is_a? String .header.merge! YAML.load(hash[:header]).symbolize_keys hash.delete :header end if hash[:subject] .header[:Subject] = hash[:subject] hash.delete :subject end hash.each do |k,v| x = .send(k) case x when Hash then v.each { |kk, vv| x[kk.to_sym] = vv } when Array then x.concat v else .send("#{k}=", v) end end end OptionParser.new do |opts| opts.separator '' opts.on('-b', '--body FILE', 'Choose a file for the mail body') do |aFile| .body = File.open(aFile) end opts.on('-f', '--mail-from NAME <EMAIL>', 'Choose the From address') do |aString| .from = aString end opts.on('-t', '--mail-to NAME', 'Choose a recipient') do |aString| .to << aString end opts.on('-s', '--server NAME:PORT', 'Choose a mail server') do |aString| .server, .port = aString.split(/:/) .port ||= 25 end opts.on('-S', '--subject NAME', 'Choose your mail subject') do |aString| .header[:Subject] = aString.sub(/\.?$/, '.') end opts.on('--[no-]sign', 'Sign the message with gpg') do |signed| .signed = signed end opts.on('--passphrase FILE', 'the passphrase file') do |aFile| .pass = Pathname.new(aFile) end opts.on('-a', '--[no-]confirm', 'Ask a confirmation before sending') do |confirm| .confirm = confirm end opts.on('--comment STRING', 'Choose a comment for GPG') do |aComment| .comment = aComment end opts.on('--header STRING', 'Add some header fields (Yaml syntax)') do |s| .header.merge! YAML.load(s) end opts.on('-m', '--[no-]mime', 'Choose the MIME protocol') do |mime| .mime = mime end opts.on('--charset STRING', 'Choose a charset for the encoding') do |aCharset| .charset = aCharset end opts.on('--encoding STRING', 'Choose an encoding') do |anEncoding| .encoding = anEncoding end opts.on_tail('-h', '--help', 'Show this message') do puts opts exit end end.parse!(string_args) .header[:To] = (.header[:To] || .to).join(', ') from = .header[:From] || .from if from.to_s =~ /<(.*)>/ .header[:From] = from .from = $1 else .header[:From] = "#{.from_name} <#{from}>" .from = from end () end |
#sendmail(*args) ⇒ Object
Mail.
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/sendmail.rb', line 119 def sendmail ( *args ) body, opts = nil, nil if MAIL_FILE.exist? STDERR.puts "Using the cache file #{MAIL_FILE}" MAIL_FILE.open do |mail_file| opts = YAML::chop_header(mail_file) body = mail_file.read end else opts = (*args) body = (opts.body.nil?)? '' : opts.body.read end opts.header.symbolize_keys! server, port = opts.server.split(/:/) port ||= 25 to = opts.header[:To] to = to.join(', ') if to.is_a? Array STDERR.puts "To: #{to.inspect}" STDERR.puts "Smtp Server: #{server}" # # confirm the user # if opts.confirm raise 'HighLine is unavailable' unless defined? HighLine question = %Q[ |Send a mail, with this subject: #{opts.header[:Subject]} | to #{to} | from #{opts.header[:From]} | #{(opts.signed)? 'signed by ' + opts.from : 'not signed !'} |Are you sure? (y/n)].head_cut! raise 'Aborting' unless HighLine.new.agree question, true end require 'net/smtp' Net::SMTP.start(server, port) do |smtp| smtp.(opts.from, opts.to) do |f| f.print "From: #{opts.header[:From]}\n" f.print "To: #{to}\n" f.print "Subject: #{opts.header[:Subject]}\n" opts.header.each do |k, v| next if [:From, :To, :Subject].include? k k = $1 if k.to_s =~ /^"(.*)"$/ f.print "#{k}: #{v}\n" end if opts.signed TempPath.new do |mail_body| mail_body.open('w') do |out| if opts.mime out.puts " |Content-Type: text/plain; charset=\"#{opts.charset}\" |Content-Transfer-Encoding: #{opts.encoding} | |".head_cut! end out.print body end f.print sign_wrap(opts, mail_body) end else f.print "\n" + body end end end end |
#sign_wrap(opts, body) ⇒ Object
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 |
# File 'lib/sendmail.rb', line 193 def sign_wrap ( opts, body ) id = "===============Vcs#{new_id 10}==" if opts.mime TempPath.new do |tmp_name| tmp_name.open('w') do |tmp| if opts.mime tmp.print %Q[ |MIME-Version: 1.0 |Content-Type: multipart/signed; micalg=pgp-sha1; | protocol="application/pgp-signature"; | boundary="#{id}"; | charset="#{opts.charset}" |Content-Transfer-Encoding: #{opts.encoding} | |This is an OpenPGP/MIME signed message (RFC 2440 and 3156) |--#{id} |].head_cut! tmp.print body.read tmp.print %Q[ | |--#{id} |Content-Type: application/pgp-signature; name="signature.asc" |Content-Description: OpenPGP digital signature |Content-Disposition: attachment; filename="signature.asc" | |].head_cut! end TempPath.new do |tmp_out| pid = fork do cmd = %w[ gpg --status-fd 2 --sign --textmode --armor --digest-algo sha1 --no-use-agent -u ] cmd << "<#{opts.from}>" cmd << ((opts.mime)? '--detach-sign' : '--clearsign') cmd += [ '--output', tmp_out ] if opts.pass cmd += [ '--no-tty', '--batch', '--passphrase-fd', opts.pass.open.to_i.to_s ] end cmd += [ '--comment', "'#{opts.comment}'" ] if opts.comment STDIN.reopen(body.open) exec(*cmd) end Process.waitpid pid tmp.print tmp_out.read tmp.puts "\n--#{id}--" if opts.mime end end return tmp_name.read end end |