PHP邮件图像附件损坏

时间:2015-07-04 10:10:23

标签: php email

我尝试发送带有图片附件的电子邮件,但是当我点击提交时,我确实收到了电子邮件,但它只发送了无数奇怪的代码而没有发送实际图像本身。由于用户应该上传图像,因此图像不在静态目录中。

<?php
if(isset($_POST['submit']))
{
//The form has been submitted, prep a nice thank you message
$output = '<h1>Thanks for your file and message!</h1>';
//Set the form flag to no display (cheap way!)
$flags = 'style="display:none;"';

//Deal with the email
$to = 'mymail@mail.com';
$subject = 'a file for you';

$message = strip_tags($_POST['message']);

$files = $_FILES['file']['file'];
$filename = $_FILES['file']['name'];    
$fp = fopen($files[$i],"rb");
$data = fread($fp,filesize($files[$i]));
fclose($fp);
$data = chunk_split(base64_encode($data));
$message .= "Content-Type: application/octet-stream; name=\"".basename($filename[$i])."\"\n" . 
                "Content-Description: ".basename($filename[$i])."\n" .
                "Content-Disposition: attachment;\n" . " filename=\"".basename($filename[$i])."\"; size=".filesize($files[$i]).";\n" . 
                "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";

$attachment = chunk_split(base64_encode(file_get_contents($_FILES['file']['tmp_name'])));
$filename = $_FILES['file']['name'];

$boundary =md5(date('r', time()));

$headers = "From: webmaster@example.com\r\nReply-To: mymail@mail.com";
$headers .= "\r\nMIME-Version: 1.0\r\nContent-Type: multipart/mixed; boundary=\"_1_$boundary\"";

$message="This is a multi-part message in MIME format.

--_1_$boundary
Content-Type: multipart/alternative; boundary=\"_2_$boundary\"

--_2_$boundary
Content-Type: text/plain; charset=\"iso-8859-1\"
Content-Transfer-Encoding: 7bit

$message

--_2_$boundary--
--_1_$boundary
Content-Type: application/octet-stream; name=\"$filename\"
Content-Transfer-Encoding: base64
Content-Disposition: attachment

$attachment
--_1_$boundary--";

mail($to, $subject, $message, $headers);
    }
?>
<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>MailFile</title>
</head>

<body>

<?php echo $output; ?>

<form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF'];?>" method="post" <?php echo $flags;?>>
<p><label for="message">Message</label> <textarea name="message" id="message" cols="20" rows="8"></textarea></p>
<p><label for="file">File</label> <input type="file" name="file" id="file"></p>
<p><input type="submit" name="submit" id="submit" value="send"></p>
</form>
</body>
</html>

1 个答案:

答案 0 :(得分:1)

使用PHP mailer。您可以毫不费力地在一行中执行电子邮件附件。

$email->AddAttachment();

以下是关于如何执行此操作的example。如果您需要将文件作为邮件附件上传,您可以执行以下操作:

if (isset($_FILES['uploaded_file']) &&
    $_FILES['uploaded_file']['error'] == UPLOAD_ERR_OK) {
    $mail->AddAttachment($_FILES['uploaded_file']['tmp_name'],
                         $_FILES['uploaded_file']['name']);
}

以下是AddAttachment定义:

public function AddAttachment($path,
                              $name = '',
                              $encoding = 'base64',
                              $type = 'application/octet-stream')

这是一个基本的example