服务器 频道

用sendmail发送HTML格式的邮件

  【IT168 服务器学院】先将来自HTML页面的数据解码,再调用unix系统sendmail命令即可。 

  例如下:

  #!/usr/local/bin/perl 
  # 
  &readparse; 
  print "Content-type: text/htmlnn"; 
  # 
  #********* BEGIN BODY******************** 

  #******** END BODY************************ 
  # 
  # EACH VALUE IN THE HTML FORM WILL BE CONTAINED IN 
  # THE THE @VALUE ARRAY. 
  sub readparse { 
  read(STDIN,$user_string,$ENV{''CONTENT_LENGTH''}); 
  if (length($ENV{''QUERY_STRING''})>0) {$user_string=$ENV{''QUERY_STRING''} 
  }; 
  $user_string =~ s/+/ /g; 
  @name_value_pairs = split(/&/,$user_string); 
  foreach $name_value_pair (@name_value_pairs) { 
  ($keyword,$value) = split(/=/,$name_value_pair); 
  $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/ge; 
  push(@value, "$value"); 
  $user_data{$keyword} = $value; 
  if ($value=~/print "Content-type: text/htmlnnNo SSI permitted"; 
  exit; 
  }; 
  }; 
  }; 

  #E-MAIL SUBROUTINE 
  #ADD "&email(to,from,subject,text)" TO YOUR SCRIPT 
  #REMEMBER TO BACKSLASH THE @ WHEN YOU ARE NOT USING IT IN AN ARRAY 
  #FOR EXAMPLE: 
  # $to=''robyoung@mediaone.net''
  # $from=''foo@company.com''
  # $subject=''Thank you for your inquiry''; 
  # $text=''Dear readernnThank you for your recent inquiry.''; 
  # &email($to,$from,$subject,$text); 

  sub email { 
  local($to,$from,$sub,$letter) = @_; 
  $to=~s/@/@/
  $from=~s/@/@/
  open(MAIL, "|/usr/lib/sendmail -t") || die 
  "Content-type: text/textnnCan''t open /usr/lib/sendmail!"; 
  print MAIL "To: $ton"; 
  print MAIL "From: $fromn"; 
  print MAIL "Subject: $subn"; 
  print MAIL "$lettern"; 
  return close(MAIL); 
  }
  

0
相关文章