PerlとRubyによるメール送信

Ruby

半手動かな。 seq は本文。

require "kconv"
require 'base64'
require 'net/smtp'

def mime_encodings(x)
  a = Base64.encode64(x.tojis)
  s = a.split ' '
  ss = []
  s.each {|a|
    ss[ss.length] = '=?iso-2022-jp?B?' + a + '?='
  }
  a = ss.join ' '
  a
end

def sendmail(from,to,subject,seq)
  if from != nil &&
      to != nil &&
      subject != nil &&
      seq != nil &&
      from =~ /\@/ &&
      to =~ /\@/
    xsubject = mime_encodings(subject)
    xseq = seq.tojis
    Net::SMTP.start("localhost",25){|smtp|
      smtp.send_mail <<EOF, from,to
X-Mailer: FortuneMailer/0.2a by hachikun
Content-Type: text/plain; charset=iso-2022-jp
From: #{from}
To: #{to}
Subject: #{xsubject}

#{xseq}

EOF
    }
  end
end

Perl

MIME::Lite 他を使った送信。X-Mailerの内容は制御できない。

#!/usr/bin/perl

use Jcode;
use MIME::Lite;
use POSIX 'strftime';

$now = strftime "%c %Z", localtime;
$mime = MIME::Lite->new(
  From => $mail,
  To   => $mailaddr,
  Subject => jcode($subject)->mime_encode,
  Type => 'text/plain; charset=iso-2022-jp',
  Encoding => '8bit',
  Data => jcode($seq)->h2z->jis,
  Datestamp => 0,
  Date => $now,
);
$mime->send();