我使用的是PHP 5.6。尝试使用脚本时,我收到错误。该脚本用于发送电子邮件,这里是它的副本。我需要帮助修复它。谢谢。错误也在下面:
注意:未定义的偏移量:第36行的C:\ Apache \ htdocs \ ~s spamsite \ email.php中的1
这是脚本:
<?php
if( isset($_POST['submit']))
{
//define the receiver of the email
$to = $_POST["to"];
//define from whom the email is from ie Name & E-Mail.
$name=$_POST["name"];
$from = $_POST["from"];
//define who will be copied.
$cc = $_POST["cc"];
//define who will be blind copied.
$bcc = $_POST["bcc"];
//define the subject of the email
$subject = $_POST["subject"];
//define the message of the email
$message=$_POST["message"];
//so we use the MD5 algorithm to generate a random hash
$random_hash = md5(date('r', time()));
//define the headers we want passed. Note that they are separated with \r\n
$headers = "From: " . $name . "<" . $from . ">" . "\nCc: " . $cc . "\nBcc: " . $bcc;
// boundary
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
// headers for attachment
$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";
// multipart boundary
$message = "--{$mime_boundary}\n" . "Content-Type: text/html; charset=\"UTF-8\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" . "<p>" . $message . "</p>" . "\n\n";
// Preparing Attachment.
for($i=0;$i<(count($_FILES["attachment"]["name"])+1);$i++)
{
if($_FILES["attachment"]["name"][$i] != "")
{
$filename = $_FILES["attachment"]["name"][$i];
$filetype = $_FILES["attachment"]["type"][$i];
$content = chunk_split(base64_encode(file_get_contents($_FILES["attachment"]["tmp_name"][$i])));
$message .= "--{$mime_boundary}\n";
$message .= "Content-Type: " . $filetype . "; name=\"".$filename."\"\n";
$message .= "Content-Description: " .$filename . "\n";
$message .= "Content-Disposition: attachment;\n" . " filename=\"".$filename."\";\n";
$message .= "Content-Transfer-Encoding: base64\n\n" . $content."\n\n";
}
}
$message .= "--{$mime_boundary}--\r\n";
//send email
$mail = @mail($to, $subject, $message, $headers, "-f$from");
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed"
echo $mail? "Your Mail Has Been Sent Successfully" : "Oops..Something Went Wrong.. Your Mail Has Not Been Sent";
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Email Sender</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
</head>
<body>
<script type="text/javascript">
var upload_number = 2;
function addFileInput() {
var d = document.createElement("div");
var file = document.createElement("input");
file.setAttribute("type", "file");
file.setAttribute("name", "attachment["+upload_number+"]");
file.setAttribute("id", "attachment["+upload_number+"]");
file.setAttribute("size", "30")
d.appendChild(file);
document.getElementById("moreUploads").appendChild(d);
upload_number++;
}
</script>
<script type="text/javascript" src="http://js.nicedit.com/nicEdit-latest.js"></script>
<script type="text/javascript">
//<![CDATA[
bkLib.onDomLoaded(function()
{
new nicEditor({fullPanel : true}).panelInstance('message');
});
//]]>
</script>
<form action="" method="post" enctype="multipart/form-data">
<table width="1321" height="70" border="0">
<tr>
<td width="1298"> </td>
</tr>
</table>
<table width="1321" border="1"><tbody>
<tr>
<td width="132">To [Direct]:</td>
<td width="1142" bgcolor="#CC0000"><input name="to" type="text" size="100" />
<strong>Separate with Commas "," While Sending to Many</strong></td></tr>
<tr>
<td width="132">To [Cc] :</td>
<td width="1142" bgcolor="#CC0000"><input name="cc" type="text" size="100" />
<strong>Separate with Commas "," While Copying to Many</strong></td></tr>
<tr>
<td width="132">To [Bcc] :</td>
<td width="1142" bgcolor="#CC0000"><input name="bcc" type="text" size="100" />
<strong>Separate with Commas "," While Blind Copying to Many</strong></td></tr>
<tr>
<td>From [Email]:</td><td><input name="from" type="text" size="100" /></td></tr>
<tr>
<td>From [Name] :</td><td><input name="name" type="text" size="100"/></td></tr>
<tr><td>Subject :</td><td><input name="subject" type="text" size="100"/></td></tr>
<tr><td>Message :</td><td><textarea cols="120" rows="15" name="message" id="message"></textarea>
</td></tr>
<tr>
<td>Add Attachment:</td><td>
<input type="file" name="attachment[]" id="attachment[]" size="30" onchange="document.getElementById('moreUploadsLink').style.display = 'block';" />
<div id="moreUploads"></div>
<div id="moreUploadsLink" style="display:none;"><a href="java-script:addFileInput();">Attach another File</a></div>
</td></tr></td></tr>
<tr>
<td height="28"><input type="submit" name="submit" value="Submit" /></td></tr>
</tbody></table>
</form>
&#13;
答案 0 :(得分:0)
如果您不确定数组是否包含某些键,则应使用
if(isset($array['key'])) {
//Do something
}
您似乎应该更改此行
for($i=0;$i<(count($_FILES["attachment"]["name"])+1);$i++)
到
if(isset($_FILES["attachment"]) && isset($_FILES["attachment"]["name"]))
for($i=0;$i<(count($_FILES["attachment"]["name"])+1);$i++)
答案 1 :(得分:0)
你的for循环看起来有错误:
试试这个:
for($i = 0; $i < count($_FILES["attachment"]["name"]); $i++)
而不是:
for($i=0;$i<(count($_FILES["attachment"]["name"])+1);$i++)
因为如果您连接了3个文件,那么将会:
$_FILES["attachment"]["name"][0]
$_FILES["attachment"]["name"][1]
$_FILES["attachment"]["name"][2]
你的count($_FILES["attachment"]["name"])
将返回3.而你只想升到2。