• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

php mail函数一段好的代码

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
<?php include('includes/title.inc.php');  ?>
<?php 
if(array_key_exists('send',$_POST)&&!$emaliSent)
{
    $to = '[email protected],[email protected]'; // use your own email address
   $subject = 'Feedback from Japan Journey site';
   
  // process the $_POST variables
  $name = $_POST['name'];
  $email = $_POST['email'];
  $comments = $_POST['comments'];
  
  // build the message
  $message = "Name: $name\n\n";
  $message .= "Email: $email\n\n";
  
  $message .= "Comments: $comments";
  
  $header='Dear webmaster';
     $mailSent=mail($to, $subject, $message,$header);
     
 }
?>

 


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Japan Journey<?php if (isset($title)) {echo "&#8212;{$title}";} ?></title>
<link href="assets/journey.css" rel="stylesheet" type="text/css" media="screen" />
</head>

<body>
<div id="header">
<h1>Japan Journey </h1>
</div>
<div id="wrapper">
<?php include('includes/menu.inc.php'); ?>
<div id="maincontent">

<?php
if ($_POST && !$mailSent) {
?>
<p class="warning">Sorry, there was a problem sending your message.
Please try later.</p>
<?php
}
elseif ($_POST && $mailSent)
{
?>

<p><strong>Your message has been sent. Thank you for your feedback.
</strong></p>
<?php } ?>
<p>Ut enim ad minim veniam . . .</p>
<h1>Contact us </h1>
<p>Ut enim ad minim veniam, quis nostrud exercitation consectetur adipisicing elit. Velit esse cillum dolore ullamco laboris nisi in reprehenderit in voluptate. Mollit anim id est laborum. Sunt in culpa duis aute irure dolor excepteur sint occaecat.</p>
<form id="feedback" method="post" action="">
<p>
<label for="name">Name:</label>
<input name="name" id="name" type="text" class="formbox" />
</p>
<p>
<label for="email">Email:</label>
<input name="email" id="email" type="text" class="formbox" />
</p>
<p>
<label for="comments">Comments:</label>
<textarea name="comments" id="comments" cols="60" rows="8"></textarea>
</p>
<p>
<input name="send" id="send" type="submit" value="Send message" />
</p>
</form>
</div>
<?php include('includes/footer.inc.php'); ?>
</div>
</body>
</html>

 

一段更好的代码,能够提示哪些必须要填的项目没填而且保存已填写的项目。(preserve the input )

<?php
include('includes/title.inc.php');
include('includes/corefuncs.php');
if (function_exists('nukeMagicQuotes')) {
  nukeMagicQuotes();
  }

// process the email
if (array_key_exists('send', $_POST)) {
  $to = '[email protected]'; // use your own email address
  $subject = 'Feedback from Japan Journey site';
  
  // list expected fields
  $expected = array('name', 'email', 'comments');
  // set required fields
  $required = array('name', 'comments');
  // create empty array for any missing fields
  $missing = array();
  
  // process the $_POST variables
  foreach ($_POST as $key => $value) {
    // assign to temporary variable and strip whitespace if not an array
    $temp = is_array($value) ? $value : trim($value);
    // if empty and required, add to $missing array
    if (empty($temp) && in_array($key, $required)) {
      array_push($missing, $key);
      }
    // otherwise, assign to a variable of the same name as $key
    elseif (in_array($key, $expected)) {
      ${$key} = $temp;
      }
    }
 
  // go ahead only if all required fields OK
  if (empty($missing)) {
    // build the message
    $message = "Name: $name\n\n";
    $message .= "Email: $email\n\n";
    $message .= "Comments: $comments";

    // limit line length to 70 characters
    $message = wordwrap($message, 70);
  
    // send it  
    $mailSent = mail($to, $subject, $message);
    if ($mailSent) {
      // $missing is no longer needed if the email is sent, so unset it
      unset($missing);
      }
    }
  }
?>

 


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Japan Journey<?php if (isset($title)) {echo "&#8212;{$title}";} ?></title>
<link href="assets/journey.css" rel="stylesheet" type="text/css" media="screen" />
</head>

