php - PHPMailer accepting raw file name as attachment, but not as variable ← (PHP)

one text

I try to build a PHP script in order to send mail with attachment, using PHPMailer module.

When I put the raw file name into this line:

$docpath = "/tmp/document.pdf"; 
$mail->addAttachment($docpath);

It works perfectly; the file is attached and the mail is sent.

The problem occurs when I put the file name into a variable, like below:

$docpath = "/tmp/$attachmentname"; 
$mail->addAttachment($docpath);

It returns this error message:

Could not access file: /tmp/document.pdf

I tried with exactly the same file, nothing changed between the two trials...

I tried to change the syntax by adding quotes, like below:

$docpath = "/tmp/$attachmentname";
$mail->addAttachment("'".$docpath."'"); 

But same result, or almost:

Could not access file: '/tmp/document.pdf'

I finally tried this syntax:

$docpath = "/tmp/" . $attachmentname ."";

But no way...:

Could not access file: /tmp/document.pdf

The correct name is displayed in the error message, whereas the file did not move from the /tmp/ directory, and kept the same permissions.

Thanks for your help.

Source