0

When i used to send email without attachments with simple text body, i got an error with Message could not be sent. Mailer Error: Could not access file: ./attachment/

if i comment my function for attachment, my code is working fine.

$mail->send function try to search for attachment folder every time. even if file is not present in the email i.e file is contains only text.This is the error i got every time.

<?php
    
include('db.php');
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require_once "vendor/autoload.php";
    
$id = $_GET['id'];
    
$query = "select * from access where uid='$id'";
$result = mysqli_query($conn,$query);
$row = mysqli_fetch_assoc($result);
    
$mail = new PHPMailer(true);
    
try {  
    
  $mail->setFrom('sender@gmail.com');
  $mail->addAddress('receiver@gmail.com');
    
  $array = explode(", ",$row['attachments']);
  $count = count($array);
  if($count > 0 && $row['attachments'] != 'null'){
    for ($i=0; $i < $count ; $i++) {
      $file_to_attach = './attachment/' . $array[$i];
      $mail->addAttachment($file_to_attach, $array[$i]); 
    }
  }
    
  $mail->isHTML(true);                                  
  $mail->Subject = $row['subject'];
  $mail->Body    = $row['body'];
    
  $mail->send();
  echo 'Message has been sent';
} catch (Exception $e) {
  echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
Atomzwieback
  • 585
  • 6
  • 18
  • Can you add the results of `var_dump($row['attachments']);` to see what the values contain. – Nigel Ren Jul 15 '20 at 08:01
  • It's possible that your exploded array contains empty values. You can see this answer on how to prevent this: https://stackoverflow.com/questions/3432183/how-can-i-remove-all-empty-values-when-i-explode-a-string-using-php – CountKyle Jul 15 '20 at 08:18

2 Answers2

0

this is solved, i put my variable where i insert value for attachments in if condition, i checked value is not equal to null in database with file name i inserted in database. if file name is not empty only then my code to save attachment will work, otherwise it'll not work.

  

    if($row['attachments']!=null) 
            {
                $array = explode(", ",$row['attachments']);
                $count = count($array);
                if($count > 0 && $row['attachments'] != 'null'){
                    for ($i=0; $i < $count ; $i++) {
                            $file_to_attach = './attachment/' . $array[$i];
                            $mail->addAttachment($file_to_attach, $array[$i]); 
                    }
                }
            }

  • This solves only the first of the things I mentioned (checking your input data). You will still get an exception if it can't read the file for some reason. – Synchro Jul 15 '20 at 08:28
0

You have enabled exceptions in PHPMailer, and you're calling addAttachments with parameters that fail (e.g. null, or a path to a file that doesn't exist, or you don't have permission to read), so it's throwing an exception, as expected. So you have two things to do: figure out why it can't read the file, and add code that deals with it failing, like this:

   if($count > 0 && $row['attachments'] != 'null'){
        for ($i=0; $i < $count ; $i++) {
                $file_to_attach = './attachment/' . $array[$i];
                try {
                    $mail->addAttachment($file_to_attach, $array[$i]);
                } catch (Exception $e) {
                    echo "Could not read file $file_to_attach)\n";
                }
        }
    }

This code allows the send to continue anyway – it's up to you whether that' s what you want to do or not.

Synchro
  • 35,538
  • 15
  • 81
  • 104