2013-03-18 102 views
0

晚上好,我在写信,因为我有问题要将使用表单加载的文件附加到电子邮件中。 我不明白,如果我必须保存之前,将其附加到文件夹或不.... 这是我的代码,邮件到达,但没有附件。有人告诉我我错在哪里?附件中的邮件php

$allegato=$_FILES['userfile']['tmp_name']; 
$allegato_name=$_FILES['userfile']['name']; 
$allegato_tipo=$_FILES['userfile']['type']; 
$uploaddir = '/uploads/'; 
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']); 
$headers = 'From: '.$email.'' . "\r\n" . 
     'Reply-To: [email protected]' . "\r\n" . 
     'X-Mailer: PHP/' . phpversion(); 

      ; 

if ($_FILES["userfile"]["error"] > 0){ 
     echo "Return Code: " . $_FILES["userfile"]["error"] . "<br>"; 
    }else{ 
    if (file_exists("uploads/" . $_FILES["userfile"]["name"])){ 
     echo $_FILES["userfile"]["name"] . " already exists. "; 
    }else{ 
     move_uploaded_file($_FILES["userfile"]["tmp_name"], 
     "uploads/" . $_FILES["userfile"]["name"]); 
     echo "Stored in: " . "uploads/" . $_FILES["userfile"]["name"]; 
    } 
    } 
    if(is_uploaded_file($allegato)){ 
     $file = fopen($allegato,'rb'); 
     $data = fread($file, filesize($allegato)); 
     fclose($file); 

     $data = chunk_split(base64_encode($data)); 

     $semi_rand = md5(time()); 
     $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; 

     $headers .= "\nMIME-Version: 1.0\n"; 
     $headers .= "Content-Type: multipart/mixed;\n"; 
     $headers .= " boundary=\"{$mime_boundary}\""; 

     $msg .= "This is a multi-part message in MIME format.\n\n"; 

     $msg .= "--{$mime_boundary}\n"; 

     $msg .= "Content-Type: text/plain; charset=\"iso-8859-1\"\n"; 
     $msg .= "Content-Transfer-Encoding: 7bit\n\n"; 
     $msg .= $messaggio . "\n\n"; 

     $msg .= "--{$mime_boundary}\n"; 

     $msg .= "Content-Disposition: attachment;\n"; 
     $msg .= " filename=\"{$allegato_name}\"\n"; 
     $msg .= "Content-Transfer-Encoding: base64\n\n"; 
     $msg .= $data . "\n\n"; 

     $msg .= "--{$mime_boundary}--\n 
    }else{ 
     $msg = $messaggio; 
    } 

     if (mail($destinatario, $oggetto, $msg, $headers)){ 
      echo "<p>Mail inviata con successo!</p>"; 
     }else{ 
      echo "<p>Errore!</p>"; 
     } 

/好的电影剧本/

 mail($destinatario, $oggetto, $messaggio, $headers) ; 
+0

_请为此使用邮件类。电子邮件是一个相当复杂的主题,如果一个人不知道自己在做什么,试图将所有必要的部分“手工”并使用PHP邮件()都很困难。 – CBroe 2013-03-18 15:15:24

+0

任何你不使用PHPMailer之类的理由? – Pitchinnate 2013-03-18 15:15:30

+0

由于您将文件的内容放入邮件中,因此您无需移动或存储它。像Pitchinnate我使用PHPMailer,它允许我轻松地附加或嵌入。 – Waygood 2013-03-18 15:17:23

回答

0

我推荐使用PHPMailer当你进入附件。一个简单的安装例:

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

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

    $mail -> IsSendmail(); 
    // telling the class to use SendMail transport 

    $body = file_get_contents('contents.html'); 
    $body = preg_replace('/[\]/i', '', $body); 

    $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 Sendmail, 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

好的谢谢,现在我试试 – 2013-03-18 15:20:43

+0

对于OP他们只需要$ mail - > AddStringAttachment(file_get_contents($ _ FILES [“userfile”] [“tmp_name”]),$ _FILES [ “userfile的”] [ “名称”]);添加文件并保留原名 – Waygood 2013-03-18 15:22:02

+0

是的,我明白了,现在所有的作品!感谢所有......最终的问题是定义我想要得到的文件的类型....在phpmailer中有一些函数可以让你过滤? – 2013-03-18 15:56:13