Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

203 lines
5.8 KiB

  1. # Filename: sendmsg.pl
  2. #
  3. # Have any changes to this file reviewed by DavePr, BryanT, or WadeLa
  4. # before checking in.
  5. # Any changes need to verified in all standard build/rebuild scenarios.
  6. #
  7. # routine for sending message at microsoft via SMTP
  8. #
  9. # to use specify
  10. # BEGIN {
  11. # require $ENV{'sdxroot'} . '\TOOLS\sendmail.pl';
  12. # }
  13. # or
  14. # BEGIN {
  15. # push @INC, $ENV{'sdxroot'} . '\TOOLS';
  16. # require 'sendmail.pl';
  17. # }
  18. #
  19. use Socket;
  20. #
  21. # sendmsg [-v,] sender, subject, message, recipient [, recipient ...]
  22. #
  23. # uses SMTP port connection to send a mail message
  24. # optional -v will provide verbosity on unexpected output from the SMTP server
  25. # returns 1 on failure, 0 on success
  26. #
  27. # A recipient of the form "replyto:alias" sets the Reply-To field to be the
  28. # specified alias rather than the default of "noone".
  29. #
  30. # A recipient of the form "cc:alias" will be placed on the CC line instead of
  31. # the To line.
  32. #
  33. # A recipient of the form "content:..." will set the message's content type.
  34. # E.g., "content:text/html", if you want your message to be intrepreted as HTML.
  35. sub sendmsg {
  36. my @SmtpServers = ("popdog", "red-imc-01", "red-imc-02", "red-imc-03", "smarthost");
  37. for (@SmtpServers) {
  38. $rc = sendmsg2 ($_, @_);
  39. last if !$rc;
  40. } continue {
  41. print "WARNING: Connection to $_ failed. Will try another SMTP server\n";
  42. }
  43. if ($rc) {
  44. print "WARNING: MAIL NOT SENT: All SMTP servers failed\n";
  45. return 1
  46. }
  47. return 0;
  48. }
  49. sub sendmsg2 {
  50. my $Company = '@Microsoft.com';
  51. #
  52. # red-imc-01/02 seem the most reliable right now (8/20/2000). We used smarthost
  53. # alone for a long time. red-imc-03 isn't authenticating us correctly (and it and
  54. # popdog expect \r\n, and don't spout OK).
  55. #
  56. my $SmtpPort = 25;
  57. # my @SmtpServers = ("popdog", "red-imc-01", "red-imc-02", "red-imc-03", "smarthost");
  58. # my @SmtpServers = ("popdog", "red-imc-01", "red-imc-02", "smarthost");
  59. my @SmtpServers = ( "red-imc-01", "red-imc-02", "smarthost");
  60. my $SmtpServer = shift;
  61. my $verbose = shift if $_[0] =~ /^-v$/i;
  62. my $NotAtMicrosoft = shift if $_[0] =~ /^-m$/i;
  63. my $sender = shift;
  64. my $subject = shift;
  65. my $msg = shift;
  66. my @rcpts = @_;
  67. my $replyto = 'noone';
  68. my $rcptlist;
  69. my $cclist;
  70. my $ct = "";
  71. my $iaddr;
  72. my $paddr;
  73. my $proto = getprotobyname('tcp');
  74. my $r;
  75. my $to;
  76. socket(SOCK, PF_INET, SOCK_STREAM, $proto) or return 1;
  77. {
  78. #
  79. # hardwired to Pacific Time
  80. # format: Date: Thu, 13 Jan 2000 15:55:25 -0800
  81. #
  82. ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime;
  83. $datestamp = sprintf "%3s, %2d %3s %4d %02d:%02d:%02d -0%1d00",
  84. (Sun, Mon, Tue, Wed, Thu, Fri, Sat)[$wday],
  85. $mday,
  86. (Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec)[$mon],
  87. 1900+$year,
  88. $hour, $min, $sec, 8 - $isdst;
  89. #
  90. # Get a connection to the SMTP server
  91. #
  92. $iaddr = inet_aton($SmtpServer);
  93. $paddr = sockaddr_in($SmtpPort, $iaddr);
  94. $rc = connect(SOCK, $paddr);
  95. last if !$rc;
  96. $r = <SOCK>;
  97. print $r if $verbose;
  98. send SOCK, "HELO\r\n", 0 or last;
  99. $r = <SOCK>;
  100. print $r if $verbose;
  101. last if $r !~ /^250 /i;
  102. if ($NotAtMicrosoft) {
  103. send SOCK, "MAIL From: <$sender>\r\n", 0 or last;
  104. } else {
  105. send SOCK, "MAIL From: <$sender$Company>\r\n", 0 or last;
  106. }
  107. $r = <SOCK>;
  108. print $r if $verbose;
  109. last if $r !~ /^250 /i;
  110. $rcptlist = "";
  111. $cclist = "";
  112. for (@rcpts) {
  113. if ($NotAtMicrosoft || /^content:/) {
  114. $to = $_;
  115. } else {
  116. $to = $_ . $Company;
  117. }
  118. if ($to =~ s/^replyto://i) {
  119. $replyto = $to;
  120. } elsif ($to =~ s/^content://i) {
  121. $ct = $to;
  122. } else {
  123. if ($to =~ s/^cc://i) {
  124. $cclist = "$cclist $to,";
  125. } else {
  126. $rcptlist = "$rcptlist $to,";
  127. }
  128. send SOCK, "RCPT To: <$to>\r\n", 0 or last;
  129. $r = <SOCK>;
  130. print $r if $verbose;
  131. last if $r !~ /^250 /i;
  132. }
  133. }
  134. chop $rcptlist; # remove trailing ','
  135. chop $cclist; # remove trailing ','
  136. send SOCK, "DATA\r\n", 0 or last;
  137. $r = <SOCK>;
  138. print $r if $verbose;
  139. last if $r !~ /^354 /i;
  140. if ($NotAtMicrosoft) {
  141. send SOCK, "From: $sender\r\n", 0 or last;
  142. } else {
  143. send SOCK, "From: $sender$Company\r\n", 0 or last;
  144. }
  145. send SOCK, "To:$rcptlist\r\n", 0 or last;
  146. send SOCK, "Cc:$cclist\r\n", 0 or last if $cclist;
  147. # send SOCK, "Bcc:\r\n", 0 or last;
  148. send SOCK, "Subject: $subject\r\n", 0 or last;
  149. send SOCK, "Date: $datestamp\r\n", 0 or last;
  150. send SOCK, "Content-Type: $ct\r\n", 0 or last if $ct;
  151. send SOCK, "Reply-To: $replyto\r\n", 0 or last;
  152. send SOCK, "Importance: High\r\n", 0 or last if $subject =~ /failed/i;
  153. send SOCK, "\r\n", 0 or last;
  154. send SOCK, "$msg", 0 or last;
  155. send SOCK, "\r\n.\r\n", 0 or last;
  156. $r = <SOCK>;
  157. print $r if $verbose;
  158. last if $r !~ /^250 /i;
  159. send SOCK, "QUIT\r\n", 0 or last;
  160. } continue {
  161. #
  162. # Success return
  163. #
  164. close(SOCK);
  165. return 0;
  166. }
  167. #
  168. # Error return (last)
  169. #
  170. close(SOCK);
  171. return 1;
  172. }
  173. #
  174. # return true to our caller
  175. #
  176. return 1;