2013-06-28 126 views
3

我正在嘗試創建一個腳本,用於發送從iPhone應用程序中上傳的圖像,並通過電子郵件發送給我。PHP發送帶有圖像附件的電子郵件

我已經能夠讓腳本給我發送附件的電子郵件。但是,附件始終爲0kb。

這裏是我的代碼:

// Standard email info 


$to = '[email protected]'; 
$subject = 'Email with an image attached'; 

// This variable will be used when declaring the "boundaries" 
// for the different sections of the email 
$boundary = md5(date('r', time())); 

//Initial Headers 
$headers = "MIME-Version: 1.0\r\n"; // <- the "\r\n" indicate a carriage return and newline, respectively 
$headers .= "From: <[email protected]>\r\n"; 
$headers .= "Content-Type: multipart/mixed; boundary=" . $boundary . "\r\n"; // <- This is 
// saying the there will be more than one (a "mix") of Content Types in this email. 
// The "boundary" value will indicate when each content type will start 

    //First Content Type 
$message = "\r\n\r\n--" . $boundary . "\r\n"; // <- This indicates that I'm going to start 
// declaring headers specific to this section of the email. 
// MAKE SURE there's only ONE(1) "\r\n" between the above boundry and the first header below (Content-Type) 
$message .= "Content-type: text/plain; charset=\"iso-8859-1\"\r\n"; // <- Here I'm saying this content should be plain text 
$message .= "Content-Transfer-Encoding: 7bit\r\n\r\n"; 

// Body of the email for the headers I just declared 
$message .= "Someone filled out the form. See their info below:\r\n"; 
$message .= "Name:"; 

//Second Content Type 
$message .= "\r\n\r\n--" . $boundary . "\r\n"; // <- This idicates that I'm going to start 
// declaring some more headers for the content below 
// MAKE SURE there's only ONE(1) "\r\n" between the above boundry and the first header below (Content-Type) 
$message .= "Content-type: image/jpeg\r\n"; // <- Here I'm saying that this Content Type is for a JPEG image 
$message .= "Content-Transfer-Encoding: base64\r\n"; // <- this is saying that this section's content will be base64 Encoded 
$message .= "Content-Disposition: attachment; filename=\"Image.jpg\"\r\n"; // <- This is saying the content below should be an attachment and gives it a file name 

// The base64_encode below is necessary because this is a file. 
$message .= base64_encode(file_get_contents($_FILES["userfile"]["name"])); 

$message .= "\r\n\r\n--" . $boundary . "--"; // <- This indicates the end of the boundries. Notice the additional "--" after the boundry's value. 

// Send the email using "mail()". 
// Adding the "$mail_sent = " before "mail()" will store TRUE in $mail_sent if the email is sent successfully 
// Adding the "@" sign before "mail()" will disable error display so users 
// won't see the actual error info if it fails, just "Mail failed". 
$mail_sent = @mail($to, $subject, $message, $headers); 

//Check to see if the email was sent successfully ($mail_sent = true). 
//If so, display "Mail Sent" to the screen, else display "Mail Failed". 
echo $mail_sent ? "Mail sent" : "Mail failed"; 

,一切工作正常,除了附件是0KB。

任何幫助表示讚賞。

謝謝。

+3

不要建立自己的MIME電子郵件。使用PHPMailer或Swiftmailer。它們都使它變得微不足道,並將代碼縮減爲幾行。 –

回答

10
 // to, from, subject, message body, attachment filename, etc. 
     $to = "[email protected]"; 
     $from = "[email protected]"; 
     $subject = "subject"; 
     $message = "this is the message body"; 
     $filename = "/home/user/file.jpeg"; 
     $fname = "file.jpeg"; 

     $headers = "From: $from"; 
     // boundary 
     $semi_rand = md5(time()); 
     $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; 

     // headers for attachment 
     $headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\""; 

     // multipart 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"; 
     $message .= "--{$mime_boundary}\n"; 

     // preparing attachments    
      $file = fopen($filename,"rb"); 
      $data = fread($file,filesize($filename)); 
      fclose($file); 
      $data = chunk_split(base64_encode($data)); 
      $message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"".$fname."\"\n" . 
      "Content-Disposition: attachment;\n" . " filename=\"$fname\"\n" . 
      "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n"; 
      $message .= "--{$mime_boundary}--\n"; 


     // send 
     //print $message; 

     $ok = @mail($to, $subject, $message, $headers, "-f " . $from);   
+0

如何附加多個圖像文件? @ mti2935 –

3

雖然有這樣的罐裝功能,對於初級程序員來說是多麼美妙的練習!

寫得很好mti2935。它會讓人們真正閱讀而不僅僅是剪切和粘貼。如果您使用php發送電子郵件,您應該瞭解這些基本概念。

可能從掩蔽你真正的代碼疏漏:

第23行應該是:

$data = fread($file,filesize($filename)); 

也就是說,$fname應該$filename

線26應該是:

$message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"".$fname."\"\n" . 

$x都不也不$files[$x]定義。

@Thomas Spade:我想提醒您,您應該始終清理輸入內容(電子郵件地址)。

0

,截止MIME分界線應該在--結束,所以線29應改爲:

$message .= "--{$mime_boundary}--\n";