Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP: Sending mails with attachment to email accounts from database
    primarykey
    data
    text
    <p>I have been trying to get an email form that will send emails with attachments to email adresses which are stored in a database. I already have a script that will send an email to the email adresses in my database and I have a script that sends 1 attachment to 1 email and both of them work fine. The problem however is that I can't get them combined into 1 script. I have tried to combine the 2 into 1 many times but I can seem to figure it out. Since I'm still a student I am still learning how to do these things.</p> <p>I will post the code I have below to show you what I have so far. If anyone has any tips or could show me how to do it it would be very helpful.</p> <p>config.php</p> <pre><code>&lt;?php $server="localhost"; $database="NAW"; $db_user="root"; $db_pass="something"; $fromadmin="test@test.com"; $table="Email"; $table_email="Email"; ?&gt; </code></pre> <p>Send mails to email adresses in database:</p> <pre><code>&lt;?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.'&lt;br&gt;'; sleep($seconds); } echo 'Mails sent. Go &lt;a href="massmail.php"&gt;Back&lt;/a&gt;'; } else { ?&gt; &lt;table height="250" cellpadding="1"&gt; &lt;tr&gt;&lt;td valign="top"&gt; &lt;h2&gt;Mail Sender&lt;/h2&gt;&lt;br&gt;&lt;form action="massmail.php" method="POST"&gt; &lt;div align="center"&gt; &lt;table cellpadding="0" border="0" align="left"&gt; &lt;tr&gt; &lt;td&gt; Subject: &lt;/td&gt; &lt;td&gt; &lt;input type="text" align="left" name="subj" size="66"&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt;&lt;td align="left" valign="top"&gt; Message Text:&lt;/td&gt;&lt;td align="left"&gt; &lt;textarea name="message" rows="15" cols="60" &gt;&lt;/textarea&gt;&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;tr&gt;&lt;td colspan="2" align="left"&gt; Seconds between messages:&lt;input type="text" size="10" name="seconds" value="0.1"&gt; (seconds) &lt;/td&gt;&lt;/tr&gt; &lt;tr&gt;&lt;td colspan="2"&gt; &lt;input type="submit" value="Send mass mails" name="submit" &gt; &lt;/Td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/div&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;?php } include "footer.php"; ?&gt; </code></pre> <p>Send a mail with an attachment:</p> <pre><code>&lt;?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 =&gt; $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(&amp;$files) { $names = array( 'name' =&gt; 1, 'type' =&gt; 1, 'tmp_name' =&gt; 1, 'error' =&gt; 1, 'size' =&gt; 1); foreach ($files as $key =&gt; $part) { // only deal with valid keys and multiple files $key = (string) $key; if (isset($names[$key]) &amp;&amp; is_array($part)) { foreach ($part as $position =&gt; $value) { $files[$position][$key] = $value; } // remove old key reference unset($files[$key]); } } } ?&gt; &lt;html&gt; &lt;body&gt; &lt;form method="POST" action="bijlagetest.php" enctype="multipart/form-data"&gt; &lt;input type="file" name="attachment[]"&gt;&lt;br/&gt; &lt;input type="submit"&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload