搜尋
首頁php教程php手册php简单实现发送带附件的邮件,php发送附件邮件

php简单实现发送带附件的邮件,php发送附件邮件

本文实例讲述了php简单实现发送带附件的邮件。分享给大家供大家参考。具体如下:

下面是静态html代码:

<html>
 <head>
 <title>带附件的邮件发送</title>
 </head>
 <body> 
 <form method="post" name="form1" action="sendmail.php" ENCTYPE="multipart/form-data"> 
  <table> 
  <tr>
   <td>发送人:</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>邮件内容:</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文件代码:

<&#63;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 "信件发送失败";  
} 
&#63;>

希望本文所述对大家的php程序设计有所帮助。

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )專業的PHP整合開發工具

EditPlus 中文破解版

EditPlus 中文破解版

體積小,語法高亮,不支援程式碼提示功能

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器