php - PHPMailer accepting raw file name as attachment, but not as variable
Get the solution ↓↓↓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.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: you must enable the openssl extension in your php.ini to load information from https://repo.packagist.org
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.