2013-10-15 112 views
0

我試圖發送一封帶有多個附件的電子郵件......第一次嘗試此操作並卡住了,它成功發送帶有附件的電子郵件,但是作爲單個文件... 當然,如果我有兩個文件的100kb1.jpg100kb2.jpg它會用附件發送電子郵件作爲200kb 這裏一個文件就是我的代碼多個文件作爲一個文件附件發送

<html> 
<head> 
<title>Sending attachment using PHP</title> 
</head> 
<body> 
<?php 
    $to = "[email protected]"; 
    $subject = "This is subject"; 
    $message = "This is test message."; 
    # Open a file 
    $file = "http://bestwallpaperhd.com/wp-content/uploads/2013/03/hot-girl-wallpaper.jpg"; 

    $content = file_get_contents($file); 

    # encode the data for safe transit 
    # and insert \r\n after every 76 chars. 
    $encoded_content = chunk_split(base64_encode($content)); 
    $content1 = file_get_contents('http://www.albnews.al/wp-content/uploads/2013/08/99163-hot-girl_original.jpg'); 

    $encoded_content1 = chunk_split(base64_encode($content1)); 
    # Get a random 32 bit number using time() as seed. 
    $num = md5(time()); 

    # Define the main headers. 
    $header = "From:[email protected]\r\n"; 
    $header .= "MIME-Version: 1.0\r\n"; 
    $header .= "Content-Type: multipart/mixed; "; 
    $header .= "boundary=$num\r\n"; 
    $header .= "--$num\r\n"; 

    # Define the message section 
    $header .= "Content-Type: text/plain\r\n"; 
    $header .= "Content-Transfer-Encoding:8bit\r\n\n"; 
    $header .= "$message\r\n"; 
    $header .= "--$num\r\n"; 

    # Define the attachment section 
    $header .= "--$num--"; 
    $header .= "Content-Type: multipart/mixed; "; 
    $header .= "name=\"hotgirl.php\"\r\n"; 
    $header .= "Content-Transfer-Encoding:base64\r\n"; 
    $header .= "Content-Disposition:attachment; "; 
    $header .= "filename=\"hotgirl.jpg\"\r\n\n"; 
    $header .= "$encoded_content\r\n"; 
    $header .= "--$num--"; 

    $header .= "--$num--"; 
    $header .= "Content-Type: multipart/mixed; "; 
    $header .= "name=\"hotgirl123.php\"\r\n"; 
    $header .= "Content-Transfer-Encoding:base64\r\n"; 
    $header .= "Content-Disposition:attachment; "; 
    $header .= "filename=\"hotgirl123.jpg\"\r\n\n"; 
    $header .= "$encoded_content1\r\n"; 
    $header .= "--$num--"; 
    # Send email now 
    $retval = mail ($to, $subject, "", $header); 
    if($retval == true) 
    { 
     echo "Message sent successfully..."; 
    } 
    else 
    { 
     echo "Message could not be sent..."; 
    } 
?> 

+0

什麼問題?你解決了嗎? – realtebo

回答

0

後續腳本應該做你想做的事情。指定要在$files數組中發送的文件並填寫電子郵件字段。

<?php 

// array with filenames to be sent as attachment 
$files = array("file1.ext","file1.ext","file1.ext", ...); 

// email fields: to, from, subject, and so on 
$to = "[email protected]"; 
$from = "[email protected]"; 
$subject ="My subject"; 
$message = "My message"; 
$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 
for($x=0;$x<count($files);$x++){ 
    $file = fopen($files[$x],"rb"); 
    $data = fread($file,filesize($files[$x])); 
    fclose($file); 
    $data = chunk_split(base64_encode($data)); 
    $message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$files[$x]\"\n" . 
    "Content-Disposition: attachment;\n" . " filename=\"$files[$x]\"\n" . 
    "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n"; 
    $message .= "--{$mime_boundary}\n"; 
} 

// send  
$ok = @mail($to, $subject, $message, $headers); 
if ($ok) { 
    echo "<p>mail sent to $to!</p>"; 
} else { 
    echo "<p>mail could not be sent!</p>"; 
} 

?> 

source

相關問題