| 1 | # Mail::SMI Perl Module | ||
| 2 | # | ||
| 3 | # If you are need some help, read documentation first. | ||
| 4 | # $ perldoc Mail::SMI | ||
| 5 | # | ||
| 6 | # $Id$ | ||
| 7 | # | ||
| 8 | package Mail::SMI; | ||
| 9 | |||
| 10 | use Socket qw(:DEFAULT :crlf); | ||
| 11 | use POSIX qw(strftime); | ||
| 12 | use strict; | ||
| 13 | use vars qw($sock $VERSION $DEBUG); | ||
| 14 | |||
| 15 | eval("use MIME::QuotedPrint"); | ||
| 16 | |||
| 17 | $DEBUG = 0; | ||
| 18 | $VERSION = '0.01'; | ||
| 19 | |||
| 20 | sub _gethostname | ||
| 21 | { | ||
| 22 | my $this= @_; | ||
| 23 | my ($port, $iaddr, $sockaddr, $hostname, $hostaddr); | ||
| 24 | |||
| 25 | $sockaddr = getpeername($sock); | ||
| 26 | ($port, $iaddr) = unpack_sockaddr_in($sockaddr); | ||
| 27 | $hostname = gethostbyaddr($iaddr, AF_INET); | ||
| 28 | return $hostname; | ||
| 29 | } | ||
| 30 | |||
| 31 | sub _getrcpt | ||
| 32 | { | ||
| 33 | my $this = shift; | ||
| 34 | return @{$this->{SMTP_RCPT}}; | ||
| 35 | } | ||
| 36 | |||
| 37 | sub connect | ||
| 38 | { | ||
| 39 | my($this,$host,$port,$hostname)=@_; | ||
| 40 | my ($iaddr, $paddr, $proto); | ||
| 41 | |||
| 42 | $this->{HOST} = $host; | ||
| 43 | $this->{PORT} = $port || 25; | ||
| 44 | |||
| 45 | $iaddr = inet_aton($this->{HOST}); | ||
| 46 | $paddr = sockaddr_in($port, $iaddr); | ||
| 47 | $proto = getprotobyname('tcp'); | ||
| 48 | |||
| 49 | unless(socket SOCKFH, AF_INET, SOCK_STREAM, $proto) | ||
| 50 | { | ||
| 51 | $this->{ERR} = "connect method failed: $!\n"; | ||
| 52 | return 0; | ||
| 53 | } | ||
| 54 | unless(connect SOCKFH, $paddr) | ||
| 55 | { | ||
| 56 | $this->{ERR} = "connect method failed: $!\n"; | ||
| 57 | return 0; | ||
| 58 | } | ||
| 59 | |||
| 60 | $sock = \*SOCKFH; | ||
| 61 | |||
| 62 | select($sock); $|=1; select(STDOUT); | ||
| 63 | |||
| 64 | my $resp = <$sock>; | ||
| 65 | if($resp !~ /^220/) | ||
| 66 | { | ||
| 67 | $this->{ERR} = "connect method failed: ($resp)"; | ||
| 68 | return 0; | ||
| 69 | } | ||
| 70 | |||
| 71 | $hostname = (gethostbyname('localhost'))[0] unless $hostname; | ||
| 72 | |||
| 73 | print $sock "HELO $hostname\015\012"; | ||
| 74 | $resp = <$sock>; | ||
| 75 | print $resp if $DEBUG; | ||
| 76 | if($resp !~ /^250/) | ||
| 77 | { | ||
| 78 | $this->{ERR} = "connect method failed: ($resp)\n"; | ||
| 79 | return 0; | ||
| 80 | } | ||
| 81 | |||
| 82 | return 1; | ||
| 83 | } | ||
| 84 | |||
| 85 | sub disconnect | ||
| 86 | { | ||
| 87 | my $resp; | ||
| 88 | |||
| 89 | print $sock "QUIT\015\012"; | ||
| 90 | $resp = <$sock>; | ||
| 91 | close($sock); | ||
| 92 | |||
| 93 | return 1; | ||
| 94 | } | ||
| 95 | |||
| 96 | sub new | ||
| 97 | { | ||
| 98 | my $this = | ||
| 99 | { | ||
| 100 | ERR => undef, | ||
| 101 | HOST => undef, | ||
| 102 | PORT => 0, | ||
| 103 | SMTP_FROM => undef, | ||
| 104 | SMTP_RCPT => [], | ||
| 105 | SMTP_DATA => undef, | ||
| 106 | }; | ||
| 107 | |||
| 108 | bless $this; | ||
| 109 | return $this; | ||
| 110 | } | ||
| 111 | |||
| 112 | sub reset | ||
| 113 | { | ||
| 114 | my $this = shift; | ||
| 115 | |||
| 116 | undef $this->{ERR}; | ||
| 117 | undef $this->{SMTP_FROM}; | ||
| 118 | undef $this->{SMTP_RCPT}; | ||
| 119 | undef $this->{SMTP_DATA}; | ||
| 120 | } | ||
| 121 | |||
| 122 | sub rfc822_date | ||
| 123 | { | ||
| 124 | return strftime "%a, %d %b %Y %H:%M:%S %z", localtime(time()); | ||
| 125 | } | ||
| 126 | |||
| 127 | sub sendmail | ||
| 128 | { | ||
| 129 | my ($this, $host, $port) = shift; | ||
| 130 | my ($resp, $c); | ||
| 131 | |||
| 132 | if($host && $port) { | ||
| 133 | return 0 unless $this->connect($host, $port); | ||
| 134 | } | ||
| 135 | |||
| 136 | if(!$this->{SMTP_FROM}) | ||
| 137 | { | ||
| 138 | $this->{ERR} = "sendmail method failed: missing SMTP_FROM\n"; | ||
| 139 | return 0; | ||
| 140 | } | ||
| 141 | |||
| 142 | print $sock "MAIL FROM: <$this->{SMTP_FROM}>\015\012"; | ||
| 143 | $resp = <$sock>; | ||
| 144 | if($resp !~ /^250/) { $this->{ERR} = "sendmail method failed (MAIL FROM)"; return 0; } | ||
| 145 | |||
| 146 | foreach my $rcpt ($this->_getrcpt()) | ||
| 147 | { | ||
| 148 | print $sock "RCPT TO: <$rcpt>\015\012"; | ||
| 149 | $resp = <$sock>; | ||
| 150 | unless($resp =~ /^250/) { $this->{ERR} = "sendmail method failed: ($resp)"; } | ||
| 151 | $c++; | ||
| 152 | } | ||
| 153 | |||
| 154 | if(!$c) | ||
| 155 | { | ||
| 156 | $this->{ERR} = "sendmail method failed: missing SMTP_RCPT\n"; | ||
| 157 | return 0; | ||
| 158 | } | ||
| 159 | |||
| 160 | print $sock "DATA\015\012"; | ||
| 161 | $resp = <$sock>; | ||
| 162 | print "smtp: $resp" if $DEBUG; | ||
| 163 | unless($resp =~ /^354/) | ||
| 164 | { | ||
| 165 | chomp $resp; | ||
| 166 | $this->{ERR} = "sendmail method failed: ($resp)"; | ||
| 167 | return 0; | ||
| 168 | } | ||
| 169 | |||
| 170 | print $sock $this->{SMTP_DATA}; | ||
| 171 | $resp = <$sock>; | ||
| 172 | print "smtp: $resp" if $DEBUG; | ||
| 173 | unless($resp =~ /^250/) | ||
| 174 | { | ||
| 175 | chomp $resp; | ||
| 176 | $this->{ERR} = "sendmail method failed: ($resp)"; | ||
| 177 | return 0; | ||
| 178 | } | ||
| 179 | |||
| 180 | if($host && $port) | ||
| 181 | { | ||
| 182 | $this->disconnect(); | ||
| 183 | } | ||
| 184 | |||
| 185 | if($this->{ERR}) { return -1; } | ||
| 186 | else { return 1; } | ||
| 187 | } | ||
| 188 | |||
| 189 | sub setdata | ||
| 190 | { | ||
| 191 | my ($this, $MSG) = @_; | ||
| 192 | my ($c, $body, $hdrs, $type); | ||
| 193 | |||
| 194 | foreach my $key(keys %{$MSG}) | ||
| 195 | { | ||
| 196 | if($key =~ /mime\-version/i) { $hdrs .="m|"; } | ||
| 197 | elsif($key =~ /content\-type/i) { $hdrs .= "ct|"; $type = $MSG->{$key}; } | ||
| 198 | elsif($key =~ /content\-transfer\-encoding/i) { $hdrs .= "ce|"; } | ||
| 199 | if($key =~ /body/i) { $body = $MSG->{$key}; } | ||
| 200 | else { $this->{SMTP_DATA} .= "$key: $MSG->{$key}\015\012"; } | ||
| 201 | } | ||
| 202 | if($hdrs !~ /m\|/) | ||
| 203 | { | ||
| 204 | $this->{SMTP_DATA} .= "Mime-version: 1.0\015\012"; | ||
| 205 | } | ||
| 206 | if($hdrs !~ /ct\|/) { | ||
| 207 | $this->{SMTP_DATA} .= "Content-type: text/plain; charset=\"iso-8859-1\"\015\012"; | ||
| 208 | } | ||
| 209 | if($hdrs !~ /ce\|/) { | ||
| 210 | $this->{SMTP_DATA} .= "Content-transfer-encoding: 8bit\015\012"; | ||
| 211 | } | ||
| 212 | |||
| 213 | if($type =~ /quoted\-printable/i) { encode_qp $body; } | ||
| 214 | else { $body =~ s/\012/\015\012/g; } | ||
| 215 | $this->{SMTP_DATA} .= "\015\012"; | ||
| 216 | $this->{SMTP_DATA} .= $body; | ||
| 217 | $this->{SMTP_DATA} .= "\015\012.\015\012"; | ||
| 218 | |||
| 219 | unless($c) | ||
| 220 | { | ||
| 221 | $this->{ERR} = "setdata method failed: DATA key missing\n"; | ||
| 222 | return 0; | ||
| 223 | } | ||
| 224 | return 1; | ||
| 225 | } | ||
| 226 | |||
| 227 | sub setfrom | ||
| 228 | { | ||
| 229 | my ($this, $from) = @_; | ||
| 230 | $this->{SMTP_FROM} = $from; | ||
| 231 | return $this->{SMTP_FROM}; | ||
| 232 | } | ||
| 233 | |||
| 234 | sub setrcpt | ||
| 235 | { | ||
| 236 | my ($this, $rcpt) = @_; | ||
| 237 | return push(@{$this->{SMTP_RCPT}}, $rcpt); | ||
| 238 | } | ||
| 239 | |||
| 240 | sub strerror | ||
| 241 | { | ||
| 242 | my $this = shift; | ||
| 243 | return $this->{ERR}; | ||
| 244 | } | ||
| 245 | |||
| 246 | 1; | ||
| 247 | __END__ |