package SimpleMail; # # coded by PoizOn (perlmonks.org.ru) use strict; use vars qw(@ISA @EXPORT $VERSION); require Exporter; @ISA = qw(Exporter); @EXPORT = qw(send); $VERSION = "1.00"; use Socket; use MIME::Base64 qw(encode_base64); ### #my %data=(Host => 'localhost', # Helo => 'notebook', # From => 'hider@notebook', # To => 'user@notebook', # charset => 'koi8-r', # Subject => 'Hello!', # Message => 'Hello! Friend!', # File => 'www4mail-3.33.tar.gz', # Type => 'application/octet-stream', # Debug =>1 # ); sub send { my $data=shift;# link on hash my $proto=getprotobyname('tcp'); my $port =$data->{Port} || (getservbyname('smtp', 'tcp'))[2]; my $host=inet_aton($data->{Host}) or die "$data->{Host}: unknown host!\n"; socket(MAIL,AF_INET,SOCK_STREAM,$proto) or die "socket create failed: $!\n"; my $dest_addr=sockaddr_in($port,$host); connect(MAIL,$dest_addr) or die "can't connect: $!\n"; select((select(MAIL),$|=1)[0]); #### начинаем обмен с сервером my $cr="\015\012";# перевод строки my $t="\x09";# табуляция my $buf=undef; my $tmp=undef; sysread(MAIL,$buf,1024); print $buf if $data->{Debug}; $tmp="HELO $data->{Helo}$cr"; syswrite(MAIL,$tmp,length($tmp)); sysread(MAIL,$buf,1024); print $buf if $data->{Debug}; $tmp="MAIL FROM: $data->{From}$cr"; syswrite(MAIL,$tmp,length($tmp)); sysread(MAIL,$buf,1024); print $buf if $data->{Debug}; $tmp="RCPT TO: $data->{To}$cr"; syswrite(MAIL,$tmp,length($tmp)); sysread(MAIL,$buf,1024); print $buf if $data->{Debug}; $tmp="DATA$cr"; syswrite(MAIL,$tmp,length($tmp)); sysread(MAIL,$buf,1024); print $buf if $data->{Debug}; $tmp="To: $data->{To}$cr"; $tmp.="Subject: $data->{Subject}$cr"; $tmp.="From: $data->{From}$cr"; #$tmp.="X-Mailer: Cool$cr"; if(defined ($data->{File})) {# кодируем файл die "File doesn't exist\n" unless (-f $data->{File}); my($line,$encode); open(FILE,"<",$data->{File}) || die "Can't open file $data->{File}: $!\n"; while (read(FILE, $line, 60*57)) { $encode.=encode_base64($line); } close(FILE); $tmp.="MIME-Version: 1.0$cr"; $tmp.="Content-Type: multipart/mixed;$cr"; $tmp.=$t."boundary=\"----=_NextPart_000_001A_01C63405.42455A40\"$cr"; $tmp.=$cr; $tmp.="This is a multi-part message in MIME format.$cr"; $tmp.=$cr; $tmp.="------=_NextPart_000_001A_01C63405.42455A40$cr"; $tmp.="Content-Type: text/plain;$cr"; $tmp.=$t."format=flowed; charset=\"$data->{charset}\";$cr"; $tmp.=$t."reply-type=original;$cr"; $tmp.="Content-Transfer-Encoding: 7bit$cr"; $tmp.=$cr; $tmp.="$data->{Message}$cr"; $tmp.="------=_NextPart_000_001A_01C63405.42455A40$cr"; $tmp.="Content-Type: $data->{Type};$cr"; $tmp.=$t."name=\"$data->{File}\"$cr"; $tmp.="Content-Transfer-Encoding: base64$cr"; $tmp.="Content-Disposition: attachment;$cr"; $tmp.=$t."filename=\"$data->{File}\"$cr"; $tmp.=$cr; $tmp.="$encode$cr"; #$tmp.="------=_NextPart_000_001A_01C63405.42455A40$cr"; } else { $tmp.="$data->{Message}$cr"; } syswrite(MAIL,$tmp,length($tmp)); $tmp=".$cr"; syswrite(MAIL,$tmp,length($tmp)); sysread(MAIL,$buf,1024); print $buf if $data->{Debug}; ## командна QUIT $tmp="QUIT$cr"; syswrite(MAIL,$tmp,length($tmp)); sysread(MAIL,$buf,1024); print $buf if $data->{Debug}; ###### close(MAIL); }