首頁 後端開發 php教程 php写的发送附件的程序实例_PHP

php写的发送附件的程序实例_PHP

Jun 01, 2016 pm 12:31 PM
t 傳送 實例 程式 附件


error_reporting(63);
include('class.html_mime_mail.inc');

/***************************************
** Example of usage.
***************************************/
/***************************************
** Read the file background.gif into
** $backgrnd.
***************************************/
$filename = 'background.gif';
$backgrnd = fread($fp = fopen($filename, 'r'), filesize($filename));
fclose($fp);

/***************************************
** Read the file test.zip into $attachment.
***************************************/
$filename = 'example.zip';
$attachment = fread($fp = fopen($filename, 'r'), filesize($filename));
fclose($fp);

/***************************************
** Create the mail object. Optional headers
** argument. Do not put From: here, this
** will be added when $mail->send
***************************************/
$mail = new html_mime_mail("X-Mailer: Html Mime Mail Class\r\n");

/***************************************
** If sending an html email, then these
** two variables specify the text and
** html versions of the mail. Don't
** have to be named as these are. Just
** make sure the names tie in to the
** $mail->add_html() command further down.
***************************************/
$text = 'This is a test.';
$html = '

Success!

';

/***************************************
** Add the text, html and embedded images.
** Each embedded image has to be added
** using $mail->add_html_image() BEFORE
** calling $mail->add_html(). The name
** of the image should match exactly
** (case-sensitive) to the name in the html.
***************************************/
$mail->add_html_image($backgrnd, 'background.gif', 'image/gif');
$mail->add_html($html, $text);

/***************************************
** If not sending an html email, then
** this is used to set the plain text
** body of the email.
***************************************/
// $mail->body = 'fsss';

/***************************************
** This is used to add an attachment to
** the email.
***************************************/
$mail->add_attachment($attachment, 'example.zip', 'application/octet-stream');

/***************************************
** Builds the message.
***************************************/
$mail->build_message();

/***************************************
** Sends the message. $mail->build_message()
** is seperate to $mail->send so that the
** same email can be sent many times to
** differing recipients simply by putting
** $mail->send() in a loop.
***************************************/
$mail->send('','szw@phpexe.com', 'From Name', 'szw@phpexe.com', 'Subject','');

/***************************************
** Debug stuff. Entirely unnecessary.
***************************************/
echo '

'; <br>echo $mail->mime; <br>echo '
登入後複製
';
?>

class html_mime_mail{

var $headers;
var $body;
var $multipart;
var $mime;
var $html;
var $html_text;
var $html_images = array();
var $cids = array();
var $do_html;
var $parts = array();

/***************************************
** Constructor function. Sets the headers
** if supplied.
***************************************/
function html_mime_mail($headers = ''){
$this->headers = $headers;
}

/***************************************
** Adds a html part to the mail.
** Also replaces image names with
** content-id's.
***************************************/
function add_html($html, $text){
$this->do_html = 1;
$this->html = $html;
$this->html_text = $text;
if(is_array($this->html_images) AND count($this->html_images) > 0){
for($i=0; $ihtml_images); $i ){
$this->html = ereg_replace($this->html_images[$i]['name'], 'cid:'.$this->html_images[$i]['cid'], $this->html);
}
}
}

