PHP自学4——通过mail函数将feedback界面用户填写表单信息发送至指定邮箱

  这一讲的内容依旧简单(谁叫PO主水平菜,依旧是个弱鸡ORZ),通过PHP的内置mail函数将一个反馈界面的信息发送到指定邮箱。在Windows平台不能直接需要使用该函数,需要下载一个sendmail并且配置php.ini和sendmail的sendmail.ini中的相关信息后才能实现邮件的发送。具体方法如下

一、PHP manual中自带热心评论

二、我知道你看不懂英文(请别打PO主),中文的在下面:

(1)先下载sendmail(下载完后直接加压到给定路径后,路径尽量精简且不要包含中文或空格之类)

下载地址:http://glob.com.au/sendmail/

(2)再配置php.ini和sendmail.ini

(3)关闭并重启Apache

具体请参考如下链接:http://blog.csdn.net/lyh66/article/details/18134993

          http://www.chinastor.com/a/jishu/mailserver/0G391362014.html

注意:不同的邮箱的smtp协议不同,需要自行查询,且smtp端口可能不是默认的25,需要自行查询。

反馈界面——feedback.html:

<html>
<head>
    <title>Wayne's DrinkShop - Customer Feedback</title>
</head>
<h1>Customer Feedback</h1>
<p>Please tell us what you think.</p>
<form action="processFeedback.php" method="post">
    <p>Your name:</p>
    <input type="text" name="name" size="40"/>
    <p>Your email address:</p>
    <input type="text" name="email" size="40"/>
    <p>Your feedback:</p>
    <textarea rows="8" cols="40" wrap="virtual" name="comment"></textarea><br />
    <input type="submit" value="Send feedback" />
</form>
</html>

 

后台处理程序——processFeedback.php:

<html>
<head>
    <title>Wayne's DrinkShop - Feedback Submitted</title>
</head>
<?php
//Get the data from form
$name = trim($_POST['name']);
$email = trim($_POST['email']);
$comment = trim($_POST['comment']);

//configuration for mail
$toAddress = "[email protected]";  #Please add the mailAddress you want send email, such as [email protected]
$subject   = "Customer's Feedback";
$message   = "Customer name: ".$name.
             "\nCustomer email: ".$email.
             "\nCustomer feedback: \n".$comment;
$fromAddress    = "From:[email protected]";
//send message and handle fail case
if(!mail($toAddress, $subject, $message, $fromAddress)){
    echo "<p><strong>Failed to send message</strong></p>";
    exit;
}
?>
<body>
<h1>Feedback Submitted</h1>
<p>Your feedback has been sent.</p>
<?php
    echo nl2br($message);
?>
</body>
</html>

反馈页面显示效果:

发送成功后页面效果:

邮箱结果

修订于2016/3/6  By野马菌

 

你可能感兴趣的:(PHP自学4——通过mail函数将feedback界面用户填写表单信息发送至指定邮箱)