...

View Full Version : help Cron Does not send attachment in mail



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

abduraooft
03-10-2010, 02:53 PM
$my_file = "monday_10_2010.doc";
$my_path = "mailer/file/"; You'd need to provide the complete-absolute path starting from root of your file system.

franzlin2010
03-10-2010, 04:17 PM
You'd need to provide the complete-absolute path starting from root of your file system.

Thank you for your quick reply

Please how do i know the complete absolute path starting from the root
I am running this cron file from the public_html directory
Both the cron file and the attachment file are in the same directory

is the absolute my home directory??
Home Directory = /home/megasale

thands is advance

abduraooft
03-10-2010, 04:31 PM
Please how do i know the complete absolute path starting from the root
Check the output of the following in your browser to see the absolute path to your root folder.

echo $_SERVER['DOCUMENT_ROOT'];
If your directory mailer is directly under root, then you could append /mailer/file/ to the above output and assign to the variable $my_path

franzlin2010
03-12-2010, 04:27 PM
abduraooft,

I have done as you have said but it was still the same thing,
the attachment does not send together with the message,

after echo $_SERVER['DOCUMENT_ROOT'];

i got the directory path
/home/megasale/public_html


Warning: filesize(): stat failed for /mailer/Equimax_Saddery_Limited.doc in /home/megasale/public_html/mail_cron.php on line 5

Warning: fopen(/mailer/Equimax_Saddery_Limited.doc): failed to open stream: No such file or directory in /home/megasale/public_html/mail_cron.php on line 6

Warning: fread(): supplied argument is not a valid stream resource in /home/megasale/public_html/mail_cron.php on line 7

Warning: fclose(): supplied argument is not a valid stream resource in /home/megasale/public_html/mail_cron.php on line 8

i knew that i specified a wrong directory and after specifying the correct directory by removing the first forward slash in in the mailer
the mails were sent sucessfully without any warning messages arrived correctly but attachment was just 1kb file instead of exact attached file.

$my_file = "document.doc";
$my_path = "mailer/";
Please does this have to do with the webhosting company??
so that i can contact them.

abduraooft
03-13-2010, 07:24 AM
I have done as you have said but it was still the same thing,
the attachment does not send together with the message,

after echo $_SERVER['DOCUMENT_ROOT'];
You can't use that global variable in your script. What I asked is to get the value of that variable and then append that path to your root with that relative path to image.

Warning: fopen(/mailer/Equimax_Saddery_Limited.doc): failed to open stream: No such file or directory in /home/megasale/public_html/mail_cron.php on line 6
I assume, it'd be something like

$my_file = "document.doc";
$my_path ="/home/megasale/public_html/mailer/file/";

franzlin2010
03-14-2010, 06:15 PM
Thank you very much abduraooft for the help you rendered me,
I have gotten it to work perfectly

I am very grateful
Incase there is anything i will not hesitate to get back to you.

Thank you very much
Francis



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum