我一直在尝试获取一封电子邮件表单,该表单会将附件的电子邮件发送到存储在数据库中的电子邮件地址。我已经有了一个脚本,它会向我的数据库中的电子邮件地址发送一封电子邮件,我有一个脚本可以将1个附件发送到1封电子邮件,并且它们都可以正常工作。然而问题是我无法将它们组合成一个脚本。我试图将2合1多次合并,但我似乎可以弄明白。由于我还是学生,我还在学习如何做这些事情。
我将发布下面的代码,向您展示到目前为止我所拥有的代码。如果有人有任何提示或可以告诉我如何做这将是非常有帮助的。
的config.php
<?php
$server="localhost";
$database="NAW";
$db_user="root";
$db_pass="something";
$fromadmin="test@test.com";
$table="Email";
$table_email="Email";
?>
将邮件发送到数据库中的电子邮件地址:
<?php
include "header.php";
include "config.php";
if( $_POST || $_FILES )
{
$seconds=$_POST['seconds'];
$subject=$_POST['subj'];
$messagesend=$_POST['message'];
mysql_connect($server, $db_user, $db_pass)
or die ("Database CONNECT Error");
$resultquery = mysql_db_query($database, "select * from $table");
while ($query = mysql_fetch_array($resultquery))
{
$emailinfo=$myemail;
$mailto=$query[$table_email];
mail($mailto, $subject, $messagesend , "From:".$fromadmin."\nReply-To:".$fromadmin."\n");
echo 'Mail sent to '.$mailto.'<br>';
sleep($seconds);
}
echo 'Mails sent. Go <a href="massmail.php">Back</a>';
}
else
{
?>
<table height="250" cellpadding="1">
<tr><td valign="top">
<h2>Mail Sender</h2><br><form action="massmail.php" method="POST">
<div align="center">
<table cellpadding="0" border="0" align="left">
<tr>
<td>
Subject:
</td>
<td>
<input type="text" align="left" name="subj" size="66">
</td>
</tr>
<tr><td align="left" valign="top">
Message Text:</td><td align="left"> <textarea name="message" rows="15" cols="60" ></textarea></td></tr>
<tr>
<tr><td colspan="2" align="left">
Seconds between messages:<input type="text" size="10" name="seconds" value="0.1"> (seconds)
</td></tr>
<tr><td colspan="2">
<input type="submit" value="Send mass mails" name="submit" >
</Td>
</tr>
</table>
</div>
</td>
</tr>
</table>
<?php
}
include "footer.php";
?>
发送带附件的邮件:
<?php
if( $_POST || $_FILES )
{
// email fields: to, from, subject, and so on
// Here
$from = "test@test.com";
$to = "test2@test.com";
$subject = "Mail with Attachment";
$message = "This is the message body and to it I will append the attachments.";
$headers = "From: $from";
// 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/plain; charset=\"iso-8859-1\"\n"."Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n";
fixFilesArray($_FILES['attachment']);
foreach ($_FILES['attachment'] as $position => $file)
{
// should output array with indices name, type, tmp_name, error, size
$message .= "--{$mime_boundary}\n";
$fp = @fopen($file['tmp_name'],"rb");
$data = @fread($fp,filesize($file['tmp_name']));
@fclose($fp);
$data = chunk_split(base64_encode($data));
$message .= "Content-Type: application/octet-stream; name=\"".$file['name']."\"\n"."Content-Description: ".$file['name']."\n" ."Content-Disposition: attachment;\n" . " filename=\"".$file['name']."\";size=".$file['size'].";\n"."Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
}
$message .= "--{$mime_boundary}--";
$returnpath = "-f" . $from;
$ok = @mail($to, $subject, $message, $headers, $returnpath);
if($ok){ return 1; } else { return 0; }
}
//This function will correct file array from $_FILES[[file][position]] to $_FILES[[position][file]] .. Very important
function fixFilesArray(&$files)
{
$names = array( 'name' => 1, 'type' => 1, 'tmp_name' => 1, 'error' => 1, 'size' => 1);
foreach ($files as $key => $part) {
// only deal with valid keys and multiple files
$key = (string) $key;
if (isset($names[$key]) && is_array($part)) {
foreach ($part as $position => $value) {
$files[$position][$key] = $value;
}
// remove old key reference
unset($files[$key]);
}
}
}
?>
<html>
<body>
<form method="POST" action="bijlagetest.php" enctype="multipart/form-data">
<input type="file" name="attachment[]"><br/>
<input type="submit">
</form>
</body>
</html>
答案 0 :(得分:1)
好的是附加文件,用户首先需要一个上传表单
<form enctype="multipart/form-data" action="uploader.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
Choose a file to upload: <input name="uploadedfile" type="file" /><br />
<input type="submit" value="Upload File" />
</form>
在PHP中保存文件做类似的事情
$target_path = "uploads/";
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['uploadedfile']['name']).
" has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}
现在您将文件作为附件发送,将您的脚本重写为可以处理多个附件的函数...
这样的事情应该有用......
<?php
function sendMail($to, $from, $subject, $message, $attachments){
$headers = "From: $from";
// 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 = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n";
$message .= "--{$mime_boundary}\n";
// preparing attachments
for($x=0;$x<count($attachments);$x++){
$file = fopen($attachments[$x],"rb");
$data = fread($file,attachmentsize($attachments[$x]));
fclose($file);
$data = chunk_split(base64_encode($data));
$message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$attachments[$x]\"\n" .
"Content-Disposition: attachment;\n" . " filename=\"$attachments[$x]\"\n" .
"Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
$message .= "--{$mime_boundary}\n";
}
// send
$ok = @mail($to, $subject, $message, $headers);
if ($ok) {
echo "<p>mail sent to $to!</p>";
} else {
echo "<p>mail could not be sent!</p>";
}
}
?>
我没有测试过这段代码,但是它应该让你朝着正确的方向前进; - )