/***************************************
** Builds html part of email.
***************************************/
function build_html($orig_boundary){
$sec_boundary = '=_'.md5(uniqid(time()));
$thr_boundary = '=_'.md5(uniqid(time()));

if(!is_array($this->html_images)){
$this->multipart.= '--'.$orig_boundary."\r\n";
$this->multipart.= 'Content-Type: multipart/alternative; boundary="'.$sec_boundary."\"\r\n\r\n\r\n";

$this->multipart.= '--'.$sec_boundary."\r\n";
$this->multipart.= 'Content-Type: text/plain'."\r\n";
$this->multipart.= 'Content-Transfer-Encoding: 7bit'."\r\n\r\n";
$this->multipart.= $this->html_text."\r\n\r\n";

$this->multipart.= '--'.$sec_boundary."\r\n";
$this->multipart.= 'Content-Type: text/html'."\r\n";
$this->multipart.= 'Content-Transfer-Encoding: 7bit'."\r\n\r\n";
$this->multipart.= $this->html."\r\n\r\n";
$this->multipart.= '--'.$sec_boundary."--\r\n\r\n";
}else{
$this->multipart.= '--'.$orig_boundary."\r\n";
$this->multipart.= 'Content-Type: multipart/related; boundary="'.$sec_boundary."\"\r\n\r\n\r\n";

$this->multipart.= '--'.$sec_boundary."\r\n";
$this->multipart.= 'Content-Type: multipart/alternative; boundary="'.$thr_boundary."\"\r\n\r\n\r\n";

$this->multipart.= '--'.$thr_boundary."\r\n";
$this->multipart.= 'Content-Type: text/plain'."\r\n";
$this->multipart.= 'Content-Transfer-Encoding: 7bit'."\r\n\r\n";
$this->multipart.= $this->html_text."\r\n\r\n";

$this->multipart.= '--'.$thr_boundary."\r\n";
$this->multipart.= 'Content-Type: text/html'."\r\n";
$this->multipart.= 'Content-Transfer-Encoding: 7bit'."\r\n\r\n";
$this->multipart.= $this->html."\r\n\r\n";
$this->multipart.= '--'.$thr_boundary."--\r\n\r\n";

for($i=0; $ihtml_images); $i ){
$this->multipart.= '--'.$sec_boundary."\r\n";
$this->build_html_image($i);
}

$this->multipart.= "--".$sec_boundary."--\r\n\r\n";
}
}
/***************************************
** Adds an image to the list of embedded
** images.
***************************************/
function add_html_image($file, $name = '', $c_type='application/octet-stream'){
$this->html_images[] = array( 'body' => $file,
'name' => $name,
'c_type' => $c_type,
'cid' => md5(uniqid(time())) );
}


/***************************************
** Adds a file to the list of attachments.
***************************************/
function add_attachment($file, $name = '', $c_type='application/octet-stream'){
$this->parts[] = array( 'body' => $file,
'name' => $name,
'c_type' => $c_type );
}

/***************************************
** Builds an embedded image part of an
** html mail.
***************************************/
function build_html_image($i){
$this->multipart.= 'Content-Type: '.$this->html_images[$i]['c_type'];

if($this->html_images[$i]['name'] != '') $this->multipart .= '; name="'.$this->html_images[$i]['name']."\"\r\n";
else $this->multipart .= "\r\n";

$this->multipart.= 'Content-ID: html_images[$i]['cid'].">\r\n";
$this->multipart.= 'Content-Transfer-Encoding: base64'."\r\n\r\n";
$this->multipart.= chunk_split(base64_encode($this->html_images[$i]['body']))."\r\n";
}

/***************************************
** Builds a single part of a multipart
** message.
***************************************/
function build_part($i){
$message_part = '';
$message_part.= 'Content-Type: '.$this->parts[$i]['c_type'];
if($this->parts[$i]['name'] != '')
$message_part .= '; name="'.$this->parts[$i]['name']."\"\r\n";
else
$message_part .= "\r\n";

// Determine content encoding.
if($this->parts[$i]['c_type'] == 'text/plain'){
$message_part.= 'Content-Transfer-Encoding: 7bit'."\r\n\r\n";
$message_part.= $this->parts[$i]['body']."\r\n";
}else{
$message_part.= 'Content-Transfer-Encoding: base64'."\r\n";
$message_part.= 'Content-Disposition: attachment; filename="'.$this->parts[$i]['name']."\"\r\n\r\n";
$message_part.= chunk_split(base64_encode($this->parts[$i]['body']))."\r\n";
}

return $message_part;
}

/***************************************
** Builds the multipart message from the
** list ($this->parts).
***************************************/
function build_message(){
$boundary = '=_'.md5(uniqid(time()));

$this->headers.= "MIME-Version: 1.0\r\n";
$this->headers.= "Content-Type: multipart/mixed; boundary=\"".$boundary."\"\r\n";
$this->multipart = '';
$this->multipart.= "This is a MIME encoded message.\r\nCreated by html_mime_mail.class.\r\nSee http://www.heyes-computing.net/scripts/ for a copy.\r\n\r\n";

if(isset($this->do_html) AND $this->do_html == 1) $this->build_html($boundary);
if(isset($this->body) AND $this->body != '') $this->parts[] = array('body' => $this->body, 'name' => '', 'c_type' => 'text/plain');

for($i=(count($this->parts)-1); $i>=0; $i--){
$this->multipart.= '--'.$boundary."\r\n".$this->build_part($i);
}

$this->mime = $this->multipart."--".$boundary."--\r\n";
}

