溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

利用php怎么發(fā)送帶附件的郵件

發(fā)布時間:2021-02-03 16:20:08 來源:億速云 閱讀:230 作者:Leah 欄目:開發(fā)技術(shù)

這篇文章給大家介紹利用php怎么發(fā)送帶附件的郵件,內(nèi)容非常詳細(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>

sendmail.php文件代碼:

<?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ā)送失敗";  
} 
?>

關(guān)于利用php怎么發(fā)送帶附件的郵件就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

php
AI