大家帮忙小弟我加急需要一份用php写的能发附件的代码,帮忙了
我已经有了能发邮件的代码了,可是我不知道怎么把发送邮件的代码加进去。帮忙了,小弟急需要,谢谢!~~~~~~~%555555555555
发邮件的代码如下:(没有问题了,可定能发邮件)
<?
///以下将表单的内容写入message中
while ( list( $key, $val ) = each( $HTTP_POST_VARS ) )
{
$message .= "${key}: ${val}\r\n";
}
if ($message == "") $message = "无内容";
if ($to == "") $to = ""; //如果表单中没有收件人,请设置默认收件人e-mail
if ($from == "") $from = "";//如果表单中没有寄件人,请设置默认寄件人e-mail
if ($subject == "") $subject = ""; //如果表单中没有指定主题,请设置默认主题
if ($end = send22($to,$from,$subject, $message ) ) echo $end;
else echo "发送成功!";
function send22($to,$from,$subject,$inFileLocation, $message )
{
//使用本函数之前,务必请定义好以下变量
#############################################
$smtp = "smtp.163.com";//您的SMTP 服务器供应商,可以是域名或IP地址
$check = 1;//SMTP需要要身份验证设值为 1 不需要身份验证值为 0,现在大多数的SMTP服务商都要验证,如不清楚请与你的smtp 服务商联系。
if ($check) {
$username = ""; //您的email帐号名称
$password = "";//您的email密码
}
$s_from = "";//此email 必需是发信服务器上的email
$mailAttachments = ""; // 附件
###############################################
//连接服务器
$fp = fsockopen ( $smtp, 25, $errno, $errstr, 60);
if (!$fp ) return "联接服务器失败".__LINE__;
set_socket_blocking($fp, true );
$lastmessage=fgets($fp,512);
if ( substr($lastmessage,0,3) != 220 ) return "错误信息:".$lastmessage.__LINE__;
//HELO
$yourname = "YOURNAME";
if($check == "1") $lastact="EHLO ".$yourname."\r\n";
else $lastact="HELO ".$yourname."\r\n";
fputs($fp, $lastact);
$lastmessage == fgets($fp,512);
if (substr($lastmessage,0,3) != 220 ) return "错误信息$lastmessage".__LINE__;
while (true) {
$lastmessage = fgets($fp,512);
if ( (substr($lastmessage,3,1) != "-")or(empty($lastmessage)) )
break;
}
//身份验证
if ($check=="1") {
//验证开始
$lastact="AUTH LOGIN"."\r\n";
fputs( $fp, $lastact);
$lastmessage = fgets ($fp,512);
if (substr($lastmessage,0,3) != 334) return "错误信息$lastmessage".__LINE__;
//用户姓名
$lastact=base64_encode($username)."\r\n";
fputs( $fp, $lastact);
$lastmessage = fgets ($fp,512);
if (substr($lastmessage,0,3) != 334) return "错误信息$lastmessage".__LINE__;
//用户密码
$lastact=base64_encode($password)."\r\n";
fputs( $fp, $lastact);
$lastmessage = fgets ($fp,512);
if (substr($lastmessage,0,3) != "235") return "错误信息$lastmessage".__LINE__;
}
//FROM:
$lastact="MAIL FROM: $s_from" . "\r\n";
fputs( $fp, $lastact);
$lastmessage = fgets ($fp,512);
if (substr($lastmessage,0,3) != 250) return "错误信息$lastmessage".__LINE__;
//TO:
$lastact="RCPT TO: $to" . "\r\n";
fputs( $fp, $lastact);
$lastmessage = fgets ($fp,512);
if (substr($lastmessage,0,3) != 250) return "错误信息$lastmessage".__LINE__;
//inFileLocation
$lastact="FormatAttachmentHeader"."\r\n";
fputs($fp,$lastact);
$lastmessage = fgets ($fp,512);
if (substr($lastmessage,0,3) !=324) return "错误信息$lastmessage".__LINE__;
//DATA
$lastact="DATA\r\n";
fputs($fp, $lastact);
$lastmessage = fgets ($fp,512);
if (substr($lastmessage,0,3) != 354) return "错误信息$lastmessage".__LINE__;
//处理Subject头
$head="Subject: $subject\r\n";
$message = $head."\r\n".$message;
//处理From头
$head="From: $from\r\n";
$message = $head.$message;
//处理To头
$head="To: $to\r\n";
$message = $head.$message;
$message .= "\r\n.\r\n";
//发送信息
fputs($fp, $message);
$lastact="QUIT\r\n";
fputs($fp,$lastace);
fclose($fp);
return 0;
}
?> <?php
echo "<form action='$PHP_SELF' enctype='multipart/form-data' method='post'>\n";
echo "from<input type='text' name='from'><br>\n";
echo "to<input type='text' name='to'><br>\n";
echo "subject<input type='text' name='subject'><br>\n";
echo "attachment<input type='file' name='attachment'><br>\n";
echo "body<textarea name='body'></textarea><br>\n";
echo "<input type='submit' name='send' value='Send'>\n";
echo "</form>\n";
if ($send) {
$boundary = uniqid( "");
$headers = "From: $from
Content-type: multipart/mixed; boundary=\"$boundary\"";
if ($attachment_type) $mimeType = $attachment_type;
else $mimeType = "application/unknown";
$fileName = $attachment_name;
$fp = fopen($attachment, "r");
$read = fread($fp, filesize($attachment));
$read = base64_encode($read);
$read = chunk_split($read);
$body = "--$boundary
Content-type: text/plain; charset=iso-8859-1
Content-transfer-encoding: 8bit
$body
--$boundary
Content-type: $mimeType; name=$fileName
Content-disposition: attachment; filename=$fileName
Content-transfer-encoding: base64
$read
--$boundary--";
mail($to, $subject, $body, $headers);
}
?> 这断代码我自己在用,
没有完成的很好,只用来发一个附件.(为我新买的的wap手机传图) 谢谢大哥,我试试看! :P
页:
[1]