2013-03-05 171 views
-1

我一直在試圖獲得一個電子郵件表格,它將帶有附件的電子郵件發送到存儲在數據庫中的電子郵件地址。我已經有一個腳本,將發送電子郵件到我的數據庫中的電子郵件地址,我有一個腳本發送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> 
+2

做你自己一個忙用[swiftmailier(http://swiftmailer.org/),或[PHPMailer的(http://phpmailer.worxware.com/) – 2013-03-05 08:46:01

回答

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

我沒有測試代碼,但它應該推你在正確的方向;-)

0

試試這個, 首先創建一個文件夾temp。

<?php 
    error_reporting(E_ALL^E_DEPRECATED); 
            //Database credentials 
    $mysql_db_hostname = "localhost"; 
    $mysql_db_user = "root"; 
    $mysql_db_password = ""; 
    $mysql_db_database = "reassurance"; 
    $con = mysql_connect($mysql_db_hostname, $mysql_db_user, $mysql_db_password) or die("Could not connect database"); 
    mysql_select_db($mysql_db_database, $con) or die("Could not select database"); 
    $filename='temp/test.csv'; 
    //$filename='temp/'.$test.'.csv'; 
    $fp=fopen($filename,"w"); 

    $sql = mysql_query("SELECT * FROM `contactus`"); 

    $row=mysql_fetch_assoc($sql); 

    $seperator=""; 

    $comma=""; 

    foreach($row as $name =>$value) 

    { 

    $seperator.=$comma.''.str_replace('','""',$name); 
    $comma=","; 

    } 
    $seperator.="\n"; 
    $seperator; 

    fputs($fp,$seperator); 

    mysql_data_seek($sql,0); 

    while($row=mysql_fetch_assoc($sql)) 

    { 

    $seperator=""; 

    $comma=""; 
    foreach($row as $name =>$value) 

    { 

    $seperator.=$comma.''.str_replace('','""',$value); 
    $comma=","; 

    } 

    $seperator.="\n"; 
    fputs($fp,$seperator); 

    } 

    fclose($fp); 
    $my_file ='test.csv'; 
    $path = "temp/"; 


    $from_name = "Hara Prasad Hota"; 

    $from_mail = "[email protected]"; 

    $mailto = "[email protected]"; 

    $subject = "This is a mail with attachment."; 

    $message = "Hallo,\r\n do you got attachment? I hope it is working.\r\n\r\Hara"; 

    $replyto="[email protected]"; 
    $file = $path.$my_file; 
     $file_size = filesize($file); 


    $handle = fopen($file, "r"); 

    $content = fread($handle, $file_size); 

    fclose($handle); 

    $content = chunk_split(base64_encode($content)); 

    $uid = md5(uniqid(time())); 

    $name = basename($file); 

    $header = "From: ".$from_name." <".$from_mail.">\r\n"; 

    $header .= "Reply-To: ".$replyto."\r\n"; 
     $header .= "MIME-Version: 1.0\r\n"; 

    $header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n"; 

    $header .= "This is a multi-part message in MIME format.\r\n"; 

    $header .= "--".$uid."\r\n"; 

    $header .= "Content-type:text/plain; charset=iso-8859-1\r\n"; 

    $header .= "Content-Transfer-Encoding: 7bit\r\n\r\n"; 

    $header .= $message."\r\n\r\n"; 
     $header .= "--".$uid."\r\n"; 

    $header .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n"; // use different content types here 

    $header .= "Content-Transfer-Encoding: base64\r\n"; 

    $header .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n"; 
     $header .= $content."\r\n\r\n"; 

    $header .= "--".$uid."--"; 

    mail($mailto, $subject, "", $header) 
    ?> 
相關問題