Search code examples
phpemailphpmaileremail-attachments

“Could Not Access File:” in PHPmail function


I am trying to email a file that exists on my server using PHPMailer. When I run this code, I get "Could not access file" and the email sends without the attachment.can anyone guide me how to fix this

    $checkyes=$_POST['check'];
    $date = date('Y-m-d');

    $my_path ="/data/data/www/fms/pricelists/$checkyes{$date}.xls";

    include "class.phpmailer.php"; // include the class file name
    $mail = new PHPMailer(); // create a new object
    $mail->IsSMTP(); // enable SMTP
    $mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
    $mail->SMTPAuth = true; // authentication enabled
    //$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for GMail
    $mail->Host = "mail.authsmtp.com";
    $mail->Port = "25"; // or 587
    $mail->IsHTML(true);
    $mail->Username = "xxxx";
    $mail->Password = "xxxxxxx";
    $mail->SetFrom("xxxxxxxxx");
    $mail->Subject = $sub1;
    $mail->Body = $text_mail;
    $mail->AddAddress("xxx@xxxxxx.com");
    $mail->AddAddress("xxxxxxx@gmail.com");

    $mail->AddAttachment($my_file, $my_path);
     if(!$mail->Send()){
     echo "Mailer Error: " . $mail->ErrorInfo;
    }
    else{
     echo "Message has been sent";
    }

Solution

  • This works:

    $my_path ="/data/data/www/fms/pricelists/example.xls";
    
    include "class.phpmailer.php"; // include the class file name
    $mail = new PHPMailer(); // create a new object
    $mail->IsSMTP(); // enable SMTP
    $mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
    $mail->SMTPAuth = true; // authentication enabled
    //$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for GMail
    $mail->Host = "mail.authsmtp.com";
    $mail->Port = "25"; // or 587
    $mail->IsHTML(true);
    $mail->Username = "xxxx";
    $mail->Password = "xxxxxxx";
    $mail->SetFrom("xxxxxxxxx");
    $mail->Subject = $sub1;
    $mail->Body = $text_mail;
    $mail->AddAddress("xxx@xxxxxx.com");
    $mail->AddAddress("xxxxxxx@gmail.com");
    
    $mail->AddAttachment($my_path);
    if(!$mail->Send()){
     echo "Mailer Error: " . $mail->ErrorInfo;
    }else{
     echo "Message has been sent";
    }