【问题标题】:attachment in a mail php邮件中的附件 php
【发布时间】:2013-03-18 15:11:29
【问题描述】:

大家晚上好,我写信是因为我无法将使用表单加载的文件附加到电子邮件中。 我不明白在将其附加到文件夹之前是否必须保存.... 这是我的代码,邮件到达,但没有附件。有人告诉我我哪里错了吗?

$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: pir.stefania@tiscali.it' . "\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) ;   

【问题讨论】:

  • 为此使用邮件类。电子邮件是一个相当复杂的主题,如果不完全了解自己在做什么,那么尝试“手动”将所有必要的部分放在一起并使用 PHP mail() 是很困难的。
  • 有什么理由不使用 PHPMailer 之类的东西?
  • 由于您将文件的内容放入邮件中,您无需移动或存储它。像 Pitchinnate 一样,我使用 PHPMailer,它可以让我轻松附加或嵌入。
  • 好吧,你说服了我,现在我尝试使用 php mailer

标签: php forms file-upload email-attachments


【解决方案1】:

我建议您在进入附件时使用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('name@yourdomain.com', 'First Last');

    $mail -> AddReplyTo("name@yourdomain.com", "First Last");

    $address = "whoto@otherdomain.com";
    $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!";
    }
?>

【讨论】:

  • 对于 OP,他们只需要 $mail -> AddStringAttachment(file_get_contents($_FILES["userfile"]["tmp_name"]), $_FILES["userfile"]["name"]);添加文件并保留原始名称
  • 是的,我明白了,现在一切正常!非常感谢......最终的问题是定义我想要获取的文件类型...... phpmailer中有一些功能可以让您过滤?
【解决方案2】:

如果您使用像 Swiftmailer 这样的 Mailer 类来处理如此复杂的任务,那将是最好的。

SwiftMailer 提供了一个名为attach() 的函数来将文件附加到电子邮件中。 (http://swiftmailer.org/docs/messages.html)

【讨论】:

    猜你喜欢
    • 2018-08-13
    • 2012-06-08
    • 1970-01-01
    • 1970-01-01
    • 2011-09-28
    • 2011-03-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多