php使用表单提交的方法来发送邮件。

主要涉及到服务器如何处理表单的数据,以及发送邮件函数mail的用法。代码如下:

<html>
<head>< >php 实例之使用表单提交的方法来发送邮件(用户反馈)</ ></head>
<body>

<?php

$self = $_SERVER[\'PHP_SELF\'];
$username = $_POST[\'username\'];
$useraddr = $_POST[\'useraddr\'];
$comments = $_POST[\'comments\'];
$sent = $_POST[\'sent\'];

$form =\"<form action=\\\"$self\\\" method=\\\"post\\\">\";
$form.=\"Name:<input type=\\\"text\\\" name=\\\"username\\\"\";
$form.=\" size=\\\"30\\\" value=\\\"$username\\\" >\";
$form.=\"Email:<input type=\\\"text\\\" name=\\\"useraddr\\\"\";
$form.=\" size=\\\"30\\\" value=\\\"$useraddr\\\">\";
$form.=\"Comments:<textarea name=\\\"comments\\\" >\";
$form.=\"$comments</textarea><br/>\";
$form.=\"<input type=\\\"submit\\\" name=\\\"sent\\\" value=\\\"Send Form\\\">\";
$form.=\"</form>\";

if($sent)
{
  $valid=true;

  if( !$username )
  { $errmsg.=\"Enter your name...<br />\"; $valid = false; }

  if( !$useraddr )
  { $errmsg .=\"Enter your email address...<br />\"; $valid = false; }

  if( !$comments )
  { $errmsg.=\"Enter your comments...<br />\"; $valid = false; }

  $useraddr = trim($useraddr);
  $_name = \"/^[-!#$%&\\\'*+\\\\.\\/0-9=?A-Z^_\'{|}~]+\";
  $_host = \"([-0-9A-Z]+\\.)+\";
  $_tlds = \"([0-9A-Z]){2,4}$/i\";
  if( !preg_match( $_name.\"@\".$_host .$_tlds,$useraddr ) )
  { 
    $errmsg.=\"Email address has incorrect format!<br />\";
    $valid=false;
  }
}

if($valid != true)
{
  echo( $errmsg.$form );
}
else
{
  $to = \"php@h.com\";

  $re = \"Feedback from $username\";

  $msg = $comments;

  $headers  = \"MIME-Version: 1.0\\r\\n\";
  $headers .= \"Content-type: text/html;\";   
  $headers .= \"charset=\\\"iso-8859-1\\\"\\r\\n\";

  $headers .= \"From: $useraddr \\r\\n\";

  if(mail($to,$re,$msg, $headers))
  { echo(\"Your comments have been sent - thanks $username\");}

}
?>

</body></html>
收藏 打印