Jun
20

PHP程序之  E-Mail

 16:05    468    0    学习手记
| |
    
       通过 PHP 发送电子邮件的最简单的方式是发送一封文本 email!
1、 语法


mail(to,subject,message,headers,parameters)


参数                                  描述
to                      必需。规定 email 接收者。
subject               必需。规定 email 的主题。注释:该参数不能包含任何新行字符。
message             必需。定义要发送的消息。应使用 LF (\n) 来分隔各行。
headers              可选。规定附加的标题,比如 From、Cc 以及 Bcc。应当使用 CRLF (\r\n) 分隔附加的标题。
parameters          可选。对邮件发送程序规定额外的参数。


注:PHP 需要一个已安装且正在运行的邮件系统,以便使邮件函数可用。所用的程序通过在 php.ini 文件中的配置设置进行定义。

实例:


<?php

$to = "someone@example.com";    '目标MAIL
$subject = "Test mail";
$message = "Hello! This is a simple email message.";
$from = "someonelse@example.com";
$headers = "From: $from";
mail($to,$subject,$message,$headers);
echo "Sent.";

?>



通过 PHP,您能够在自己的站点制作一个反馈表单。下面的例子向指定的 e-mail 地址发送了一条文本消息


<html>
<body>

<?php
if (isset($_REQUEST['email']))
//if "email" is filled out, send email
  {
  //send email
  $email = $_REQUEST['email'] ;
  $subject = $_REQUEST['subject'] ;
  $message = $_REQUEST['message'] ;
  mail( "someone@example.com", "Subject: $subject",
  $message, "From: $email" );
  echo "Thank you for using our mail form";
  }
else
//if "email" is not filled out, display the form
  {
  echo "<form method='post' action='mailform.php'>
  Email: <input name='email' type='text' /><br />
  Subject: <input name='subject' type='text' /><br />
  Message:<br />
  <textarea name='message' rows='15' cols='40'>
  </textarea><br />
  <input type='submit' />
  </form>";
  }
?>

</body>
</html>



例子解释:

1、首先,检查是否填写了邮件输入框
2、如果未填写(比如在页面被首次访问时),输出 HTML 表单
3、如果已填写(在表单被填写后),从表单发送邮件
4、当点击提交按钮后,重新载入页面,显示邮件发送成功的消息
发表评论
表情
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
打开HTML
打开UBB
打开表情
隐藏
记住我
昵称   密码   游客无需密码
网址   电邮   [注册]