SERVICE

各言語でのサンプルコード Ruby

スリーメール


Ruby版サンプル

/API/setを使用してメール配信の設定を行うサンプルです。

接続先ホスト名(example.com)および、接続時のIDとパスワード(login_id, password)につきましては適宜ご変更ください。

#!/usr/bin/env ruby
 
# 3mail API接続サンプルプログラム
#
# ruby 1.8.5 にて動作確認済み
# 別途以下のライブラリが必要です。
# * rubygems (http://rubyforge.org/projects/rubygems/)
# * httpclient (http://raa.ruby-lang.org/project/httpclient/)
#
# Author 3hands dev Team
# Copyright 3hands Inc.
 
require 'rubygems'
require 'httpclient'
require "rexml/document"
require "nkf"
 
boundary = "3mailboundary"
c = HTTPClient.new
open("sample.csv.gz") do |file|
  # 送信データの設定
  postdata = {
    'login_id'      => 'login_id',
    'password'      => 'login_password',
    "file"          => file,
    'subject'       => NKF.nkf('-W -s', 'テスト件名'),
    'body'          => NKF.nkf('-W -s', 'テスト本文'),
    'from_address'  => 'from@example.com',
    'start_sending' => 'now'
  }
 
  # APIへ接続
  retval = c.post_content("http://example.com/API/set", postdata,
                   "content-type" => "multipart/form-data, boundary=#{boundary}")
 
  # 結果(XML形式)を取得
  xml = REXML::Document.new retval
 
  # 内容を表示
  puts "status: "  + xml.elements['/response/status'].text
  puts "code: "    + xml.elements['/response/code'].text
  puts "message: " + xml.elements['/response/message'].text
end