franzlin2010
03-10-2010, 12:53 PM
Dear Helpers
I am working on a mailing list contained in a database,
Emails are to be sent with an attachment each,
After writing the code and and setting the cron in the control panel of my webhosting, to run every five minutes
I detected that mails that were sent did'not contain the attachment, rather, it only contained a 1byte file.
But when i run the same file in my browser, the mails and the attachment are sent correctly,
Please can u help me, does it mean that cron cannot handle attachments??
thnx in advance
<?php
////////////////////////
function mail_attachment($filename, $path, $mail_address, $from_mail, $from_name, $replyto, $subject, $message) {
$file = $path.$filename;
$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";
$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."--";
///////////////////////////////////////////////////
///////////////////////////////////////////////////
if (mail($mail_address, $subject, "", $header)) {
echo "<div style=\"margin-bottom: 2px;\">message sucessfully sent to: $mail_address</div>";
}
else {
echo "mail sending ... ERROR!";
}
}
$db['username'] = "username";
$db['password'] = "password";
$db['host'] = "localhost";
$db['dbname'] = "dbname";
$db['error'] = "The website encountered an error";
///////////////////////////////////////
$my_file = "monday_10_2010.doc";
$my_path = "mailer/file/";
$my_name = "";
$my_mail = "today@sample.com";
$my_replyto = "mail@sample.com";
$subject = "Monday 10 of March 2010";
$my_subject = $subject;
$message = "My message";
////////////////////////////////////////////////////////
$connect = mysqli_connect($db['host'], $db['username'], $db['password'], $db['dbname']) or die($db['error']);
$select_query = "SELECT * FROM mailing_lists WHERE sent = '0' limit 2";
$query_select = mysqli_query($connect, $select_query) or die($db['error']);
$no_rows = mysqli_num_rows($query_select);
// A loop that sends the mails//
while($adds_array = mysqli_fetch_array($query_select))
{
$my_message = "Dear ".$adds_array['address'].",\n\n";
$my_message .= $message;
mail_attachment($my_file, $my_path, $adds_array['address'], $my_mail, $my_name, $my_replyto, $my_subject, $my_message);
//After sending the mails, update the list of sent addresses//
mysqli_query($connect, "UPDATE mailing_lists SET sent = '1' WHERE sent = '0' AND id = '$adds_array[id]'");
}
?>
I'll be very happy if a help is offered
I am working on a mailing list contained in a database,
Emails are to be sent with an attachment each,
After writing the code and and setting the cron in the control panel of my webhosting, to run every five minutes
I detected that mails that were sent did'not contain the attachment, rather, it only contained a 1byte file.
But when i run the same file in my browser, the mails and the attachment are sent correctly,
Please can u help me, does it mean that cron cannot handle attachments??
thnx in advance
<?php
////////////////////////
function mail_attachment($filename, $path, $mail_address, $from_mail, $from_name, $replyto, $subject, $message) {
$file = $path.$filename;
$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";
$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."--";
///////////////////////////////////////////////////
///////////////////////////////////////////////////
if (mail($mail_address, $subject, "", $header)) {
echo "<div style=\"margin-bottom: 2px;\">message sucessfully sent to: $mail_address</div>";
}
else {
echo "mail sending ... ERROR!";
}
}
$db['username'] = "username";
$db['password'] = "password";
$db['host'] = "localhost";
$db['dbname'] = "dbname";
$db['error'] = "The website encountered an error";
///////////////////////////////////////
$my_file = "monday_10_2010.doc";
$my_path = "mailer/file/";
$my_name = "";
$my_mail = "today@sample.com";
$my_replyto = "mail@sample.com";
$subject = "Monday 10 of March 2010";
$my_subject = $subject;
$message = "My message";
////////////////////////////////////////////////////////
$connect = mysqli_connect($db['host'], $db['username'], $db['password'], $db['dbname']) or die($db['error']);
$select_query = "SELECT * FROM mailing_lists WHERE sent = '0' limit 2";
$query_select = mysqli_query($connect, $select_query) or die($db['error']);
$no_rows = mysqli_num_rows($query_select);
// A loop that sends the mails//
while($adds_array = mysqli_fetch_array($query_select))
{
$my_message = "Dear ".$adds_array['address'].",\n\n";
$my_message .= $message;
mail_attachment($my_file, $my_path, $adds_array['address'], $my_mail, $my_name, $my_replyto, $my_subject, $my_message);
//After sending the mails, update the list of sent addresses//
mysqli_query($connect, "UPDATE mailing_lists SET sent = '1' WHERE sent = '0' AND id = '$adds_array[id]'");
}
?>
I'll be very happy if a help is offered