《PHP學(xué)習(xí):php簡(jiǎn)單實(shí)現(xiàn)發(fā)送帶附件的郵件》要點(diǎn):
本文介紹了PHP學(xué)習(xí):php簡(jiǎn)單實(shí)現(xiàn)發(fā)送帶附件的郵件,希望對(duì)您有用。如果有疑問,可以聯(lián)系我們。
PHP學(xué)習(xí)本文實(shí)例講述了php簡(jiǎn)單實(shí)現(xiàn)發(fā)送帶附件的郵件.分享給大家供大家參考.具體如下:
PHP學(xué)習(xí)下面是靜態(tài)html代碼:
PHP學(xué)習(xí)
<html>
<head>
<title>帶附件的郵件發(fā)送</title>
</head>
<body>
<form method="post" name="form1" action="sendmail.php" ENCTYPE="multipart/form-data">
<table>
<tr>
<td>發(fā)送人:</td>
<td><input type="text" name="from"></td>
</tr>
<tr>
<td>收件人:</td>
<td><input type="text" name="to"></td>
</tr>
<tr>
<td>郵件主題:</td>
<td><input type="text" name="subject"></td>
</tr>
<tr>
<td>郵件內(nèi)容:</td>
<td><textarea name="body"></textarea></td>
</tr>
<tr>
<td>附件上傳:</td>
<td><input type="file" name="upload_file"></td>
</tr>
<tr>
<td span=2>
<input type="submit" value="提交">
<input type="reset" value="重置">
</td>
</tr>
</table>
</form>
</body>
</html>
PHP學(xué)習(xí)sendmail.php文件代碼:
PHP學(xué)習(xí)
<?php
//獲得表單信息
$from = $_POST['from'];
$to = $_POST['to'];
$subject = $_POST['subject'];
$body = $_POST['body'];
// 定義分界線
$boundary = "345894369383"; //分界線是一串無規(guī)律的字符
//設(shè)置header
$header = "Content-type: multipart/mixed; boundary= $boundary/r/n";
$header .= "From:$from/r/n";
//獲得上傳文件的文件內(nèi)容
$file = $_FILES['upload_file']['tmp_name'];
//確定上傳文件的MIME類型
$mimeType = $_FILES['upload_file']['type'];
//獲得上傳文件的文件名
$fileName = $_FILES['upload_file']['name'];
//讀取上傳文件
$fp = fopen($file, "r"); //打開文件
$read = fread($fp, filesize($file)); //讀入文件
$read = base64_encode($read); //base64編碼
$read = chunk_split($read); //切割字符串
//建立郵件的主體,分為郵件內(nèi)容和附件內(nèi)容兩部分
$body = "--$boundary
Content-type: text/plain; charset=iso-8859-1
Content-transfer-encoding: 8bit
$body
--$boundary
Content-type: $mimeType; name=$fileName
Content-disposition: attachment; filename=$fileName
Content-transfer-encoding: base64
$read
--$boundary--";
//發(fā)送郵件 并輸出是否發(fā)送成功的信息
if(mail($to, $subject,$body,$header))
{
echo "信件發(fā)送成功";
}
else
{
echo "信件發(fā)送失敗";
}
?>
PHP學(xué)習(xí)希望本文所述對(duì)大家的php程序設(shè)計(jì)有所贊助.
歡迎參與《PHP學(xué)習(xí):php簡(jiǎn)單實(shí)現(xiàn)發(fā)送帶附件的郵件》討論,分享您的想法,維易PHP學(xué)院為您提供專業(yè)教程。
轉(zhuǎn)載請(qǐng)注明本頁(yè)網(wǎng)址:
http://www.snjht.com/jiaocheng/10372.html