0

好的,php4 - 我明白了。也就是说,由于许多原因,无法升级到 php5 并使用 phpMailer - 就像我想的那样。我四处搜寻,没有其他答案。所以,这就是问题所在。我创建了一个文本文件 /tmp2/tmporder2

下面的代码可以很好地发送带有附件的电子邮件,但附件是 0​​ 字节,NADA。

<?php
$path = '/tmp2';
$filename = 'tmporder2';
$mailto = 'dave@powerfulhosting.com';
$from_mail = 'estore order receipt';
 $from_name = 'bagelworks order';
 $replyto = '';
 $subject = 'TESTING EMAIL ATTACHMENT';

    $file = $path.$filename;
    $file_size = filesize($file);
    $handle = fopen($file, "r");
    $content = fread($handle, $file_size);
    fclose($handle);
    $message=$handle;

    $content = chunk_split(base64_encode($content));
    $uid = md5(uniqid(time()));
    $name = basename($file);
    $header = "From: ".$from_name." <".$from_mail.">\r\n";
    $header .= "Reply-To: ".$replyto."\r\n";
    $header .= "MIME-Version: 1.0\r\n";
    $header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
    $header .= "This is a multi-part message in MIME format.\r\n";
    $header .= "--".$uid."\r\n";
    $header .= "Content-type:text/plain; charset=iso-8859-1\r\n";
    $header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
    $header .= $message."\r\n\r\n";
    $header .= "--".$uid."\r\n";
    $header .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n"; // use different content types here
    $header .= "Content-Transfer-Encoding: base64\r\n";
    $header .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
    $header .= $content."\r\n\r\n";
    $header .= "--".$uid."--";
    if (mail($mailto, $subject, "", $header)) {
        echo "mail send ... OK"; // or use booleans here
    } else {
        echo "mail send ... ERROR!";
    }
?>

任何想法,将不胜感激。

4

1 回答 1

0

从我的服务器的根目录尝试您的脚本时,使用:

$path = '/tmp2/';
$filename = 'tmporder2.txt'; // I renamed it with an `.txt` extension to test

它没有发送它并收到许多错误。

但是,当我测试相同的脚本但/在开头删除tmp2并在末尾添加 a/时,例如:

$path = 'tmp2/';
$filename = 'tmporder2.txt';

它工作并向我发送了一封附有文件的电子邮件。

尝试从root您的服务器运行我的示例代码,创建一个名为的子文件夹tmp2并在其中插入tmporder2.txt任何类型的文本,然后再试一次。

代码中的额外错误

我还注意到您的代码中还有一些错误。

例如,$message=$handle;如果要作为附件发送,这行 That 不应该是您的邮件的一部分。

  • 这条线if (mail($mailto, $subject, "", $header)) {

第三个参数是消息正文进入的位置。

我将它替换if (mail($mailto, $subject, $message, $header)) {$message现在$message = "Hello there";将出现在正文中的位置。我收到一条错误消息:

警告:mail() 期望参数 3 为字符串


欢迎您试用以下运行良好的脚本。

要使用它,您需要对这些进行更改:

$name = 'image.jpg';
// The path to the image with the file name:
$fileatt = "images/".$name;

在下面的代码中:(预先测试)

<?php

// Set who this goes to:
$to = array('Your Name','email@example.com');
// Give the message a subject:
$subject = 'Some subject';
// Set who this message is from:
$from = array("My Company", "email@example.com");

// Create the header information:
$random_hash = md5(date('r', time())); 
$mime_boundary = "==Multipart_Boundary_x{$random_hash}x"; 
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-Type: multipart/mixed; boundary="'.$mime_boundary.'"' . "\r\n";
$headers .= 'To: '.$to[0].' <'.$to[1].'>' . "\r\n";
$headers .= 'From: '.$from[0].' <'.$from[1].'>' . "\r\n";

// Build the message (can have HTML in it) message:
$message = 'This is an <b>HTML message</b> it comes with an attachment!'."\n\n".
// Do not edit this part of the message:
"--{$mime_boundary}\n" .
"Content-Type:text/html; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$message . "\n\n";
// The image that you would like to send (filename only):
$name = 'image.jpg';
// The path to the image with the file name:
$fileatt = "images/".$name;

$fileatt_type = "application/octet-stream"; // File Type
// Filename that will be used for the file as the attachment
$fileatt_name = $name; 

// Read the file attachment:
$file = fopen($fileatt,'rb');
$data = fread($file,filesize($fileatt));
fclose($file); 

// Create sendable information
$data = chunk_split(base64_encode($data));

// Finalize the message with attachment
$message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatt_type};\n" .
" name=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mime_boundary}\n";
unset($data);
unset($file);
unset($fileatt);
unset($fileatt_type);
unset($fileatt_name);

// Send the message:
mail($to[1], $subject, $message, $headers);

echo "Email sent";

?> 
于 2013-08-17T03:36:22.150 回答