2012-05-21 103 views
1

我此腳本的作品完美,但附件是空的(我的意思是,我可以看到attachments..but每個文件我載荷0 KB爲大小) 這裏是代碼:添加附件郵寄

allegato意味着附件

$path = $allegato['tmp_name']; 
    $fp = fopen($path, 'r'); 
    do //we loop until there is no data left 
    { 
    $data = fread($fp, 8192); 
    if (strlen($data) == 0) break; 
    $content .= $data; 
    } while (true); 
    $content_encode = chunk_split(base64_encode($content)); 


    $mime_boundary = "<<<--==+X[".md5(time())."]"; 

    $headers .= "From:".$email."\r\n"; 
    $headers .= "To: me <[email protected]>\r\n"; 

    $headers .= "MIME-Version: 1.0\r\n"; 
    $headers .= "Content-Type: multipart/mixed;\r\n"; 
    $headers .= " boundary=\"".$mime_boundary."\""; 

    $message .= "This is a multi-part message in MIME format.\r\n"; 
    $message .= "\r\n"; 
    $message .= "--".$mime_boundary."\r\n"; 

    $message .= "Content-Type: text/plain; charset=\"iso-8859-1\"\r\n"; 
    $message .= "Content-Transfer-Encoding: 7bit\r\n"; 
    $message .= "\r\n"; 
    $message .= "Email sent from:".$nome." \r\n"; 
    $message .= $messaggio."\r\n"; 
    $message .= "--".$mime_boundary."\r\n"; 

    $message .= "Content-Type: ".$allegato_type."\"\r\n"; 
    $message .= " name=\ ".$allegato_name."\"\r\n"; 
    $message .= "Content-Transfer-Encoding: base64\r\n"; 
    $message .= "Content-Disposition: attachment;\r\n"; 
    $message .= " filename=\ ".$allegato_name."\"\r\n"; 
    $message .= "\r\n"; 
    $message .= $content_encode; 
    $message .= "\r\n"; 
    $message .= "--".$mime_boundary."\r\n"; 

    $ok = mail("[email protected]", $object, $message, $headers); 

    if($ok) 
    { 
    echo'<script type="text/javascript">alert("mail sent successfully ! ");</script>'; 
    } 
else 
    { echo'<script type="text/javascript">alert("mail failed! ");</script>';} 

有人可以帶附件的幫助嗎?(發送的每一個文件是0 KB,應該如何解決?)

+0

我開始了這種方式爲好,然後我意識到PHP PEAR框架的[Mail_Mime包(HTTP ://pear.php.net/package/Mail_Mime/redirected)要容易得多,而且我的問題少了很多。我建議你試試看。 – Travesty3

+0

我不知道它是否相關,但似乎在''filename = \「中缺少一個引用。$ allegato_name。」\「\ r \ n」;' – sofl

+0

Travesty3問題是我不知道if梨是安裝在生產服務器。sofl也許你是對的,它應該如何? – Kato

回答

1

我建議使用PHPMailer的!

它可以免費從http://sourceforge.net/projects/phpmailer/

只要把它在服務器上的目錄,包括它的主要PHP和你的生活從PHP發送電子郵件時,會變成一種享受。

我有一個簡單的開始,我可以提供給你立竿見影的效果的功能:

require("/path/to/class.phpmailer.php"); 

function mail_sender($from_name,$from_email,$to_email,$subject,$mailtext,$att = false) { 

    $mail = new PHPMailer(); 

    $mail->IsSMTP();    // set mailer to use SMTP 
    $mail->Host = "localhost";  // specify main and backup server 
    $mail->SMTPAuth = false;  // turn on SMTP authentication 

    $mail->From = $from_email; 
    $mail->FromName = $from_name; 
    $mail->WordWrap = 50;   // set word wrap to 50 characters 
    $mail->IsHTML(true);   // set email format to HTML 
    $mail->CharSet="utf-8"; 

    $mail->Subject = $subject; 
    $mail->Body = $mailtext; 
    $mail->AltBody = ""; 

    $mail->AddAddress($to_email); 

    if ($att) { 
     $mail->AddAttachment("include/attachment.doc"); 
    } 

    if(!$mail->Send()) { 
      echo "Message could not be sent. <p>"; 
      echo "Mailer Error: " . $mail->ErrorInfo; 
      exit; 
    } 

}