发附件问题,mb_send_mail不可以,mail可以,为啥?

thefirstwind 2007-06-15 03:14:02
很着急啊,
求解。
谢谢先
以下是我的程序:



<?php

$from .= " <kei@lx94>";

$to .= " <kei@lx94>";

$subject = "添付ファイルテスト";
$body = "添付ファイルテスト正文";

$filename = "/work/cgi-bin/prs/test.txt";
$att_files = "/work/cgi-bin/prs/テストファイル.txt";
//$mine = "js";

//send_mail2($to, $subject, $text_body, $headers, $html_body="", $att_names="", $att_files="", $file_types="");
/*
if ($filename){
($attach = file_get_contents($filename)) Or die("Open Error: $filename");
$filename = basename($filename);
$ret = Attach_Mail($from, $to, $subject, $body, $filename, $attach, $mine);
}else{

}
*/
$ret = send_mail($to, $from, $subject, $body, $cc = null, $bcc = null, $filename);
//echo $ret;

function send_mail($to, $from, $subject, $body, $cc = null, $bcc = null, $attachment = null)
{
if ($to != "" && isset( $to ) ) {
$to =str_replace( " " , "" , $to ) ;
$to =str_replace( "\t" , "," , $to ) ;
}
if ($cc != "" && isset( $cc ) ) {
$cc =str_replace( " " , "" , $cc ) ;
$cc =str_replace( "\t" , "," , $cc ) ;
}
if ($bcc != "" && isset( $bcc ) ) {
$bcc =str_replace( " " , "" , $bcc ) ;
$bcc =str_replace( "\t" , "," , $bcc ) ;
}

// 念の為、言語と文字コードの設定
$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");

// From を変換
//$FromName = mb_encode_mimeheader(mb_convert_encoding($FromName,'JIS','auto'));
//$header .= 'From:'.$FromName.' <'.$FromMail.'>'."\r\n";
$to = "testmail@lx94";
$cc = "testmail@lx94";
$header .= 'From:'.$from."\r\n";
//$ToName = mb_encode_mimeheader(mb_convert_encoding($ToName,'JIS','auto'));
//$header .= 'To:"'.$ToName.'"<'.$to.'>'."\r\n";
//$header .= 'To:'.$to."\r\n";
$header .= "Cc:".$cc."\r\n";
$header .= "Bcc:".$bcc."\r\n";


if ($attachment){

// ($attach = file_get_contents($attachment)) Or die("Open Error: $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 ); // ISO-2022-JP/Base64に変換

// 添付データのエンコード
// 日本語のファイル名はRFC違反ですが、多くのメーラは理解します
$filename = mb_encode_mimeheader( $filename ); // ISO-2022-JP/Base64に変換
$attach = chunk_split(base64_encode($attach),76,"\n"); // Base64に変換し76Byte分割

// メディアタイプ未指定の場合は汎用のタイプを指定
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" ;
//"Content-Transfer-Encoding: 7bit";

// マルチパート:本文
$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);
}
}

?>
...全文
1046 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
lacylxc 2012-04-27
  • 打赏
  • 举报
回复
急求PHP mail()函数配置问题
thefirstwind 2007-06-20
  • 打赏
  • 举报
回复
楼上


对于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编码,这个是不变的。

谢谢楼上了。

peterxuxu 2007-06-20
  • 打赏
  • 举报
回复
$mbody .= "--$boundary--\n";多段文本应该是-==dddd==开头 -==dddd==结尾才对
而且内容编码后使用chuk函数分割一下最好。
另外其实并没有必要为编码犯愁采用BASE64 ENCODE一下就可以了
例如"Subject: =?gb2312?B?".base64_encode("subject")."?=";这样的代码可以使用任何编码(gb2312就是编码你可以换成UTF-8)
thefirstwind 2007-06-20
  • 打赏
  • 举报
回复
very nice , 回家看看,谢谢楼上。
fredyj 2007-06-20
  • 打赏
  • 举报
回复
关注

手册上面看不出来什么 note里还有人说应该可以用。

你看看这个人说的是不是有帮助~
http://cn2.php.net/manual/zh/function.mb-send-mail.php#32273
thefirstwind 2007-06-20
  • 打赏
  • 举报
回复
谢谢楼上,不过我还是想求个答案。
看了MIME的协议,根本没有思路就是瞎试,
li1229363 2007-06-18
  • 打赏
  • 举报
回复
你提问等待时间太早了,而且今天是周一。

mb_send_mail不是php标准的函数,所以,就是贴出了代码,一般人是没有时间帮你分析的

mail是php的标准函数,所以,可以发送成功是无可厚非的
thefirstwind 2007-06-18
  • 打赏
  • 举报
回复
没有人知道吗?
圆圆木公 2007-06-16
  • 打赏
  • 举报
回复
把错误贴出来,
thefirstwind 2007-06-16
  • 打赏
  • 举报
回复
对不起,可能是我表述的不太清楚,
上面的程序是正确的,
我想全部送信处理使用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是没有问题的。
purplecalm 2007-06-15
  • 打赏
  • 举报
回复
show me the error log.
thefirstwind 2007-06-15
  • 打赏
  • 举报
回复
对不起,上面这个垃圾代码太多,我把没用的注释掉了,



<?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);
}
}

?>

21,886

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