/***************************************
** Sends the mail.
***************************************/
function send($to_name, $to_addr, $from_name, $from_addr, $subject = '', $headers = ''){

if($to_name != '') $to = '"'.$to_name.'" ';
else $to = $to_addr;
if($from_name != '') $from = '"'.$from_name.'" ';
else $from = $from_addr;
$this->headers.= 'From: '.$from."\r\n";
//$this->headers.= $headers;
mail($to, $subject, $this->mime, $this->headers);
}

/***************************************
** Use this method to deliver using direct
** smtp connection. Relies upon Manuel Lemos'
** smtp mail delivery class available at:
** http://phpclasses.upperdesign.com
**
** void smtp_send( string *Name* of smtp object,
** string From address,
** array To addresses,
** array Headers,
** string The body)
***************************************/
function smtp_send($smtp_obj, $from_addr, $to_addr){
global $$smtp_obj;
$smtp_obj = $$smtp_obj;

if(substr($this->headers, -2) == "\r\n") $this->headers = substr($this->headers,0,-2);
$this->headers = explode("\r\n", $this->headers);

$smtp_obj->sendmessage($from_addr, $to_addr, $this->headers, $this->mime);
}

} // End of class.
?>

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡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

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

熱工具

記事本++7.3.1

記事本++7.3.1

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

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

熱門話題

Java教學
1664
14
CakePHP 教程
1422
52
Laravel 教程
1316
25
PHP教程
1266
29
C# 教程
1239
24
如何在iPhone中使Google地圖成為預設地圖 如何在iPhone中使Google地圖成為預設地圖 Apr 17, 2024 pm 07:34 PM

iPhone上的預設地圖是Apple專有的地理位置供應商「地圖」。儘管地圖越來越好,但它在美國以外的地區運作不佳。與谷歌地圖相比,它沒有什麼可提供的。在本文中,我們討論了使用Google地圖成為iPhone上的預設地圖的可行性步驟。如何在iPhone中使Google地圖成為預設地圖將Google地圖設定為手機上的預設地圖應用程式比您想像的要容易。請依照以下步驟操作–先決條件步驟–您必須在手機上安裝Gmail。步驟1–開啟AppStore。步驟2–搜尋“Gmail”。步驟3–點選Gmail應用程式旁

如何透過C++編寫一個簡單的倒數計時程式? 如何透過C++編寫一個簡單的倒數計時程式? Nov 03, 2023 pm 01:39 PM

C++是一種廣泛使用的程式語言,在編寫倒數計時器方面非常方便且實用。倒數計時程式是一種常見的應用,它能為我們提供非常精確的時間計算和倒數功能。本文將介紹如何使用C++來寫一個簡單的倒數計時程式。實現倒數程序的關鍵就是使用計時器來計算時間的流逝。在C++中,我們可以使用time.h頭檔中的函數來實作計時器的功能。下面是一個簡單的倒數計時程式的程式碼

iPhone中缺少時鐘應用程式:如何修復 iPhone中缺少時鐘應用程式:如何修復 May 03, 2024 pm 09:19 PM

您的手機中缺少時鐘應用程式嗎?日期和時間仍將顯示在iPhone的狀態列上。但是,如果沒有時鐘應用程序,您將無法使用世界時鐘、碼錶、鬧鐘等多項功能。因此,修復時鐘應用程式的缺失應該是您的待辦事項清單的首位。這些解決方案可以幫助您解決此問題。修復1–放置時鐘應用程式如果您錯誤地從主畫面中刪除了時鐘應用程序,您可以將時鐘應用程式放回原位。步驟1–解鎖iPhone並開始向左側滑動,直到到達「應用程式庫」頁面。步驟2–接下來,在搜尋框中搜尋「時鐘」。步驟3–當您在搜尋結果中看到下方的「時鐘」時,請按住它並

如何使用任務規劃程式開啟網站 如何使用任務規劃程式開啟網站 Oct 02, 2023 pm 11:13 PM

