Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Mar 2008
    Posts
    11
    Thanks
    1
    Thanked 0 Times in 0 Posts

    help Cron Does not send attachment in mail

    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 Code:
    <?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

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,862
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    $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.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #3
    New Coder
    Join Date
    Mar 2008
    Posts
    11
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by abduraooft View Post
    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

  • #4
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,862
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    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.
    PHP Code:
    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
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #5
    New Coder
    Join Date
    Mar 2008
    Posts
    11
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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
    Code:
    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.
    PHP Code:
    $my_file "document.doc";
                
    $my_path "mailer/"
    Please does this have to do with the webhosting company??
    so that i can contact them.
    Last edited by franzlin2010; 03-12-2010 at 06:16 PM.

  • #6
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,862
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    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
    PHP Code:
    $my_file "document.doc";
                
    $my_path ="/home/megasale/public_html/mailer/file/"
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #7
    New Coder
    Join Date
    Mar 2008
    Posts
    11
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •