2012-04-24 128 views
1

我是法國人,所以請原諒我的口音。 ;-) 我正在研究一個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 
////////////////////////////////////// 

} 

非常感謝。

+2

對'mail()'的調用在查看每條記錄的while循環中。你的目標是什麼?你想在1封電子郵件中發送所有圖片嗎?只需在循環中構建一條消息,一旦退出循環,就調用mail()。 – drew010 2012-04-24 18:18:14

+0

考慮使用PHP的郵件庫,有很多可供選擇的選項,這會在您的郵件被標記爲垃圾郵件或其他討厭郵件時在稍後爲您節省時間。 – Jakub 2012-04-25 14:52:27

回答

0

正如我所提到的使用圖書館,使容易對自己的事情: http://swiftmailer.org/

然後你想創建代碼,以便與附件:

require_once 'lib/swift_required.php'; 

// Create the message 
$message = Swift_Message::newInstance() 

    // Give the message a subject 
    ->setSubject('Your subject') 

    // Set the From address with an associative array 
    ->setFrom(array('[email protected]' => 'John Doe')) 

    // Set the To addresses with an associative array 
    ->setTo(array('[email protected]', '[email protected]' => 'A name')) 

    // Give it a body 
    ->setBody('Here is the message itself') 

    // And optionally an alternative body 
    ->addPart('<q>Here is the message itself</q>', 'text/html') 

    // Optionally add any attachments 
    ->attach(Swift_Attachment::fromPath('my-document.pdf')) 
    ; 

參考:http://swiftmailer.org/docs/messages.html

多年後,我自己創建郵件功能後,最好堅持一個偉大的圖書館來完成這項工作。

對於你的問題,你只是重複附件的圖片:

->attach(Swift_Attachment::fromPath('image1.jpg')) 
->attach(Swift_Attachment::fromPath('image2.jpg')) 
->attach(Swift_Attachment::fromPath('image3.jpg')) 
0

感謝您的幫助!我終於使用了http://swiftmailer.org/ lib。它非常完美!

要發送的多連接郵件與swiftmailer,我喜歡這樣(後爲那些誰搜索):

////////////////////////////////////// 
// Appel de la librairie Mail 
////////////////////////////////////// 

require_once 'lib/swift_required.php'; 

... votre appel en base (ou autre) 

////////////////////////////////////// 
// array with filenames to be sent as attachment 
////////////////////////////////////// 

$fichiers[] = $Photos; 

////////////////////////////////////// 
// DECLARE LES VARIABLES 
////////////////////////////////////// 

$mail = $_POST ['email']; 
$from = "Your name"; 

////////////////////////////////////// 
// Fermeture du while 
////////////////////////////////////// 


} 

////////////////////////////////////// 
// Construction du message 
////////////////////////////////////// 

$message = Swift_Message::newInstance() 

////////////////////////////////////// 
// Sujet du mail 
////////////////////////////////////// 

->setSubject('Votre sujet') 

////////////////////////////////////// 
// De 
////////////////////////////////////// 

->setFrom(array('[email protected]' => 'www.xxxx.xx')) 

////////////////////////////////////// 
// A 
////////////////////////////////////// 

->setTo($mail) 

////////////////////////////////////// 
// Contenu du mail 
////////////////////////////////////// 

->setBody($votre_contenu) 

////////////////////////////////////// 
// Contenu du mail (alternatif) 
////////////////////////////////////// 

->addPart('<q>'.$votre_contenu.'</q>', 'text/html') 
; 

////////////////////////////////////// 
// Pieces jointes multiples 
////////////////////////////////////// 

foreach ($fichiers as $fichier) 
{ 
$message->attach(Swift_Attachment::fromPath($fichier)) ; 
} 

////////////////////////////////////// 
// Création du "moyen de transport" 
////////////////////////////////////// 

$transport = Swift_MailTransport::newInstance(); 

////////////////////////////////////// 
// Envoi du mail 
////////////////////////////////////// 

$mailer = Swift_Mailer::newInstance($transport); 
$result = $mailer->send($message); 

////////////////////////////////////// 
// Réponse de l'envoi 
////////////////////////////////////// 

if($result) 
{ 
echo '<h2>Votre sélection vous a été expédié à '.$_POST ['email'].'.</h2> 
<br/>'; 
} 
else 
{ 
echo 'Erreur'; 
} 

非常感謝大家!

Regards