2011-11-02 74 views
1

我正在嘗試創建一個PHP函數來發送帶附件的SMTP電子郵件。我很難嘗試創建MIME標題,因爲我希望正文包含HTM格式的文​​本和要附加的文件。我使用PHP的一個非常舊的版本(4.3.8),這是唯一的方法這是工作。試過PEAR,但不會正確驗證SMTP。帶附件的SMTP MIME標頭

這就是我到目前爲止: 我編輯了這段代碼,因爲現在我得到的消息正確,但文件已損壞。我將文件更改爲文本文件,並開始正常,但出現很多垃圾文本。

$newLine = "\r\n"; 
$attachment="myFile.zip"; 
$message = "<br><h1><center>TEST MESSAGE</center></h1>" ; 


    //Body 
    $headers .= "Content-Type: multipart/mixed;" . $newLine; 
    $headers .= "  boundary=\"_e9e06aa5-1550-464d-ace4-e85b575d1899_\"" . $newLine . $newLine; 
    $newLine . $newLine; 

    $headers .= "--_e9e06aa5-1550-464d-ace4-e85b575d1899_" . $newLine; 
    $headers .= "Content-type: text/html; charset=iso-8859-1" . $newLine; 
    $headers .= "Content-Transfer-Encoding: quoted-printable" . $newLine; 
    $headers .= "  boundary=\"_7fdd1316-6f68-41bb-93f7-134933fc9aad_\"" . $newLine . $newLine; 

    $headers .= $message . $newLine; 

    //$headers .= "--_7fdd1316-6f68-41bb-93f7-134933fc9aad_--" . $newLine; // If I leave this line it appears in the end of the message area. 

    //Attachment 
    $headers .= "--_e9e06aa5-1550-464d-ace4-e85b575d1899_" . $newLine; 

    $headers .= "Content-Transfer-Encoding: base64" . $newLine; 
    $headers .= "Content-Type: text/plain;" . $newLine; 
    $headers .= "Content-Disposition: attachment;" . $newLine; 
    $headers .= " filename=" . $attachment . $newLine . $newLine; 

    $handle = fopen($attachment, "rb"); 
    $contents = ''; 
    while (!feof($handle)) { 
     $contents .= fread($handle, filesize($attachment)); 
    } 
    fclose($handle); 

    $contents = base64_encode($contents); 

    $headers .= $contents . $newLine; 
    $headers .= "--_e9e06aa5-1550-464d-ace4-e85b575d1899_--" . $newLine; 

回答

1

如何使用現成的類如PHPMailer?這是多麼eaysier。還有PHP4的版本。

例子:

require_once '../class.phpmailer.php'; 

$mail = new PHPMailer(); 
$mail->AddReplyTo('[email protected]', 'First Last'); 
$mail->AddAddress('[email protected]', 'John Doe'); 
$mail->SetFrom('[email protected]', 'First Last'); 
$mail->AddReplyTo('[email protected]', 'First Last'); 
$mail->Subject = 'PHPMailer Test Subject via mail()'; 
$mail->MsgHTML('Hello world'); 
$mail->AddAttachment('file.zip'); 
$mail->Send(); 

製作自定義頁眉可以使頭痛。

+0

phpmailer的要求之一是它需要PHP版本5或更高版本。可悲的是,我不能在4.3.8上運行的時候對服務器進行任何更新。 – Rick

+0

**「有PHP4的版本。」**鏈接爲你 - http://sourceforge.net/projects/phpmailer/files/phpmailer%20for%20php4/ – Peter

+0

使用此代碼http://coffeeandpaste.blogspot。 com/2009/02/phpmailer-smtp-authentication.html,因爲我的SMTP已通過身份驗證,但無法連接。 – Rick

0

的SMTP數據流是這樣的:(空行是顯著並用CRLF所有端)

To: <[email protected]> 
From: [email protected] 
MIME-Version: 1.0 
Subject:email subject line 
Content-Type: multipart/mixed; 
    boundary="Z--Z=_CGI_Perl_Cookbook_=1409529510" 

This is a multi-part message in MIME format. 

--Z--Z=_CGI_Perl_Cookbook_=1409529510 
Content-Transfer-Encoding: quoted-printable 
Content-Type: text/plain; charset="us-ascii" 

Email message 
text 

--Z--Z=_CGI_Perl_Cookbook_=1409529510 
Content-Disposition: attachment; 
    filename=attachment.txt 
Content-Type: plain/text 
Content-Transfer-Encoding: None 

sometext data 
attached to 
the email 

--Z--Z=_CGI_Perl_Cookbook_=1409529510-- 

. << \n\.\n to terminate the email 
0

多部分/混合MIME郵件創建腳本:https://github.com/breakermind/PhpMimeParser/blob/master/PhpMimeClient_class.php帶有附件和內聯信息,CC和BCC

$m = new PhpMimeClient(); 
// Add to 
$m->addTo("[email protected]", "Albercik"); 
$m->addTo("[email protected]", "Adela"); 
// Add Cc 
$m->addCc("[email protected]", "Ben"); 
$m->addCc("[email protected]"); 
// Add Bcc 
$m->addBcc("[email protected]", "BOSS");  
// Add files inline 
$m->addFile('photo.jpg',"zenek123"); 
// Add file 
$m->addFile('sun.png'); 
// create mime 
$m->createMime("Witaj księżniczko Alabambo",'<h1>Witaj księżniczko Alabambo <img src="cid:zenek123"> </h1>',"Wesołych świąt życzę!","Heniek Wielki", "[email protected]"); 
// get mime 
// $m->getMime(); 
// Show mime 
echo nl2br(htmlentities($m->getMime()));