PHP 发送电子邮件

2018 年 2 月 16 日2660

PHP 发送电子邮件

PHP Sessions

PHP 安全 E-mail

PHP 允许您从脚本直接发送电子邮件

PHP mail() 函数

PHP mail() 函数用于从脚本中发送电子邮件。

语法

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 Mail 参考手册阅读更多内容。

PHP 简易 E-Mail

通过 PHP 发送电子邮件的最简单的方式是发送一封文本 email。

在下面的例子中,我们首先声明变量($to, $subject, $message, $from, $headers),然后我们在 mail() 函数中使用这些变量来发送了一封 e-mail:

<?php







$to = "someone@example.com";



$subject = "Test mail";



$message = "Hello! This is a simple email message.";



$from = "someonelse@example.com";



$headers = "From: $from";



mail($to,$subject,$message,$headers);



echo "Mail Sent.";







?>

PHP Mail Form

通过 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 type='text' /><br />



Subject: <input type='text' /><br />



Message:<br />



<textarea rows='15' cols='40'>



</textarea><br />



<input type='submit' />



</form>";



}



?>







</body>



</html>

例子解释:

PHP Mail 参考手册

如需更多有关 PHP mail() 函数的信息,请访问我们的 PHP Mail 参考手册。

PHP Sessions

PHP 安全 E-mail

0 0