MBF Knowledge Base

I am trying to email a file that exists on my server using PHPMailer. When I run the script, I receive a "Could not access file" error and the email sends, but without the attachment. How do I resolve this?

There are a few reasons this can happen:

1.) If you are not using your $filename variable.

2.) You are not passing $newpath into your function as a parameter.

3.) AddAttachment is trying to attach the file but it passes it an empty string.

Try the following to resolve the issue:

1.) Try passing the correct path into your function like this:

function smtpmailer($to, $from, $from_name, $subject, $body, $newpath)

Then, call it like this:

smtpmailer('xxxxxxx@me.com', 'xxxxxxxx@gmail.com', 'Name', 'test mail message', 'This is a test', $newpath);

2.) Make sure the user/group that Apache and PHP are running under have at least READ permissions on the file. This issue is common if you're working on a server and upload files using sFTP/SSH as a different user. Find out what user and group apache/php are running under and give that user and group read access to the file you are trying to attach.

3.) You may need to run an extra command in your php script such as move_uploaded_file(). This will move a file to an internal directory from your host home, then you will be able to send the attachment.