<body>
<div id="header">
<h1>Japan Journey </h1>
</div>
<div id="wrapper">
<?php include('includes/menu.inc.php'); ?>
<div id="maincontent">
<h1>Contact us</h1>
<?php
if ($_POST && isset($missing)) {
?>
<p class="warning">Please complete the missing item(s) indicated.</p>
<?php
}
elseif ($_POST && !$mailSent) {
?>
<p class="warning">Sorry, there was a problem sending your message. Please try later.</p>
<?php
}
elseif ($_POST && $mailSent) {
?>
<p><strong>Your message has been sent. Thank you for your feedback.</strong></p>
<?php } ?>
<p>Ut enim ad minim veniam, quis nostrud exercitation consectetur adipisicing elit. Velit esse cillum dolore ullamco laboris nisi in reprehenderit in voluptate. Mollit anim id est laborum. Sunt in culpa duis aute irure dolor excepteur sint occaecat.</p>
<form id="feedback" method="post" action="">
<p>
<label for="name">Name: <?php
if (isset($missing) && in_array('name', $missing)) {
?>
<span class="warning">Please enter your name</span><?php } ?>
</label>
<input name="name" id="name" type="text" class="formbox"
<?php if (isset($missing)) {
echo 'value
="'.htmlentities($_POST['name']).'"';} ?>
/>
</p>
<p>
<label for="email">Email: <?php
if (isset($missing) && in_array('email', $missing)) {
?>
<span class="warning">Please enter your email address</span><?php } ?>
</label>
<input name="email" id="email" type="text" class="formbox"
<?php if (isset($missing)) {
echo 'value
="'.htmlentities($_POST['email']).'"';} ?>
/>
</p>
<p>
<label for="comments">Comments: <?php
if (isset($missing) && in_array('comments', $missing)) {
?>
<span class="warning">Please enter your comments</span><?php } ?>
</label>
<textarea name="comments" id="comments" cols="60" rows="8"><?php
if (isset($missing)) {
echo htmlentities($_POST['comments']);
}
?></textarea>
</p>
<p>
<input name="send" id="send" type="submit" value="Send message" />
</p>
</form>
</div>
<?php include('includes/footer.inc.php'); ?>
</div>
</body>
</html>

If studying PHP code makes your brain hurt, you don’t need to worry about how this works. As long as you create the $expected, $required, and $missing arrays in  the previous step, you can just copy and paste the code for use in any form. So    what does it do? In simple terms,

 this foreach loop goes through the $_POST array,  strips out any whitespace from user input, and assigns its contents to a variable  with the same name (so $_POST['email'] becomes $email, and so on). If a
required field is left blank, its name attribute is added to the $missing array.

// go ahead only if all required fields OK
if (empty($missing)) {
// build the message
$message = "Name: $name\n\n";
$message .= "Email: $email\n\n";
$message .= "Comments: $comments";
// limit line length to 70 characters
$message = wordwrap($message, 70);
// send it
$mailSent = mail($to, $subject, $message);
if ($mailSent) {
// $missing is no longer needed if the email is sent, so unset it
unset($missing);
}
}
}
This ensures that the mail is sent only if nothing has been added to $missing.
However, $missing will be used to control the display in the main body of the
page, so you need to get rid of it if the mail is successfully sent. This is done by
using unset(), which destroys a variable and any value it contains.

Let’s turn now to the main body of the page. You need to display a warning if anything
is missing. Amend the conditional statement at the top of the page content
like this:

    <h2>contact us</h2>
    <?php if($_POST&&isset($missing)){ ?>
    <p class="waring">Please complete the missing item(s) indicated.</p>
    <?php } else if($_POST&&!mailSent) { ?>
    <p class="warning">Sorry, there was a problem sending your message.
    Please try later.</p>
    <?php } elseif($_POST&&mailSent) {?>
    <p><strong>Your message has been sent. Thank you for your feedback.
       </strong></p>
     <?php } ?>

 

To display a suitable message alongside each missing required field, add a PHP code
block to display a warning as a <span> inside the <label> tag like this:
<label for="name">Name: <?php
if (isset($missing) && in_array('name', $missing)) { ?>
<span class="warning">Please enter your name</span><?php } ?>
</label>


完整代码:


<?php
if (array_key_exists('send', $_POST)) 
{
    $to="[email protected]";
$subject="test";
$expected=array('name','email','comments'); $required=array('name','comments'); $missing=array(); foreach($_POST as $key=>$value) { $temp=is_array($value)?$value:trim($value); if(empty($temp)&&in_array($key,$required)) { array_push($missing,$key); //$missing[]=$key; } elseif(in_array($key,$expected)) { ${$key}=$temp; } } // go ahead only if all required fields OK if (empty($missing)) { // build the message $message = "Name: $name\n\n"; $message .= "Email: $email\n\n"; $message .= "Comments: $comments"; // limit line length to 70 characters $message = wordwrap($message, 70); // send it $mailSent = mail($to, $subject, $message); if ($mailSent) { // $missing is no longer needed if the email is sent, so unset it unset($missing); } } }

 

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>


<style type="text/css">
body 
{
     width:750px;
     margin:0 auto;
     font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
     background-color:#CF9;
}

label
{
    font-weight:bold;
    color:#000;
}
.warning
{
    font-weight:bold;
    color:red;
}

</style>
</head>

<body>

<div id="header">
    <h1>Japan Journey</h1>
  
</div>
<div id="content">
    <h2>contact us</h2>
    <?php if($_POST&&isset($missing)){ ?>
    <p class="waring">Please complete the missing item(s) indicated.</p>
    <?php } else if($_POST&&!$mailSent) { ?>
    <p class="warning">Sorry, there was a problem sending your message.
    Please try later.</p>
    <?php } elseif($_POST&&$mailSent) {?>
    <p><strong>Your message has been sent. Thank you for your feedback.
       </strong></p>
     <?php } ?>
     
        
<form id="feedback" method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
<p>
<label for="name">Name:</label>
  <?php if(isset($missing)&&in_array('name',$missing)) { ?>
  <span class="warning">Please enter your name</span> <?php } ?><br/>
  
<input name="name" id="name" type="text" class="formbox"  <?php if(isset($missing)){
     echo 'value="'.htmlentities($_POST['name']).'"' ; } ?>/>  
</p>
<p>
<label for="email">Email:</label> <br/>
<input name="email" id="email" type="text" class="formbox" />
</p>
<p>
<label for="comments">Comments:</label>
 <?php if(isset($missing)&&in_array('comments',$missing)) { ?>
  <span class="warning">Please enter your  comments</span> <?php } ?><br/>
<textare 

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
关于PHP程序员技术职业生涯规划发布时间:2022-07-10
下一篇:
Php wampserver修改根目录发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap