php mail郵件發送帶附件功能。

來源:互聯網
上載者:User
 代碼如下 複製代碼

$pdfname="test.pdf";
$email="test@test.com";
$text = "您好,附件中是您需要的pdf檔案。請點擊下載。<br><a href=http://www.111cn.net>www.111cn.net</a>";         //常值內容

$text = base64_encode($text);     //用base64方法把它編碼
$text = chunk_split($text);     //把這個長字串切成由每行76個字元組成的小塊

$subject = $pdfname;         //標題
$from = "admin@111cn.net";     //寄件者
$to = $email;     //接受者

//附件
// 定義分界線
$boundary = "nextpart_".uniqid("");
$boundary2 = "nextpart_".uniqid("");
$headers = "to: $torn";
$headers .= "from: $fromrn";
$headers .="mime-version: 1.0rn";
$headers .= "content-type: multipart/mixed;
            boundary="----=_$boundary"rn";

$read=file_get_contents($pdfname);

$read = base64_encode($read);     //用base64方法把它編碼
$read = chunk_split($read);     //把這個長字串切成由每行76個字元組成的小塊

//現在我們可以建立郵件的主體
$body = "this is a multi-part message in mime format.

------=_$boundary
content-type: multipart/alternative;
    boundary="----=_$boundary2";

------=_$boundary2
content-type: text/html;
    charset="gbk"
content-transfer-encoding: base64

$text

------=_$boundary2--

------=_$boundary
content-type: application/octet-stream;
    charset="gbk";
    name="$pdfname"
content-disposition: attachment; filename="$pdfname"
content-transfer-encoding: base64

$read

-------=_$boundary--";

if(mail($to, $subject,$body,$headers))
   echo "您需要的pdf檔案(".$pdfname.")已經發往您的郵箱:".$to."。<br>請查收。";
else
   echo "抱歉,發送失敗了。<br>";


   ?>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.