#! /usr/local/bin/ruby # # vCard ファイルを添付したメールを作る # # % ruby vcardmail.rb foo@example.com person.vcf | mail foo@example.com # # Copyright (C) 2001 Satoru Takabayashi # All rights reserved. # This is free software with ABSOLUTELY NO WARRANTY. # # You can redistribute it and/or modify it under the terms of # the GNU General Public License version 2. # require 'kconv' require 'tmail' require 'net/smtp' def generate_filename (vcard) /^FN:(.*)/ =~ vcard name = $1 "=?ISO-2022-JP?B?" + (name + '.vcf').tojis.to_a.pack('m').chomp + "?=" end raise unless ARGV.length == 2 to = ARGV.shift vcard = File.open(ARGV.shift).readlines.join('') mail = TMail::Mail.new mail.to = to mail.from = 'satoru@namazu.org' mail.subject = "vcard" mail.date = Time.now mail.mime_version = '1.0' message = TMail::Mail.new message.set_content_type('text', 'plain') message.transfer_encoding = '7bit' message.body = "vcard is attached.\n" filename = generate_filename(vcard) encoded_vcard = [vcard].pack('m').chomp.gsub(/.{76}/, "\\1\n") attachment = TMail::Mail.new attachment.body = encoded_vcard attachment.transfer_encoding = 'base64' attachment.set_content_type('text', 'x-vcard', 'name' => filename) attachment.set_content_disposition('attachment', 'filename' => filename) mail.parts.push(message) mail.parts.push(attachment) mail.write_back puts mail.encoded