PHP 数组 + 邮件附加了几个部分


PHP array + Mail several parts attached

我是法国人,所以请原谅我的口音。我已经在处理PHP脚本(基于AJAX购物车)好几个星期了,但我找不到我的问题的正确答案。我解释说:我有一个拖放篮,可以从图像库中选择图片。当用户设置他的电子邮件并单击按钮时,他会通过电子邮件收到他选择的图片。

这是我的问题:如果篮子里有 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
//////////////////////////////////////
}

多谢。

正如我提到的,使用库来简化自己的工作: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'))

感谢您的帮助!我终于使用了 http://swiftmailer.org/库。它工作得很好!

要使用 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';
}

非常感谢大家!

问候