php簡單實現發送帶附件的郵件,php發送附件郵件
本文執行個體講述了php簡單實現發送帶附件的郵件。分享給大家供大家參考。具體如下:
下面是靜態html代碼:
帶附件的郵件發送
sendmail.php檔案代碼:
<?php //獲得表單資訊 $from = $_POST['from']; $to = $_POST['to']; $subject = $_POST['subject']; $body = $_POST['body']; // 定義分界線 $boundary = "345894369383"; //分界線是一串無規律的字元 //設定header $header = "Content-type: multipart/mixed; boundary= $boundary/r/n"; $header .= "From:$from/r/n"; //獲得上傳檔案的檔案內容 $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); //切割字串 //建立郵件的主體,分為郵件內容和附件內容兩部分 $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--"; //發送郵件 並輸出是否發送成功的資訊 if(mail($to, $subject,$body,$header)) { echo "信件發送成功"; } else { echo "信件發送失敗"; } ?>
希望本文所述對大家的php程式設計有所協助。
http://www.bkjia.com/PHPjc/1014426.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1014426.htmlTechArticlephp簡單實現發送帶附件的郵件,php發送附件郵件 本文執行個體講述了php簡單實現發送帶附件的郵件。分享給大家供大家參考。具體如下: 下面...