send.pl
上传用户:dostar
上传日期:2007-01-04
资源大小:21k
文件大小:9k
源码类别:

WEB邮件程序

开发平台:

Perl

  1. #!/usr/local/bin/perl
  2. BEGIN { $APP_PATH=$0; $APP_PATH=~s/(/*)[^/]+$/$1/g; }
  3. # @ ----------------------------------------------------------------------------------------------------------
  4. # @ This code is (c) 1999 Alexandre Aufrere and NikoSoft.
  5. # @ Published under NPL rights, meaning you have the right
  6. # @ to use and modify this code freely, provided it 
  7. # @ remains available and free. Any modified code should be
  8. # @ submitted to Nikopol Software Corp. or Alexandre Aufrere.
  9. # @ This code is protected by the French laws on Copyright.
  10. # @ Please note that there it comes with NO WARRANTY of any kind,
  11. # @ and especially for any damage it could cause to your computer
  12. # @ or network.
  13. # @ Using this code means you agree to this license agreement.
  14. # @ Further information at http://nikosoft.free.fr
  15. # @ ----------------------------------------------------------------------------------------------------------
  16. # @ 
  17. # @ Project NS WebMail
  18. # @ 
  19. # @ Filename send.pl
  20. # @ 
  21. # @ Description sends mail using SMTP protocol. the mail sender
  22. # @  is authentified through POP, preventing this script
  23. # @  from beeing used for bomb mailing and so on.
  24. # @ 
  25. # @ Version 1.0
  26. # @ 
  27. # @ ----------------------------------------------------------------------------------------------------------
  28. use Socket;
  29. use Mail::POP3Client;
  30. use MIME::Lite;
  31. use CGI; # required due to file upload needs
  32. require $APP_PATH."config.pl";
  33. require $APP_PATH."common.pl";
  34. #obtain the FORM information that has been passed by using
  35. #the param() method of the cgi object.
  36. my $query = new CGI;
  37. $loginname = $query->param("loginname");
  38. $password  = $query->param("password");
  39. $POPserver = $query->param("POPserver");
  40. $sender    = $query->param("sender");
  41. $to        = $query->param("to");
  42. $cc        = $query->param("cc");
  43. $subject   = $query->param("subject");
  44. $message   = $query->param("message");
  45. $cache     = $query->param("cache");
  46. # POP connexion to authentify sender
  47. my $pop = new Mail::POP3Client( USER=>$loginname, 
  48. PASSWORD=> decryptit($password), 
  49. HOST=>$POPserver,
  50.             AUTH_MODE=>$POPauth
  51. );
  52. my $MessageCount = $pop->Count;
  53. $pop->Close();
  54. if ($MessageCount == -1) {
  55. print "<font size=+1>$POPserver: $loginname, $incorrectlogin";
  56. exit;
  57. }
  58. # sending the mail using sendmail function
  59. # the apparently-from field  is made using login and POP server, but the
  60. # from and reply-to fields are specified by the sender in the from field
  61. #output the header to the client browser (DO NOT DELETE).
  62. #lack of this will result in an error 500.
  63. print "Content-type: text/htmlnn";
  64. print "<HTML><HEAD><TITLE>NSWM - Message sent</TITLE>";
  65. if ($cache eq "No") { print "<META HTTP-EQUIV='Pragma' CONTENT='no-cache'>"; }
  66. # file upload
  67. if ($query->param("file") ne "") {
  68. my $file = $query->param("file");
  69. my $bytesread;
  70. my $buffer;
  71. my $tmpfilename="$temppath/upload".(localtime)[0].(localtime)[1].".tmp";
  72. # writing file on disk
  73. open (OUTFILE,">$tmpfilename");
  74. binmode(OUTFILE);
  75. while ($bytesread=read($file,$buffer,1024)) {
  76. print OUTFILE $buffer;
  77. }
  78. close OUTFILE;
  79. my $filename=$file;
  80. $filename =~ s/.*\(.*)$/$1/i;
  81. my $fullqfn=$tmpfilename;
  82. # MIME Type detection
  83. my $mime_filetype="";
  84. $mime_filetype = "image/jpg" if ($filename =~ m/.*jpg$/i);
  85. $mime_filetype = "image/gif" if ($filename =~ m/.*gif$/i);
  86.     $mime_filetype = "image/png" if ($filename =~ m/.*png$/i);
  87. $mime_filetype = "application/msword" if ($filename =~ m/.*doc$/i);
  88. $mime_filetype = "application/rtf" if ($filename =~ m/.*rtf$/i);
  89. $mime_filetype = "application/x-zipped-compressed" if ($filename =~ m/.*zip$/i);
  90. $mime_filetype = "application/pdf" if ($filename =~ m/.*pdf$/i);
  91. $mime_filetype = "application/vnd.ms-powerpoint" if ($filename =~ m/.*ppt$/i);
  92. $mime_filetype = "application/vnd.ms-excel" if (($filename =~ m/.*xls$/i)||($filename =~ m/.*xlt$/i));
  93.     $mime_filetype = "application/octet-stream" if ($filename =~m/.*exe$/i);
  94.     $mime_filetype = "application/x-tar" if ($filename =~m/.*tar$/i);
  95.     $mime_filetype = "application/x-gzip" if ($filename =~m/.*gz$/i);
  96.     $mime_filetype = "text/x-vcard" if ($filename =~m/.*vcf$/i);
  97.     $mime_filetype = "audio/x-wav" if ($filename =~m/.*wav$/i);
  98.     $mime_filetype = "video/mpeg" if ($filename =~m/.*mpg$/i);
  99. if ($mime_filetype eq "") {
  100. print "$mimetypenotdetected $filename.n";
  101. exit;
  102. }
  103. # Building Mail Message
  104. my $msg = new MIME::Lite
  105. Type =>'multipart/mixed';
  106. attach $msg
  107. Type =>'TEXT',
  108. Data =>$message;
  109. attach $msg
  110. Type =>$mime_filetype,
  111. Path =>$fullqfn,
  112. Encoding =>"base64",
  113. Filename =>$filename;
  114. $message = $msg->as_string;
  115. $message =~ s/X-Mailer.*n//;
  116. unlink($tmpfilename);
  117. }
  118. else {
  119.   $message="rn".$message;
  120. }
  121. $status = sendmail($loginname."@".$POPserver, $sender, $sender,  $to, $cc, $SMTPserver, $subject, $message);
  122. $date=localtime();
  123. # in case of success, we redirect to the inbox
  124. print "<META HTTP-EQUIV="Refresh" CONTENT="5;URL='inbox.pl?loginname=$loginname&POPserver=$POPserver&cache=$cache&password=$password'">" if ($status==1);
  125. print "</HEAD><BODY BGCOLOR='FFFFFF'>";
  126. print "<br><ul><h3>";
  127. if    ($status ==  1) {
  128. $logfilename=$LOG_PATH.$loginname."@".$POPserver.".log";
  129. print $messagesent;
  130. open (f, ">>$logfilename");
  131. print f $date.";".$to.";".$subject."n";
  132. close (f);
  133. elsif ($status == -1) {print " $smtp : $smtphostunkown";}
  134. elsif ($status == -2) {print $createsocketfailed;}
  135. elsif ($status == -3) {print $connectionfailed;}
  136. elsif ($status == -4) {print $servicena;}
  137. elsif ($status == -5) {print $commerror;}
  138. elsif ($status == -6) {print " $to ($smtp): $userunkown.";}
  139. elsif ($status == -7) {print " $transfailed.";}
  140. elsif ($status == -8) {print " $notofield";}
  141. else {print " $errorunkown.";}
  142. print "</h3></ul><br>";
  143. #send the ending html code (/body and /head tags)
  144. print "</BODY></HTML>";
  145. exit;
  146. #-----------------------------SUBROUTINES------------------------
  147. #------------------------------------------------------------
  148. # sub sendmail()
  149. #
  150. # send/fake email around the world ...
  151. #
  152. # Version : 1.21
  153. # Environment: Hip Perl Build 105 NT 3.51 Server SP4
  154. # Environment: Hip Perl Build 110 NT 4.00
  155. #
  156. # arguments:
  157. #
  158. # $afrom apparently-from email address of sender
  159. # $from email address of sender
  160. # $reply email address for replying mails
  161. # $to email address of reciever
  162. # (multiple recievers can be given separated with space)
  163. # $smtp name of smtp server (name or IP)
  164. # $subject subject line
  165. # $message (multiline) message
  166. #
  167. # return codes:
  168. #
  169. # 1 success
  170. # -1 $smtphost unknown
  171. # -2 socket() failed
  172. # -3 connect() failed
  173. # -4 service not available
  174. # -5 unspecified communication error
  175. # -6 local user $to unknown on host $smtp
  176. # -7 transmission of message failed
  177. # -8 argument $to empty
  178. #
  179. # usage examples:
  180. #
  181. # print
  182. # sendmail("Alice <alice@company.com>",
  183. # "alice@company.com",
  184. # "joe@agency.com charlie@agency.com",
  185. # $smtp, $subject, $message );
  186. #
  187. # or
  188. #
  189. # print
  190. # sendmail($from, $reply, $to, $smtp, $subject, $message );
  191. #
  192. # (sub changes $_)
  193. #
  194. #------------------------------------------------------------
  195. ################################################################
  196. ### sendmail ###################################################
  197. ################################################################
  198. sub sendmail {
  199. my ($afrom,$from, $reply, $to, $cc, $smtp, $subject, $message) = @_;
  200. $date=localtime();
  201. my $fromaddr = $from; chomp $fromaddr; chomp $from;
  202. my $replyaddr = $reply; chomp $replyaddr; chomp $reply; 
  203. $to =~ s/[ t]+/, /g if ($to !~ m/</); # pack spaces and add comma (if email not in John Doe <John.Doe@somewhere.com> format)
  204. $cc =~ s/[ t]+/, /g if ($cc !~ m/</); # pack spaces and add comma
  205. $fromaddr =~ s/.*<([^s]*?)>/$1/; # get from email address
  206. $replyaddr =~ s/.*<([^s]*?)>/$1/; # get reply email address
  207. $replyaddr =~ s/^([^s]+).*/$1/; # use first address
  208. $message =~ s/^./../gm; # handle . as first character
  209. $message =~ s/rn/n/g; # handle line ending
  210. $message =~ s/n/rn/g;
  211. $smtp =~ s/^s+//g; # remove spaces around $smtp
  212. $smtp =~ s/s+$//g;
  213. if (!$to) { return -8; }
  214. my($proto) = (getprotobyname('tcp'))[2];
  215. my($port) = (getservbyname('smtp', 'tcp'))[2];
  216. my($smtpaddr) = ($smtp =~
  217. /^(d{1,3}).(d{1,3}).(d{1,3}).(d{1,3})$/)
  218. ? pack('C4',$1,$2,$3,$4)
  219. : (gethostbyname($smtp))[4];
  220. if (!defined($smtpaddr)) { return -1; }
  221. if (!socket(S, AF_INET, SOCK_STREAM, $proto)) { return -2; }
  222. if (!connect(S, pack('Sna4x8', AF_INET, $port, $smtpaddr))) { return -3; }
  223. my($oldfh) = select(S); $| = 1; select($oldfh);
  224. $_ = <S>; if (/^[45]/) { close S; return -4; }
  225. print S "helo localhostrn";
  226. $_ = <S>; if (/^[45]/) { close S; return -5; }
  227. print S "mail from: <$fromaddr>rn";
  228. $_ = <S>; if (/^[45]/) { close S; return -5; }
  229. foreach (split(/, /, $to)) {
  230.   chomp;
  231.   $tmpto=$_;
  232.   $tmpto =~ s/.*<([^s]*?)>/$1/;
  233.   $tmpto =~ s/,//g;
  234.   print S "rcpt to: <$tmpto>rn";
  235.   $_ = <S>; if (/^[45]/) { close S; return -6; }
  236. }
  237. foreach (split(/, /, $cc)) {
  238.   chomp;
  239.   $tmpcc=$_;
  240.   $tmpcc =~ s/.*<([^s]*?)>/$1/;
  241.   $tmpto =~ s/,//g;
  242.   print S "rcpt to: <$tmpcc>rn";
  243.   $_ = <S>; if (/^[45]/) { close S; return -6; }
  244. }
  245. print S "datarn";
  246. $_ = <S>; if (/^[45]/) { close S; return -5; }
  247. print S "To: $torn";
  248. if ($cc ne "") { print S "CC: $ccrn"; }
  249. print S "From: $fromrn";
  250. print S "Reply-to: $replyaddrrn" if $replyaddr;
  251. print S "Apparently-from: $afromrn";
  252. print S "X-Mailer: NikoSoft WebMail Perl from Alexandre Aufrerern";
  253. print S "Date: $datern";
  254. print S "Subject: $subjectrn";
  255. print S "$message";
  256. print S "rn.rn";
  257. $_ = <S>; if (/^[45]/) { close S; return -7; }
  258. print S "quitrn";
  259. $_ = <S>;
  260. close S;
  261. return 1;
  262. }