2012-04-24 10 views
1

私はフランス語ですので、私のアクセントになります。 ;-) 私は何週間もPHPスクリプト(based on AJAX Shopping Cart)に取り組んでおり、私の問題に対する正解を見つけることができません。私は説明します:イメージバンクから画像を選択するドラッグアンドドロップバスケットがあります。ユーザが電子メールを設定してボタンをクリックすると、彼は電子メールで自分の写真を受け取る。PHP配列+メールいくつかの部分が添付されています

私の問題は:バスケットに3枚の写真がある場合、彼は電子メールで1枚の写真3枚を受け取ります。 12 589の写真がある場合... 12 589 email !!

誰かが私を助けたり、なぜそれが間違っているのか説明してくれることを願っています。

<?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

+0

PHP用のメールライブラリを使用することを検討してください。かなりの数があります。後でメールが迷惑メールやその他の迷惑行為としてマークされてしまったときに頭痛を軽減します。 – Jakub

答えて

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'; 
} 

おかげでたくさん!

大丈夫

関連する問題