没有发送附件的邮件(奇怪的情况)?

时间:2013-04-20 18:48:35

标签: php

此代码将html文件作为附件发送正常。但是在下一个代码片段中,当我将附件添加到图像时,它没有被发送。为什么会这样呢?我不想使用phpmailer或swift邮件!

<?php
$file_path = "file.html"; // server path where file is placed
$file_path_type = "text/html"; // File Type
$file_path_name = "newfilename.html"; // this file name will be used at reciever end 

$from = "xyz@gmail.com"; // E-mail address of sender
$to = "abc@gmail.com"; // E-mail address of reciever
$subject = "Please check the Attachment."; // Subject of email
$message = "This is the message body.&lt;br&gt;&lt;br&gt;Thank You!&lt;br&gt;&lt;a href='http://7tech.co.in'&gt;7tech.co.in Team&lt;/a&gt;"; 

$headers = "From: ".$from; 

$file = fopen($file_path,'rb');
$data = fread($file,filesize($file_path));
fclose($file); 

$rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$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/html; 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: {$file_path_type};\n" .
" name=\"{$file_path_name}\"\n" .
"Content-Disposition: attachment;\n" .
" filename=\"{$file_path_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data .= "\n\n" .
"--{$mime_boundary}--\n";  

if(@mail($to, $subject, $message, $headers)) {
echo "File send!";

} else {
echo 'Failed';
}
?>

现在,当我将附件更改为图像即screenshot.png时,它无法发送消息。

<?php
    $file_path = "screenshot.png"; // server path where file is placed
    $file_path_type = "image/png"; // File Type
    $file_path_name = "screenshot.png"; // this file name will be used at reciever end 

    $from = "xyz@gmail.com"; // E-mail address of sender
    $to = "abc@gmail.com"; // E-mail address of reciever
    $subject = "Please check the Attachment."; // Subject of email
    $message = "This is the message body.&lt;br&gt;&lt;br&gt;Thank You!&lt;br&gt;&lt;a href='http://7tech.co.in'&gt;7tech.co.in Team&lt;/a&gt;"; 

    $headers = "From: ".$from; 

    $file = fopen($file_path,'rb');
    $data = fread($file,filesize($file_path));
    fclose($file); 

    $rand = md5(time());
    $mime_boundary = "==Multipart_Boundary_x{$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/html; 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: {$file_path_type};\n" .
    " name=\"{$file_path_name}\"\n" .
    "Content-Disposition: attachment;\n" .
    " filename=\"{$file_path_name}\"\n" .
    "Content-Transfer-Encoding: base64\n\n" .
    $data .= "\n\n" .
    "--{$mime_boundary}--\n";  

    if(@mail($to, $subject, $message, $headers)) {
    echo "File send!";

    } else {
    echo 'Failed';
    }
    ?>

你们可以指出错误吗。我曾试图在1-2个地方改变内容类型,但它没有用。我什么都错过了?

1 个答案:

答案 0 :(得分:0)

使用

file_get_contents('http://www.example.com/');

而不是

$file = fopen($file_path,'rb');
$data = fread($file,filesize($file_path));
fclose($file); 

并尝试使用此代码..

$random_hash = md5(time());

$headers .= "\r\nContent-Type: multipart/alternative; boundary=\"PHP-alt-".$random_hash."\"";

$headers .= "\r\nMIME-Version 1.0";

$attachment = chunk_split(base64_encode(file_get_contents($filename)));

$message = 
"--PHP-alt-$random_hash
Content-Type: text/plain

Dear Same,

We would like to thank you for your registration to be held on Saturday August 25, 2012 at the....

--PHP-alt-$random_hash
Content-Type: application/pdf; name=$filename
Content-Transfer-Encoding: base64
Content-Disposition: attachment

$attachment
--PHP-alt-$random_hash--";

@mail($to, $subject, $message, $headers);
相关问题