2011-11-17 194 views
0

我正在嘗試製作一個腳本,以便稍後發送電子郵件並附加pdf或其他類型的文件。發送帶有PHP問題的電子郵件pdf附件

我一直在關注各種教程,並已走向死衚衕。

任何人都可以看到我的代碼有什麼問題嗎?我想我已經看了太久了。

<?php 
    include ("../../includes/auth.php"); 

    $id = $_GET['id']; 
    $date = date('y-m-d h:i:s'); 

    $recipient = $_GET['email']; 
    $lname = $_GET['lname']; 
    $fname = $_GET['fname']; 
    $subject ="Enquiry Form - $date"; 
    //-------------attachment stuff---------- 
    $fileatt = "/test.pdf"; 
    $fileatttype = "application/pdf"; 
    //$fileattname = "newname.pdf"; 
    $file = fopen($fileatt, 'rb'); 
    $data = fread($file, filesize($fileatt)); 
    fclose($file); 
    $semi_rand = md5(time()); 
    $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; 
    //------------------------------------- 
    $headers = "From: test person <[email protected]>"; 
    $headers .= "\nMIME-Version: 1.0\n". 
    "Content-Type: multipart/mixed;\n". 
    " boundary=\"{$mime_boundary}\""; 
    $message = "This is a multi-part message in MIME format.\n\n" . 
    "--{$mime_boundary}\n". 
    "Content-Type: text/plain; charset=\"iso-8859-1\"\n". 
    "Content-Transfer-Encoding: 7bit\n\n". $message ."\n\n"; 
    $data = chunk_split(base64_encode($data)); 
    $message .= "–-{$mime_boundary}\n" . 
    "Content-Type: {$fileatttype};\n" . 
    " name=\"{$fileattname}\"\n" . 
    "Content-Disposition: attachment;\n" . 
    " filename=\"{$fileatt}\"\n" . 
    "Content-Transfer-Encoding: base64\n\n" . 
    $data . "\n\n" ."--{$mime_boundary}--\n"; 
    $message ."Dear ".$fname." ".$lname." Test message".$date; 

    echo $recipient."<br />"; 
    echo $subject."<br />"; 
    echo $message."<br />"; 
    echo $headers."<br />"; 

    mail($recipient, $subject, $message, $headers); 

    include ("../../includes/dbconn.php"); 

    $set_datesent = "UPDATE _leads SET `Covering_letter_sent` = '$date' WHERE `ID` = '$id'"; 
    //echo $set_datesent; 
    $result = mysql_query ($set_datesent, $connection) 
    or die ("Failed to perform query : $sql <br />".mysql_error()); 
?> 
+0

嘗試給出完整路徑$ fileatt =「/test.pdf」; – Lylo

+0

你在哪裏指定你的問題是什麼? –

回答

1

我建議你使用類似PEAR Mail庫的東西。然後發送附件變得像下面的代碼一樣簡單易讀。您必須確保已安裝郵件庫,但這是一項相當簡單的任務。

require_once('Mail.php'); 
require_once('Mail/mime.php'); 

$text = wordwrap($message, 70); // You'd put the text version of your message here as $message 

$mime = new Mail_Mime(); 
$mime->setTXTBody($text); 
$mime->setFrom($name . ' <' . $from_email . '>'); // The sender name and email ID go here 
$mime->addAttachment($fpath, $ftype); // File path and type go here 
$mime->setSubject($subject); // Subject goes here 

$body = $mime->get() 
$headers = $mime->headers(); 

$mail =& Mail::factory('mail'); 
$mail->send($to_email, $headers, $body); // The recipients email address goes here 

unset($mime, $mail); 

其他的東西也變得更容易這種方式,例如,寫一個HTML郵件。

0

我強烈建議使用像SwiftMailer這樣的庫。它是免費的,並通過很多簡化你的生活。

+0

謝謝,我會看看這兩個歡呼 –

+0

還有[PHPMailer](http://phpmailer.worxware.com)。在功能/可用性方面都相當。 –

+0

@webnet嗨,你可以幫助我與swiftmailer,我寫了一個腳本使用的文檔和即時通訊只是得到錯誤:(糟糕的時代。 –

0

我建議Pear Mail_mime包(http://pear.php.net/package/Mail_Mime/)。我曾經親手處理過附件處理,因爲我不知道這樣的庫存在(愚蠢的我),當我找到它時,我立即擺脫了我製作不佳的腳本並開始使用Mail_mime。它沒有什麼小故障,但是,它比手工編寫代碼容易得多。

+0

謝謝,我會看看這兩個歡呼聲 –

相關問題