您是否每天在大約相同的時間頻繁地造訪同一網站?這可能會導致花費大量時間打開多個瀏覽器選項卡,並在執行日常任務時使瀏覽器充滿混亂。好吧,打開它而不必手動啟動瀏覽器怎麼樣?這非常簡單,不需要您下載任何第三方應用程序,如下所示。如何設定任務計劃程序以開啟網站?按鍵,在搜尋框中鍵入任務計劃程序,然後按一下開啟。 Windows在右側側邊欄上,按一下「建立基本任務」選項。在名稱欄位中,輸入要開啟的網站的名稱,然後按一下下一步。接下來,在觸發器下,按一下時間頻率並點擊下一步。選擇您希望活動重複多長時間並點擊下一步。選擇啟

iOS 17:如何在「訊息」中組織iMessage應用程式 iOS 17:如何在「訊息」中組織iMessage應用程式 Sep 18, 2023 pm 05:25 PM

在iOS17中,蘋果不僅增加了幾個新的訊息功能,而且還調整了訊息應用程式的設計,使其外觀更乾淨。現在,所有iMessage應用程式和工具(如相機和照片選項)都可以透過點擊鍵盤上方和文字輸入欄位左側的「+」按鈕來存取。點擊“+”按鈕會彈出一個選單列,該列具有預設的選項順序。從頂部開始,有相機,照片,貼紙,現金(如果可用),音訊和位置。最底部是一個「更多」按鈕,點擊該按鈕時會顯示任何其他已安裝的訊息應用程式(您也可以向上滑動以顯示此隱藏清單)。如何重新組織您的iMessage應用程式您可以透過以下方

無法允許存取 iPhone 中的相機和麥克風 無法允許存取 iPhone 中的相機和麥克風 Apr 23, 2024 am 11:13 AM

您在嘗試使用應用程式時是否收到“無法允許存取攝影機和麥克風”?通常,您可以在需要提供的基礎上向特定物件授予攝影機和麥克風權限。但是,如果您拒絕權限,攝影機和麥克風將無法運作,而是顯示此錯誤訊息。解決這個問題是非常基本的,你可以在一兩分鐘內完成。修復1–提供相機、麥克風權限您可以直接在設定中提供必要的攝影機和麥克風權限。步驟1–轉到“設定”選項卡。步驟2–打開「隱私與安全」面板。步驟3–在那裡打開“相機”權限。步驟4–在裡面,您將找到已要求手機相機權限的應用程式清單。步驟5–開啟指定應用的“相機”

抖音如何發給別人文件?上發給別人文件怎麼刪除? 抖音如何發給別人文件?上發給別人文件怎麼刪除? Mar 22, 2024 am 08:30 AM

在抖音上,使用者不僅可以分享自己的生活點滴和才藝,還可以和其他使用者互動交流。在這個過程中,有時候我們需要向其他用戶發送文件,例如圖片、影片等。那麼,在抖音上如何發給別人文件呢?一、抖音上如何發給別人文件? 1.開啟抖音,進入你想要傳送檔案的聊天介面。 2.點選聊天介面中的「+」號,選擇「檔案」。 3.在檔案選項中,你可以選擇傳送圖片、影片、音訊等檔案。選擇你想要發送的文件後,點擊「發送」。 4.等待對方接受你的文件,一旦對方接受,文件就會傳輸成功。二、抖音上發給別人檔案怎麼刪除? 1.打開抖音,進入你發送文

WhatsApp技巧:發送高清照片和影片的方法 WhatsApp技巧:發送高清照片和影片的方法 Sep 10, 2023 am 10:13 AM

WhatsApp推出了一個新選項,允許用戶透過訊息平台以高解析度發送照片和影片。繼續閱讀以了解它是如何完成的。 WhatsApp發布了一個更新,允許iPhone和Android用戶以高分辨率發送照片和視頻,最終解決了該服務的低品質媒體共享限制。該選項稱為“高清品質”,意味著用戶可以以最小的壓縮發送更清晰的照片和影片。例如,在iPhone上捕獲的圖像現在可以以3024x4032分辨率發送,而不是以前的最大920x1280分辨率,而視頻可以以1280×718分辨率發送,而不是848×476分辨率。

See all articles