note 55078 deleted from ref.mail by mazzanet
| From: | mazzanet@php.net | Date: | Sun, 25 Dec 2005 23:20:57 +0000 |
| Subject: | note 55078 deleted from ref.mail by mazzanet | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-100696@lists.php.net to get a copy of this message | ||
Note Submitter: marco at bernawebdesign dot ch
----
hi all!
here is a little re-work and upgrade of the function posted by sadikkeskin at hotmail dot com.
the function can be used with a form (enctype="multipart/form-data") wich has the fields
"text" and as many "input type=file name="attachment[]" " as you
desire...
it then sends the mail to all the email adresses stored in your database
enjoy! Marco
<?php
$boundary='--' . md5( uniqid("myboundary") );
$priorities = array( '1 (Highest)', '2 (High)', '3 (Normal)',
'4 (Low)', '5 (Lowest)' );
$priority=$priorities[2];
$charset="iso-8859-1";
$ctencoding="8bit";
$sep= chr(13) . chr(10);
$disposition="inline";
$from="newsletter@example.com";
$subject = "Newsletter example.com - ".date("d.m.Y",mktime());
//_________________________________header_____________________
$header.="From: $from\nX-Priority: $priority\nCC: $cc\n";
$header.="Mime-Version: 1.0\nContent-Type: multipart/mixed;\n
boundary=\"$boundary\"\n";
$header.="Content-Transfer-Encoding: $ctencoding\nX-Mailer: Php/libMailv1.3\n";
//_________________________________header_____________________
//__________________________STARTS FETCHING MySQL EMAILS____________________
//needs a valid MySQL resource ($result)
while ($row = mysql_fetch_array($result)){
$mailtext = "Hello $row[name]\r\n".$_POST['text'];
$message .="This is a multi-part message in MIME format.\n--$boundary\n";
$message .= "Content-Type: text/plain; charset=$charset\n";
$message .= "Content-Transfer-Encoding: $ctencoding\n\n" . $mailtext ."\n";
foreach($_FILES['attachment']['error'] as $key => $error) {
if($error == UPLOAD_ERR_OK) {
$filename = $_FILES['attachment']['name'][$key];
$filetype = $_FILES['attachment']['type'][$key];
$message .="--$boundary\nContent-type: $filetype ;\n
name=\"$filename\"\n";
$message .="Content-Transfer-Encoding: base64\nContent-Disposition: $disposition;\n
filename=\"$filename\"\n";
$linesz= filesize( $_FILES['attachment']['tmp_name'][$key])+1;
$fp= fopen( $_FILES['attachment']['tmp_name'][$key], 'r' );
$content = chunk_split(base64_encode(fread( $fp, $linesz)));
fclose($fp);
$message .= $sep.$content;
}
}
// sends the mail to each member of the newsletter
$sended = mail($row['email'], $subject, $message, $header);
if ($sended){$emails.= $row['email'].", ";}
}
//__________________________END FETCHING MySQL EMAILS____________________
//removes the last ", "
$emails = substr($emails,0,strlen($emails)-2);
// prints the adresses the mail was succesfully send to
print "<p>Your message has been sent to the following addresses:
$emails</p>";
print "<div align ='right'><a href='javascript:
history.go(-1);'>Back</a></div>";
?>