当前位置: 首页 > 网络学院 > 服务端脚本教程 > PHP > PHP 发送邮件

PHP
WINDOWS下安装MySQL
PHP 制作 网站/服务器 监视脚本
用PHP和CSS制作活动按钮
PHP 单件模式
PHP MVC模式,类封装以及HACK
PHP 中使用正则表达式
PHP 防止 SQL 注入攻击
PHP 跨站点脚本攻击
PHP 防止用户操纵 GET 变量
PHP 防止远程表单提交

PHP 发送邮件


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-03-01   浏览: 1315 ::
收藏到网摘: n/a

PHP allows you to send e-mails directly from a script.
PHP允许你通过脚本直接发送e-mail。


The PHP mail() Function
PHP mail()函数

The PHP mail() function is used to send emails from inside a script.
PHP mail()函数可以从脚本内发送email。

Syntax
语法

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

 

Parameter
参数
Description
描述
to Required. Specifies the receiver / receivers of the email
必要参数。指定email的接收者
subject Required. Specifies the subject of the email. Note: This parameter cannot contain any newline characters
必要参数。指出email的主题。注意:这个参数包含的字符不能换行(只能显示一行)
message Required. Defines the message to be sent. Each line should be separated with a LF (n). Lines should not exceed 70 characters
必要参数。定义发送的信息。每行都必须用(n)分隔开。每行不能超过70个字符
headers Optional. Specifies additional headers, like From, Cc, and Bcc. The additional headers should be separated with a CRLF (rn)
可选参数。指定附加标题,如:form、Cc及Bcc。附加标题通过(rn)进行分隔
parameters Optional. Specifies an additional parameter to the sendmail program
可选参数。给sendmail程序指定一个附加参数

Note: For the mail functions to be available, PHP requires an installed and working email system. The program to be used is defined by the configuration settings in the php.ini file. Read more in our PHP Mail reference.
注意:PHP需要安装、运行email系统才可以使用mail函数。所使用的程序是通过php.ini文件中的配置属性来确定的。阅读更多的关于PHP Mail参数


PHP Simple E-Mail
简易的E-mail

The simplest way to send an email with PHP is to send a text email.
使用PHP发送邮件的最简单的方法就是发送一个文本格式的email。

In the example below we first declare the variables ($to, $subject, $message, $from, $headers), then we use the variables in the mail() function to send an e-mail:
在下面的案例当中,我们首先声明变量($to, $subject, $message, $from, $headers)。接下来我们使用mail()函数中的变量来发送一封e-mail:

<?php
$to = "[email protected]";
$subject = "Test mail";
$message = "Hello! This is a simple email message.";
$from = "[email protected]";
$headers = "From: $from";
mail($to,$subject,$message,$headers);
echo "Mail Sent.";
?>

 


PHP Mail Form
PHP 邮件[mail]格式

With PHP, you can create a feedback-form on your website. The example below sends a text message to a specified e-mail address:
你可以使用PHP在你的网站上建立一个反馈表单[feedback-form]。我们将通过下面的案例教你如何向一个指定的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( "[email protected]", "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>


This is how the example above works:
下面我们具体的讲一下上述代码的工作原理:

  • First, check if the email input field is filled out
    首先,检查email输入框是否已被填充信息
  • If it is not set (like when the page is first visited); output the HTML form
    如果没有填充信息(比如:第一次访问页面);输出HTML表单
  • If it is set (after the form is filled out); send the email from the form
    如果已填充信息(表但已被填写);则发送表单中的email
  • When submit is pressed after the form is filled out, the page reloads, sees that the email input is set, and sends the email
    当表单填写完毕,确认提交之后,页面将被重新装载;此时,你可以看见email设置完毕,并被发送出去

PHP Mail Reference
PHP 邮件[mail]参数

For more information about the PHP mail() function, visit our PHP Mail Reference.
如果你想了解更多关于PHP mail()函数,请访问我们的PHP Mail 参数

评论 (2) 1 All

登陆 | 还没注册?