对不起,上面这个垃圾代码太多,我把没用的注释掉了,<?php$from = " <kei@lx94>";
$to = " <kei@lx94>";
$subject = "添付ファイルテスト";
$body = "添付ファイルテスト正文";
$filename = "/work/cgi-bin/prs/test.txt";
$ret = send_mail($to, $from, $subject, $body, $cc = null, $bcc = null, $filename);
function send_mail($to, $from, $subject, $body, $cc = null, $bcc = null, $attachment = null)
{
$body = "BCC:$bcc\r\n$body\r\n";
$body = "CC:$cc\r\n$body\r\n";
$to_tmp = join("\r\n ",explode(",",$to));
$body = "TO:$to_tmp\r\n$body\r\n"; mb_language("Japanese");
mb_internal_encoding("EUC-JP");
mb_detect_order("ASCII, JIS, UTF-8, EUC-JP, SJIS"); $to = "testmail@lx94";
$cc = "testmail@lx94";
$header .= 'From:'.$from."\r\n";
$header .= "Cc:".$cc."\r\n";
$header .= "Bcc:".$bcc."\r\n";
if ($attachment){ $fh = fopen($attachment,rb);
$attach = fread ($fh, filesize ($attachment));
fclose ($fh); $filename = basename($attachment);
$boundary = "_Boundary_" . uniqid(rand(1000,9999) . '_') . "_"; $subject = mb_encode_mimeheader( $subject ); $filename = mb_encode_mimeheader( $filename );
$attach = chunk_split(base64_encode($attach),76,"\n"); if (!$mime) $mime = "application/octet-stream";
$header = "To: ".$to."\n" .
"From: ".$from."\n" .
"X-Mailer: PHP/" . phpversion() . "\n".
"MIME-Version: 1.0\n" .
"Content-Type: Multipart/Mixed; boundary=\"".$boundary."\"\n" ; $mbody .= "--$boundary\n";
$mbody .= "Content-Type: text/plain; charset=ISO-2022-JP\n" .
"Content-Transfer-Encoding: 7bit\n";
$mbody .= "\n";
$mbody .= "$body\n";
$mbody .= "--".$boundary."\n";
$mbody .= "Content-Type: ".$mime."; name=\"".$filename."\"\n" .
"Content-Transfer-Encoding: base64\n" .
"Content-Disposition: attachment; filename=\"".$filename."\"\n";
$mbody .= "\n";
$mbody .= "$attach\n";
$mbody .= "--$boundary--\n"; return mail(NULL, $subject, $mbody, $header);
}else{
return mb_send_mail($to, $subject, $body, $header);
}
}?>
$to = " <kei@lx94>";
$subject = "添付ファイルテスト";
$body = "添付ファイルテスト正文";
$filename = "/work/cgi-bin/prs/test.txt";
$ret = send_mail($to, $from, $subject, $body, $cc = null, $bcc = null, $filename);
function send_mail($to, $from, $subject, $body, $cc = null, $bcc = null, $attachment = null)
{
$body = "BCC:$bcc\r\n$body\r\n";
$body = "CC:$cc\r\n$body\r\n";
$to_tmp = join("\r\n ",explode(",",$to));
$body = "TO:$to_tmp\r\n$body\r\n"; mb_language("Japanese");
mb_internal_encoding("EUC-JP");
mb_detect_order("ASCII, JIS, UTF-8, EUC-JP, SJIS"); $to = "testmail@lx94";
$cc = "testmail@lx94";
$header .= 'From:'.$from."\r\n";
$header .= "Cc:".$cc."\r\n";
$header .= "Bcc:".$bcc."\r\n";
if ($attachment){ $fh = fopen($attachment,rb);
$attach = fread ($fh, filesize ($attachment));
fclose ($fh); $filename = basename($attachment);
$boundary = "_Boundary_" . uniqid(rand(1000,9999) . '_') . "_"; $subject = mb_encode_mimeheader( $subject ); $filename = mb_encode_mimeheader( $filename );
$attach = chunk_split(base64_encode($attach),76,"\n"); if (!$mime) $mime = "application/octet-stream";
$header = "To: ".$to."\n" .
"From: ".$from."\n" .
"X-Mailer: PHP/" . phpversion() . "\n".
"MIME-Version: 1.0\n" .
"Content-Type: Multipart/Mixed; boundary=\"".$boundary."\"\n" ; $mbody .= "--$boundary\n";
$mbody .= "Content-Type: text/plain; charset=ISO-2022-JP\n" .
"Content-Transfer-Encoding: 7bit\n";
$mbody .= "\n";
$mbody .= "$body\n";
$mbody .= "--".$boundary."\n";
$mbody .= "Content-Type: ".$mime."; name=\"".$filename."\"\n" .
"Content-Transfer-Encoding: base64\n" .
"Content-Disposition: attachment; filename=\"".$filename."\"\n";
$mbody .= "\n";
$mbody .= "$attach\n";
$mbody .= "--$boundary--\n"; return mail(NULL, $subject, $mbody, $header);
}else{
return mb_send_mail($to, $subject, $body, $header);
}
}?>
解决方案 »
- [求助]关于discuz里面的php,应该怎么写才能把改动提交到数据库?
- 嵌套的函数会不会占用栈空间太大?
- mysql 的存储过程返回了多个结果集 怎么取其中的一个?
- 求助:文件管理,大侠帮帮忙!
- (50分求助)PHP执行遇到 Warning: Cannot modify header information,请教大家解决办法
- 請問在php下如何能得到从客户端读取到的数据的字节数
- error_reporting(7); 这句代码是什么意思?
- 关于MAIL的问题
- 哪儿有PHP+MySQL的免费主页空间?
- thinkphp 怎么设置APACHE的目录保护
- mysql 能实现相关度排序吗?
- 关于open_basedir restriction in effect...File(/XXX/XXX) is not within the allowed path的问题
上面的程序是正确的,
我想全部送信处理使用mb_send_mail
也就是return mail(NULL, $subject, $mbody, $header);
替换成,
return mb_send_mail($to, $subject, $body, $header);结果是上面的分支,原来使用mail送信的那个地方,改称mb_send_mail之后,发信可以,但是无法解析成附件。
不理解。php版本5.0,按理说直接把mail替换成mb_send_mail是没有问题的。
看了MIME的协议,根本没有思路就是瞎试,
http://cn2.php.net/manual/zh/function.mb-send-mail.php#32273
而且内容编码后使用chuk函数分割一下最好。
另外其实并没有必要为编码犯愁采用BASE64 ENCODE一下就可以了
例如"Subject: =?gb2312?B?".base64_encode("subject")."?=";这样的代码可以使用任何编码(gb2312就是编码你可以换成UTF-8)
对于boundary的写法我想我这里是没有问题的,
http://jp2.php.net/manual/ja/ref.mail.php
<?
$mime_boundary_2 = "1_".$mime_boundary; $msg .= "Content-Type: multipart/alternative; boundary=\"".$mime_boundary_2."\"".$eol;
# Text Version
$msg .= "--".$mime_boundary_2.$eol;
$msg .= "Content-Type: text/plain; charset=iso-8859-1".$eol;
$msg .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$msg .= strip_tags(str_replace("<br>", "\n", $body)).$eol.$eol;
# HTML Version
$msg .= "--".$mime_boundary_2.$eol;
$msg .= "Content-Type: text/html; charset=iso-8859-1".$eol;
$msg .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$msg .= $body.$eol.$eol;
# New Subboundary Finished
$msg .= "--".$mime_boundary_2."--".$eol.$eol; # Top-Boundary Finished
$msg .= "--".$mime_boundary."--".$eol.$eol;
?>看来不是非要-==dddd==开头结尾的。你说的那个chun函数应该是
<?php
$new_string = chunk_split(base64_encode($data));
?>
吧。我的程序里面有啊。
对于编码的问题,邮件文本采用EUC-JP编码,和文本采用SHIFT-JIS编码,这个是不变的。谢谢楼上了。