Question

I am trying to process an email in response to a submission. The email should grab a previously uploaded file and include it as an attachment. There are two problems occuring right now.

  1. I am receiving error messages on the page when the process runs to send the message. The errors are: Warning: filesize() [function.filesize]: stat failed for ./example.pdf in /home/avantjob/public_html/portal/profilemenu.php on line 186

Warning: fopen(./example.pdf) [function.fopen]: failed to open stream: No such file or directory in /home/avantjob/public_html/portal/profilemenu.php on line 187

Warning: fread() expects parameter 1 to be resource, boolean given in /home/avantjob/public_html/portal/profilemenu.php on line 188

Warning: fclose() expects parameter 1 to be resource, boolean given in /home/avantjob/public_html/portal/profilemenu.php on line 189

Despite receiving these messages, the email is being sent, and the attachment is included, whoever the file is corrupt/truncated and will not open.

The code is listed below:

//.Class for processing mail
class AttachmentEmail {
private $from = 'triangle@avant.jobs';
private $from_name = 'AVANT Portal';
private $reply_to = 'triangle@avant.jobs';
private $to = '';
private $subject = '';
private $message = '';
private $attachment = '';
private $attachment_filename = '';

public function __construct($to, $subject, $message, $attachment = '', $attachment_filename = '') 
    {
    $this -> to = $to;
    $this -> subject = $subject;
    $this -> message = $message;
    $this -> attachment = $attachment;
    $this -> attachment_filename = $attachment_filename;
    }

public function getMimeType($file) 
{
// MIME types array
require_once('mimetypes.php');
$extension = end(explode('.', $file));
return $mimeTypes[$extension]; // return the array value
}

public function mail() {
    if (!empty($this -> attachment)) {
        $filename = empty($this -> attachment_filename) ? basename($this -> attachment) : $this -> attachment_filename ;
        $path = dirname($this -> attachment);
        $mailto = $this -> to;
        $from_mail = $this -> from;
        $from_name = $this -> from_name;
        $replyto = $this -> reply_to;
        $subject = $this -> subject;
        $message = $this -> message;

        $file = $path.'/'.$filename;
        $mime_type = $this->getMimeType($file);
        $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: ".mime_type."; 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($mailto, $subject, "", $header)) {
            return true;
        } else {
            return false;
        }
    } else {
        $header = "From: ".($this -> from_name)." <".($this -> from).">\r\n";
        $header .= "Reply-To: ".($this -> reply_to)."\r\n";

        if (mail($this -> to, $this -> subject, $this -> message, $header)) {
            return true;
        } else {
            return false;
        }

    }
}
}

// Display links to each profile sub-program

if (isset($_POST['submit'])) 
    {
    $queryname = "SELECT FNAME, LNAME, MNAME, JOBWANT, PAYWANT, PAYCODE, RESUMEFILE FROM APP WHERE ID_NUM = '" . $_SESSION['IDNUM'] . "'";
    $namedata = mysqli_query($dbc, $queryname);
    $namerow = mysqli_fetch_array($namedata);
    $fname = $namerow['FNAME'];
    $mname = $namerow['MNAME'];
    $lname = $namerow['LNAME'];
    $jobwant = $namerow['JOBWANT'];
    $paywant = $namerow['PAYWANT'];
    $paycode = $namerow['PAYCODE'];
    $resumefile = $namerow['RESUMEFILE'];
    $msg = "This message was sent in response to a completed application on the AVANT Portal. \n".
        " \n".
        "The application information is listed below. \n".
        " \n".
        "Name: $fname $mname $lname \n".
        "Desired Job: $jobwant \n".
        "Pay Rate: $paywant per $paycode \n".
        "Please review this candidate as soon as possible. \n".
        " \n".
        "The resume can be viewed at www.avant.jobs/portal/uploads/" . $resumefile . "\n".
        " \n";
    $subject = "AVANT Portal Application - " . $fname . " " . $lname;
    $sendit = new AttachmentEmail('triangle@avant.jobs', $subject, $msg, $resumefile);
    $sendit -> mail();

Any help you can offer would be greatly appreciated. Thank you.

Was it helpful?

Solution

You have one error - the file does not exist. Check the path in the error message (the one with stat failed on it) and replace it with a correct one, and preferrably fix your error handling to check if the file exists and error out if it does not.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top