0

我正在尝试发送带有多个附件的电子邮件...第一次尝试并卡住了,它成功发送了带有附件的电子邮件,但作为单个文件...例如,如果 我1.jpg有两个100kb文件将带有附件的电子邮件作为单个文件发送, 这就是我的代码2.jpg100kb200kb

<html>
<head>
<title>Sending attachment using PHP</title>
</head>
<body>
<?php
  $to = "abc@ex.com";
  $subject = "This is subject";
  $message = "This is test message.";
  # Open a file
  $file = "http://bestwallpaperhd.com/wp-content/uploads/2013/03/hot-girl-wallpaper.jpg";

  $content = file_get_contents( $file);

  # encode the data for safe transit
  # and insert \r\n after every 76 chars.
  $encoded_content = chunk_split( base64_encode($content));
  $content1 = file_get_contents('http://www.albnews.al/wp-content/uploads/2013/08/99163-hot-girl_original.jpg');

  $encoded_content1 = chunk_split( base64_encode($content1));
  # Get a random 32 bit number using time() as seed.
  $num = md5( time() );

  # Define the main headers.
  $header = "From:admin@hotesttube.com\r\n";
  $header .= "MIME-Version: 1.0\r\n";
  $header .= "Content-Type: multipart/mixed; ";
  $header .= "boundary=$num\r\n";
  $header .= "--$num\r\n";

  # Define the message section
  $header .= "Content-Type: text/plain\r\n";
  $header .= "Content-Transfer-Encoding:8bit\r\n\n";
  $header .= "$message\r\n";
  $header .= "--$num\r\n";

  # Define the attachment section
  $header .= "--$num--";
  $header .= "Content-Type:  multipart/mixed; ";
  $header .= "name=\"hotgirl.php\"\r\n";
  $header .= "Content-Transfer-Encoding:base64\r\n";
  $header .= "Content-Disposition:attachment; ";
  $header .= "filename=\"hotgirl.jpg\"\r\n\n";
  $header .= "$encoded_content\r\n";
  $header .= "--$num--";

  $header .= "--$num--";
  $header .= "Content-Type:  multipart/mixed; ";
  $header .= "name=\"hotgirl123.php\"\r\n";
  $header .= "Content-Transfer-Encoding:base64\r\n";
  $header .= "Content-Disposition:attachment; ";
  $header .= "filename=\"hotgirl123.jpg\"\r\n\n";
  $header .= "$encoded_content1\r\n";
  $header .= "--$num--";
  # Send email now
  $retval = mail ( $to, $subject, "", $header );
  if( $retval == true )
   {
      echo "Message sent successfully...";
   }
   else
   {
      echo "Message could not be sent...";
   }
?>

4

1 回答 1

0

下面的脚本应该做你想做的事。指定要在$files数组中发送的文件并填写电子邮件字段。

<?php

// array with filenames to be sent as attachment
$files = array("file1.ext","file1.ext","file1.ext", ...);

// email fields: to, from, subject, and so on
$to = "mail@mail.com";
$from = "mail@mail.com"; 
$subject ="My subject"; 
$message = "My message";
$headers = "From: $from";

// boundary 
$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";
    $message .= "--{$mime_boundary}\n";
}

// send    
$ok = @mail($to, $subject, $message, $headers); 
if ($ok) { 
    echo "<p>mail sent to $to!</p>"; 
} else { 
    echo "<p>mail could not be sent!</p>"; 
} 

?>

资源

于 2013-10-15T11:09:42.793 回答