2008-03-11

GmailのSMTPサーバを利用した添付ファイルを送れるメールスクリプト

#!/usr/local/bin/ruby

require 'net/smtp'
require 'rubygems'
require 'tlsmail'
require 'base64'

class GSMTP

  def initialize(username, password)
    @smtp = 'smtp.gmail.com'
    @helo = 'gmail.com'
    @username = username
    @password = password
    @from_addr = username
    @boundary = 'myboundry'
  end

  def send(to_addr, subject, text, *filepath)
    body = Array.new
    body << "From: #{@from_addr}"
    body << "To: #{to_addr}"
    body << "Subject: #{subject}"
    body << "MIME-Version: 1.0"
    body << "Content-Type: multipart/mixed; boundary=\"#{@boundary}\""
    body << ""
    body << "--#{@boundary}"
    body << 'Content-Type: text/plain; charset="iso-2022-jp"'
    body << ""
    body << "#{text}"

    filepath.each do |path|
      body << "--#{@boundary}"
      body << "Content-Type: application/octet-stream"
      body << "Content-Transfer-Encoding: base64"
      body << "Content-Disposition: attachment; filename=\"#{File.basename(path)}\""
      body << ""
      begin
        body << Base64.encode64(File.read(File.expand_path(path)))
      rescue => encode_error
        puts encode_error
        return
      end
    end

    body << "--#{@boundary}--"

    gmail = Net::SMTP.new(@smtp, 25)
    gmail.enable_tls(OpenSSL::SSL::VERIFY_NONE)
    begin
      gmail.start(@helo, @username, @password, :login) do |smtp|
        smtp.send_mail(body.join("\n"), @from_addr, to_addr)
      end
    rescue => send_error
      puts send_error
      return
    end
  end
end

mail = GSMTP.new("account@gmail.com", "password")
mail.send('to-address@somehost.com', '件名', 'ここが本文。これはテキストのみメール')
mail.send('to-address@somehost.com', '件名', '本文。以降の添付ファイルも送る', './hoge.txt', './foo.jpg')
Posted at 00:49 in | WriteBacks (0) | Edit

2008-03-10

GmailのSMTPサーバを利用したメールスクリプト

既にたくさんの方が書いているのでスクリプトとしての意味はないですが、お勉強目的でやってみました。

#!/usr/local/bin/ruby -Ku

require 'net/smtp'
require 'rubygems'
require 'tlsmail'

class GSMTP
  def initialize(username, password)
    @smtp = 'smtp.gmail.com'
    @helo = 'gmai.com'
    @username = username
    @password = password
    @from_addr = username
  end

  def send(to_addr, subject, text)
    body = <<EOF
From: #{@from_addr}
To: #{to_addr}
Subject: #{subject}
Content-Type: text/plain; charset="UTF-8"

#{text}
EOF
    gmail = Net::SMTP.new(@smtp, 25)
    gmail.enable_tls(OpenSSL::SSL::VERIFY_NONE)
    gmail.start(@helo, @username, @password, :login) do |smtp|
      smtp.send_mail(body, @from_addr, to_addr)
    end
  end
end

mail = GSMTP.new("youraddress@gmail.com", "password")
mail.send('anyone@somehost.com', '件名', 'ここが本文。')
Posted at 00:29 in | WriteBacks (0) | Edit

2008-03-03

シェルの変数

って、typesetで宣言してあげればローカル変数なんですね。1000行くらいのシェルスクリプトを覗いてみたら、関数内の変数は全てtypesetがついてあったので、何か意味でもあるのかと思って調べてみて初めて知りました。

Posted at 21:57 in | WriteBacks (0) | Edit

2008-03-01

Vimから実行したコマンドの実行結果

「v:shell_error」には、Vimから実行したコマンドの戻り値が入るみたい。

ファイルタイプがRubyのとき、保存時に構文チェックをするVimスクリプト。構文誤りがあったときだけメッセージを出します。

autocmd BufWritePost * :call RubySyntaxCheck()
fun! RubySyntaxCheck()
  if &ft == "ruby"
    let syntax_check_result = system("/usr/bin/ruby -c " . expand("%"))
    if v:shell_error != 0
      echomsg syntax_check_result
    endif
    unlet syntax_check_result
  endif
endfun
Posted at 04:19 in | WriteBacks (0) | Edit

2008-02-27

* Vim: autocmdの使い方メモ

autocmd BufWritePost *.rb :!/usr/local/bin/ruby -c %
→バッファを保存するとき、ファイルの拡張子がrbだったら、保存と同時に構文チェックを実行。
autocmd FileType changelog :nmap L :call AddEntry()<cr>
→ファイルタイプがchangelogのときだけ、LでAddEntry関数を実行。
Posted at 02:13 in | WriteBacks (0) | Edit