我是法國人,所以請原諒我的口音。 ;-) 我正在研究一個PHP腳本(based on AJAX Shopping Cart),因爲很多周,我無法找到正確的答案來解決我的問題。我解釋:我有一個拖放籃子來選擇圖片庫中的圖片。當用戶設置他的電子郵件並點擊一個按鈕時,他通過電子郵件收到他選擇的圖片。PHP數組+郵件附件
這是我的問題:如果籃子裏有3張照片,他會收到3封電子郵件,附有1張照片。如果有12 589張照片... 12 589電子郵件!
我希望有人能幫助我或解釋爲什麼它是錯的。
這是我(的一部分)代碼:
<?php
//////////////////////////////////////
// From Drag and Drop
//////////////////////////////////////
$cnt = array();
$products = array();
foreach($_POST as $key=>$value)
{
$key=(int)str_replace('_cnt','',$key);
$products[]=$key;
$cnt[$key]=$value;
}
$result = mysql_query("SELECT * FROM internet_shop WHERE id IN(".join($products,',').")");
if(!mysql_num_rows($result))
{
echo '<h2>Votre sélection est vide. Mais comment êtes vous arrivé(e) ici ? </h2><a href="contact.php">contactez-nous</a>';
}
else
{
echo '<h2>Votre sélection vous a été expédié à <span style="color:#4FACC1;">'.$_POST ['email'].'</span>.</h2>
<br/>
<h2 style="color:#4FACC1;">Détails de votre sélection</h2>
';
while($row=mysql_fetch_array($result))
{
//////////////////////////////////////
// Display selection
//////////////////////////////////////
echo '';
echo ' <span style="float:left;margin:10px;text-align: center;">
<img src="'.$row['chemin'].'/'.$row['img'].'"
alt="'.htmlspecialchars($row['name']).'"
width="128" height="128"
class="pngfix" />
<br/>
'.$row['name'].'
<b style="color:#4FACC1;font-size:9px;"> ('.$row['price'].' Ko)</b>
</span>';
$total += $cnt[$row['id']] * $row['price'];
//////////////////////////////////////
// Variable
//////////////////////////////////////
$sujet_reportage = str_replace('/', ' : ', $row['chemin']);
$sujet_reportage = str_replace('_', ' ', $sujet_reportage);
$withpoutthunb = str_replace('thumb_', '', $row['img']);
$Photos = $row['chemin'].'/'.$withpoutthunb;
$selection = 'Votre sélection photo : '.$withpoutthunb;
$to = $_POST ['email'];
$from = "www.xxxxx.com";
$subject = $selection;
$message = $sujet_reportage;
$headers = "From: $from";
//////////////////////////////////////
// array with filenames to be sent as attachment
//////////////////////////////////////
$files = array($Photos);
//////////////////////////////////////
// Frontière
//////////////////////////////////////
$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";
if($x == (count($files)-1))
{
$message .= "–{$mime_boundary}–";
}
else
{
$message .= "–{$mime_boundary}\n";
}
}
//////////////////////////////////////
// send
//////////////////////////////////////
$ok = @mail($to, $subject, $message, $headers);
//////////////////////////////////////
// End of while
//////////////////////////////////////
}
非常感謝。
對'mail()'的調用在查看每條記錄的while循環中。你的目標是什麼?你想在1封電子郵件中發送所有圖片嗎?只需在循環中構建一條消息,一旦退出循環,就調用mail()。 – drew010 2012-04-24 18:18:14
考慮使用PHP的郵件庫,有很多可供選擇的選項,這會在您的郵件被標記爲垃圾郵件或其他討厭郵件時在稍後爲您節省時間。 – Jakub 2012-04-25 14:52:27