简体   繁体   English

PHP不发送带有附件的邮件

[英]PHP not sending mail with attachment

I've got an HTML sign up/contact form that currently sends the fields in an email, no problem. 我有一个HTML注册/联系表单,当前可以通过电子邮件发送字段,没问题。 The problem I'm having is attaching an uploaded file to the email. 我遇到的问题是将上传的文件附加到电子邮件。

With the PHP I'm using at the moment for the file, the email is sending, but the MIME information is within the email and I got an error indicating that the message is 'badly structured and could not be fully examined' and the file isn't attached to the email. 使用我目前使用的PHP来发送文件,电子邮件正在发送,但是MIME信息在电子邮件中,并且我收到一条错误消息,指示该消息“结构不良,无法完全检查”,并且该文件未附加到电子邮件中。

<?php
if (isset($_POST['submit'])) {

    //if ($_SERVER['submit']=="POST"){

    $to = "me@mymail.com";                           // Email address
    $from = $_POST['text_96'];                        // sender's Email
    $fullname = $_POST['text_95'];                    // users fullname
    $email = $_POST['text_96'];                        // users email address
    $dob = $_POST['date_12'];                            // Date of birth
    $addr = $_POST['textarea_31'];                    // Address
    $maths = $_POST['text_93'];                        // Maths 
    $english = $_POST['text_31'];                        // English 
    $holidayst = $_POST['date_74'];                    // Holiday1
    $holidayend = $_POST['date_91'];                    // Holiday2
    $distance = $_POST['number_58'];                    // Dist
    $awayhome = $_POST['selectlist_90'];                // AFW [boolean]
    $reasontext = $_POST['textarea_60'];                // 100 word answer
    $subject = "New application submission";           // subject of email

    $mssg = "New submission from " . $email . "\n\n" . "Full name: " . $fullname . "\nDOB: " . $dob . "\nAddress:\n" . $addr . "\n\nMaths: " . $maths . "\nEnglish: " . $english . "\nHoliday start: " . $holidayst . "\nHoliday end: " . $holidayend . "\nDistance to travel: " . $distance . "km" . "\nAFH?: " . $awayhome . "\nAppr choices: " . $apprent . "\n\nReason for applying: " . $reasontext . "\n";

    foreach ($_POST['checkbox'] as $key => $val) {        // Gets list of appr
        $apprent .= $val . ' ';                        // 
    }

    //  --  --  --  \\

    $fileatt = $_FILES['file'];
    //$fileatttype = "application/pdf";
    //$fileattname = "newname.pdf";
    $headers = "From: $from";
    $file = fopen($fileatt, 'rb');
    $data = fread($file, filesize($fileatt));
    fclose($file);
    $semi_rand = md5(time());
    $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

    $headers .= "\nMIME-Version: 1.0\n" .
        "Content-Type: multipart/mixed;\n" .
        " boundary=\"{$mime_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";

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

    $message .= "–{$mime_boundary}\n" .
        "Content-Type: {$fileatttype};\n" .
        " name=\"{$fileattname}\"\n" .
        "Content-Disposition: attachment;\n" .
        " filename=\"{$fileattname}\"\n" .
        "Content-Transfer-Encoding: base64\n\n" . $mssg .

        $data . "\n\n" .
        "-{$mime_boundary}-\n";

    if (mail($to, $subject, $message, $headers)) {
        $message = NULL;
        $headers = NULL;
        header('Location: application_submitted.html');
    } else {
        header('Location: index.php');

        //\\    --  --  --  //

        //$headers = "From:" . $from;
        //@mail($to,$subject,$message,$headers);
        // You can also use header('Location: thank_you.php'); to redirect to another page.
        //$message = NULL;
        //$headers = NULL;
        //header('Location: application_submitted.html');

        //if (@mail($to, $subject, $message, $headers))
        //$message = NULL;
        //$headers = NULL;
        //header('Location: application_submitted.html');
        //else
        //echo "Failed to send";
    }
}
?>

How can I attach a file to this email and send it without getting the error explained above, please? 我如何在此电子邮件中附加文件并将其发送,而不会出现上述错误?

Many thanks for any help and assistance with this! 非常感谢您对此提供的帮助和协助!

Your code is too broken to be fixed here, but here are two main errors for you to focus on: 您的代码太破损,无法在此处进行修复,但是您要注意以下两个主要错误:

  1. The referencing of the uploaded file is incorrect 上载文件的引用不正确
  2. Your headers are not separated properly. 标头未正确分隔。

Firstly, you do not reference an uploaded file using $_FILES['file'] , its location is stored in $_FILES['file']['tmp_name'] . 首先,您不使用$_FILES['file']引用上传的文件,其位置存储在$_FILES['file']['tmp_name'] Here is a useful tutorial: PHP 5 File Upload on W3schools . 这是一个有用的教程: W3schools上的PHP 5文件上传

Secondly, headers should be separated using \\r\\n . 其次,应使用\\r\\n分隔标题。 The mail() function documentation provides useful information on this, along with security tips. mail()函数文档提供了有关此方面的有用信息以及安全提示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM