2010-06-20 130 views
1

您好我需要幫助下面的腳本發送附件,我相當新的PHP和不能似乎得到的附件工作,電子郵件確實發送,但我認爲heaaders是腐敗和返回garbge作爲反對附加文件PHP發送多個附件與電子郵件

$to  = $_SESSION['companymail']; 

    $email = $_POST['email']; 
    $name = $_POST['fullname']; 
    $subject = $subject; 
    $comment = $message; 

    $To   = strip_tags($to); 
    $TextMessage =strip_tags(nl2br($comment),"<br>"); 
    $HTMLMessage =nl2br($comment); 
    $FromName =strip_tags($name); 
    $FromEmail =strip_tags($email); 
    $Subject  =strip_tags($subject); 

    $boundary1 =rand(0,9)."-" 
    .rand(10000000000,9999999999)."-" 
    .rand(10000000000,9999999999)."=:" 
    .rand(10000,99999); 
    $boundary2 =rand(0,9)."-".rand(10000000000,9999999999)."-" 
    .rand(10000000000,9999999999)."=:" 
    .rand(10000,99999); 

    for($i=0; $i < count($_FILES['fileatt']['name']); $i++){ 

     if(is_uploaded_file($_FILES['fileatt']['tmp_name'][$i]) && 
     !empty($_FILES['fileatt']['size'][$i]) && 
     !empty($_FILES['fileatt']['name'][$i])){ 

      $attach  ='yes'; 
      $end   =''; 
      $handle  =fopen($_FILES['fileatt']['tmp_name'][$i], 'rb'); 
      $f_contents =fread($handle, $_FILES['fileatt']['size'][$i]); 
      $attachment[]=chunk_split(base64_encode($f_contents)); 
      fclose($handle); 

      $ftype[]  =$_FILES['fileatt']['type'][$i]; 
      $fname[]  =$_FILES['fileatt']['name'][$i]; 

     } 

    } 



/*************************************************************** 

HTML Email WIth Multiple Attachment <<----- Attachment ------ 

***************************************************************/ 



if($attach=='yes') { 



$attachments=''; 

$Headers  =<<<AKAM 

From: $FromName <$FromEmail> 

Reply-To: $FromEmail 

MIME-Version: 1.0 

Content-Type: multipart/mixed; 

boundary="$boundary1" 

AKAM; 



for($j=0;$j<count($ftype); $j++){ 

$attachments.=<<<ATTA 

--$boundary1 

Content-Type: $ftype[$j]; 

name="$fname[$i]" 

Content-Transfer-Encoding: base64 

Content-Disposition: attachment; 

filename="$fname[$j]" 



$attachment[$j] 



ATTA; 

} 



$Body  =<<<AKAM 

This is a multi-part message in MIME format. 



--$boundary1 

Content-Type: multipart/alternative; 

boundary="$boundary2" 



--$boundary2 

Content-Type: text/plain; 

charset="windows-1256" 

Content-Transfer-Encoding: quoted-printable 



$TextMessage 

--$boundary2 

Content-Type: text/html; 

charset="windows-1256" 

Content-Transfer-Encoding: quoted-printable 



$HTMLMessage 



--$boundary2-- 



$attachments 

--$boundary1-- 

AKAM; 

} 





    /*************************************************************** 

    Sending Email 

    ***************************************************************/ 

    $ok=mail($To, $Subject, $Body, $Headers); 

回答

3

我不建議推倒重來,嘗試使用免費提供類較高的聲譽。

退房PHPMailer

require_once('../class.phpmailer.php'); 

$mail    = new PHPMailer(); // defaults to using php "mail()" 

$body    = file_get_contents('contents.html'); 
$body    = eregi_replace("[\]",'',$body); 

$mail->AddReplyTo("[email protected]","First Last"); 

$mail->SetFrom('[email protected]', 'First Last'); 

$mail->AddReplyTo("[email protected]","First Last"); 

$address = "[email protected]"; 
$mail->AddAddress($address, "John Doe"); 

$mail->Subject = "PHPMailer Test Subject via mail(), basic"; 

$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test 

$mail->MsgHTML($body); 

$mail->AddAttachment("images/phpmailer.gif");  // attachment 
$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment 

if(!$mail->Send()) { 
    echo "Mailer Error: " . $mail->ErrorInfo; 
} else { 
    echo "Message sent!"; 
} 
+0

謝謝您的回答,你怎麼可以使用命令行$ MAIL-> AddAttachment( 「圖像/ phpmailer.gif」); // attachment $ mail-> AddAttachment(「images/phpmailer_mini.gif」);通過直接從表單文章獲取文件詳細信息,在我的情況下,我想處理多個附件 – tom 2010-06-20 14:11:40

+1

只需使用從$ _FILES獲得的輸入,如 '$ mail-> AddAttachment($ _ FILES [「file1」] [「tmp_name」 ],$ _ FILES [ 「文件1」] [ 「名稱」])' – Pentium10 2010-06-20 15:01:47