1

私はフランス語なので、私のアクセントを許してください. ;-) 何週間も前から PHP スクリプト ( AJAX ショッピング カートに基づく) に取り組んでいますが、問題に対する正しい答えが見つかりません。私は説明します:画像バンクから写真を選択するためのドラッグアンドドロップバスケットがあります。ユーザーがメールを設定してボタンをクリックすると、選択した写真をメールで受け取ります。

ここに私の問題があります:バスケットに3枚の写真がある場合、彼は1枚の写真を含む3通の電子メールを電子メールで受け取ります。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
//////////////////////////////////////

}

どうもありがとう。

4

2 に答える 2

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('xx@xxxx.com' => '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';
}

どうもありがとうございました!

よろしく

于 2012-04-25T16:26:01.817 に答える
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('john@doe.com' => 'John Doe'))

  // Set the To addresses with an associative array
  ->setTo(array('receiver@domain.org', 'other@domain.org' => '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'))
于 2012-04-25T15:03:52.253 に答える