好的,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!";
}
?>
任何想法都会受到赞赏。
答案 0 :(得分:0)
使用以下命令从我的服务器根目录尝试脚本时
$path = '/tmp2/';
$filename = 'tmporder2.txt'; // I renamed it with an `.txt` extension to test
它没有发送它并收到很多错误。
但是,当我测试相同的脚本但删除/
开头的tmp2
并在末尾添加/
时,例如:
$path = 'tmp2/';
$filename = 'tmporder2.txt';
它起了作用,并向我发送了附带该文件的电子邮件。
尝试从服务器的root
运行我的示例代码,创建一个名为tmp2
的子文件夹,并在其中插入包含任何类型文本的tmporder2.txt
,然后再次尝试。
我还注意到代码中还有一些错误。
例如,此行$message=$handle;
如果要将其作为附件发送,则不应该成为您邮件的一部分。
if (mail($mailto, $subject, "", $header)) {
第3个参数是邮件正文所在的位置。
我将其替换为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";
?>