我一直在試圖獲得一個電子郵件表格,它將帶有附件的電子郵件發送到存儲在數據庫中的電子郵件地址。我已經有一個腳本,將發送電子郵件到我的數據庫中的電子郵件地址,我有一個腳本發送1封附件到1封電子郵件,並且它們都正常工作。但問題是我無法將它們合併爲一個腳本。我試圖將2合併成許多次,但我似乎可以弄明白。由於我還是個學生,我仍然在學習如何做這些事情。PHP:從數據庫發送帶附件到電子郵件帳戶的郵件
我會發布我下面的代碼,告訴你我到目前爲止。如果任何人有任何提示或可以告訴我如何去做,這將是非常有益的。
的config.php
<?php
$server="localhost";
$database="NAW";
$db_user="root";
$db_pass="something";
$fromadmin="[email protected]";
$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 = "[email protected]";
$to = "[email protected]";
$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>
做你自己一個忙用[swiftmailier(http://swiftmailer.org/),或[PHPMailer的(http://phpmailer.worxware.com/) – 2013-03-05 08:46:01