Phpmailer Could not access file

Hi all,
i have spent almost a day to find the problem. still i could not. please help.
The email delivery works charm. but attachment is giving the “Could not access file” error.

<?php

error_reporting(E_ALL);
error_reporting(-1); 
ini_set("display_errors", "true");

//include ($_SERVER["DOCUMENT_ROOT"] . "/config.php");



$validExtensions = array("jpeg", "jpg", "png", "pdf", "doc", "docx"); // valid extensions
$path = "uploads/"; // upload directory

if(isset($_FILES["file_upload"])){
    //echo "yes";
    
    $name=$_POST["name"];
    if($name==""){
       echo "<div class='alert alert-danger' role='alert'>Name is required.</div>";
       exit;
    }
    
    $tel=$_POST["tel"];
    if($tel==""){
       echo "<div class='alert alert-danger' role='alert'>Phone Number is Required.</div>";
       exit;
    } 
    
    $email=$_POST["email"];
    if($email==""){
       echo "<div class='alert alert-danger' role='alert'Email is Required.</div>";
       exit;
    }   
    
    $interest=$_POST["interest"];
    $age_range=$_POST["age_range"];
    $education=$_POST["education"];
    $residency=$_POST["residency"];     
    
    
   //Handle upload//
   $file_name = $_FILES["file_upload"]["name"];
   $tmp = $_FILES["file_upload"]["tmp_name"];

   // Get the uploaded file's extension
   $ext = strtolower(pathinfo($file_name, PATHINFO_EXTENSION));
   // Generate a unique file name using the rand() function
   $file_1 = rand(1000, 1000000) . $file_name;

   // Check if the file format is valid
   if (in_array($ext, $validExtensions)) { 
     $path = $path . $file_1; // meaning : "/uploads" . uploaded file name
     
     if (move_uploaded_file($tmp, $path)) {
        //echo "done";
     } 

   }else{
     echo "invalid";
   }
   
   
   /* =========Mail Part========= */
    $subject="New Message from Website Contact From";
    
    
    $message = "
    
    	<div>
    	
    				<table>
    				   <tr><td><span><h3>A New message has been received via xxx.sg website contact form. The Details are as followes : </h3></span></td></tr>				   
    				</table>
    				
    				<br>
    				
    				<table>
    				   <tr><td><span>Name : $name </span></td></tr>
    				   <tr><td><span>Telephone : $tel </span></td></tr>
    				   <tr><td><span>Email : $email </span></td></tr>
    				   <tr><td><span>Interest : $interest </span></td></tr>		
    				   <tr><td><span>Age Range : $age_range </span></td></tr>	
    				   <tr><td><span>Education : $education </span></td></tr>	
    				   <tr><td><span>Residency : $residency </span></td></tr>					   
    				</table>
    
    	
    	</div>";
    	
    	
    require_once "include/phpmailer/src/PHPMailer.php";
    require_once "include/phpmailer/src/SMTP.php";
    require_once "include/phpmailer/src/Exception.php";

    $mail = new PHPMailer\PHPMailer\PHPMailer();
    //$mail = new PHPMailer(true);                              
    try {
        //Server settings
        $mail->SMTPDebug = 2;                                 
        //$mail->isSMTP();                                      
                                 
    
        //Recipients
        $mail->setFrom("webmaster@xxx.sg", "Webmaster");          //This is the email your form sends From
        $mail->addAddress("akram@xxx.sg", "Akram"); // Add a recipient address
        //$mail->addAddress("contact@example.com");               // Name is optional
        //$mail->addReplyTo(" . $email . ", " . $email . ");
        
        //$mail->addCC("cc@example.com");
        //$mail->addBCC("bcc@example.com");
    
    
        //Attachments
        echo $path . "<br>";
        $mail->addAttachment(" . $path . ");         // Add attachments
        //$mail->addAttachment("uploads/754127Chicken-Supreme-Pizza.png", "");    // Optional name


        //Content
        $mail->isHTML(true);                                  // Set email format to HTML
        $mail->Subject = "New Message from Website Contact From";
        $mail->Body    = " . $message . ";
        //$mail->AltBody = "This is the body in plain text for non-HTML mail clients";
    
        $mail->send();
        echo "Message has been sent";
    } catch (Exception $e) {
        echo "Message could not be sent.";
        echo "Mailer Error: " . $mail->ErrorInfo;
    }  	
    /* ========= /Mail Part========= */
   
   
}  


?>




image

Notes :

  • the first line of the above image shows echo of the path of dynamically uploaded file.
  • tried chmod 0777 on the file before the attachment line. but no luck.
  • If i hard code the attachment path it works.
$mail->addAttachment("uploads/180774BBQ-Texas-Pizza.png", ""); 

even i tried with full url. but same result. first line show file path is working fine when past on the browser. but in php it says could not access file.

image

This using a space, a dot, another space, the $path, followed by a space, a dot, and a space as the value. Just use the variable $path as the parameter.

Oh maan…, Thanks a lot.

$mail->addAttachment($path);

Worked Charm!

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.