简体   繁体   English

尝试使用 PHP mail() 发送附件

[英]Trying to send attachments with PHP mail()

I'm trying to store an uploaded file from a form to the uploads folder as well as send the file as an attachment to an email.我正在尝试将上传的文件从表单存储到上传文件夹,并将该文件作为电子邮件的附件发送。 The email sends perfectly fine and there is also an attachment, however the attachment is unreadable/corrupted.电子邮件发送得很好,还有一个附件,但是附件无法读取/损坏。

I have tried with multiple file types and that makes no difference, I still have the same errors.我尝试了多种文件类型,但没有任何区别,我仍然遇到相同的错误。

    <?php

//Store File
if (!empty($_FILES) && isset($_FILES['upload'])) {
    switch ($_FILES['upload']["error"]) {
        case UPLOAD_ERR_OK:
            $target = "uploads/";
            $target = $target . basename($_FILES['upload']['name']);

            if (move_uploaded_file($_FILES['upload']['tmp_name'], $target)) {
                $status        = "The file " . basename($_FILES['upload']['name']) . " has been uploaded";
                $imageFileType = pathinfo($target, PATHINFO_EXTENSION);
            } else {
                $status = "Sorry, there was a problem uploading your file.";
            }
            break;

    }

    echo "Status: {$status}<br/>\r\n";

}


$fileatt      = getcwd() . "/uploads"; // Path to the file
$fileatt_type = "application/pdf"; // File Type
$fileatt_name = $_FILES['upload']['name']; // Filename that will be used for the file as the attachment

$email_from    = $_POST['email']; // Who the email is from
$email_subject = "Your attached file"; // The Subject of the email
$email_message = "Thanks for visiting mysite.com! Here is your free file.";
$email_message .= "Thanks for visiting."; // Message that the email has in it

$email_to = 'test@test.com'; // Who the email is to

$headers = "From: " . $email_from;

$file = fopen($fileatt . "/" . $fileatt_name, 'rb');
$data = fread($file, filesize($fileatt . "/" . $fileatt_name));
fclose($file);

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

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

$email_message .= "This is a multi-part message in MIME format.\r\n\r\n" . "--{$mime_boundary}\r\n" . "Content-Type:text/html; charset=\"iso-8859-1\"\r\n" . "Content-Transfer-Encoding: 7bit\r\n\r\n" . $email_message .= $_POST['email'] . "<br>" . $_POST['firstname'] . "<br>" . $_POST['surname'] . "<br>" . $_POST['degree'] . "<br>" . "\r\n\r\n";

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

$email_message .= "--{$mime_boundary}\r\n" . "Content-Type: {$fileatt_type};\r\n" . " name=\"{$fileatt_name}\"\r\n" . "Content-Disposition: attachment;\r\n" . " filename=\"{$fileatt_name}\"\r\n" . "Content-Transfer-Encoding: base64\r\n\r\n" . $data .= "\r\n\r\n" . "--{$mime_boundary}--\r\n";

$ok = mail($email_to, $email_subject, $email_message, $headers);

if ($ok) {
    echo "You file has been sent
    to the email address you specified.

    Make sure to check your junk mail!

    Click here to return to mysite.com.";

} else {
    die("Sorry but the email could not be sent. Please go back and try again!");
}


?>

Please could somebody point me in the right direction?请有人能指出我正确的方向吗?

You can find the solution here.您可以在此处找到解决方案。

Explained completely to need to ass same info again.完全说明需要再次输入相同的信息。

Solution 解决方案

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

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