Reputation: 12222
PHPMailer checks is_file
for every attachment (in addAttachment
function, in class.phpmailer.php file):
if (!@is_file($path)) {
throw new phpmailerException($this->lang('file_access') . $path, self::STOP_CONTINUE);
}
My problem is that I can make work is_file
only giving full local path to file, not URLs:
is_file('C:/wamp/www/myFolder/rocks.png'); //True
is_file('http://localhost/myFolder/rocks.png'); //False :(
So I can't attach any file from the remote server.
What am I doing wrong?? It can be a permission issue?
EDIT:
I know that there are other ways to check if file exists.
But is_file
is in the PhpMailer library, I prefer to not touch it and I want to know if it's possible to make it work using its methods.
Thanks.
Upvotes: 13
Views: 19947
Reputation: 37750
It doesn't need a workaround, you're just using a function explicitly intended for local files on a remote resource. This is a deliberate choice by PHPMailer because it dos not want to act as an HTTP client – that's an entirely separate job better handled by other code. To attach a remote resource without involving local files, just do this:
$mail->addStringAttachment(file_get_contents($url), 'filename');
While this makes the HTTP request your responsibility (as PHPMailer intends), I would not recommend this direct inline approach because it makes error handling more difficult (e.g. if the URL fails to respond).
This is essentially a duplicate of this question.
Upvotes: 28
Reputation: 1
I'm just a beginner in web development, maybe I'm wrong, but I have this code working:
$filePath1 = __DIR__ . "/files/" . $_FILES['foto-item']['name'];
__DIR__
indicates the absolute path to the file. It works on both local and remote servers.
Upvotes: 0
Reputation: 151604
Later in code it uses file_get_contents()
to include the attachment's contents. While file_get_contents()
supports HTTP, is_file()
doesn't.
Given you don't want to alter PhpMailer, you'll have to download the file from HTTP yourself and provide the temporary path to PhpMailer. After sending you can delete the temporary file.
Something like this (from PHP manual: sys_get_temp_dir and Download File to server from URL):
$attachmentUrl = "http://example.com/image.jpg";
$tempFile = tempnam(sys_get_temp_dir(), 'mailattachment');
file_put_contents($tempFile, $attachmentUrl);
Then you can attach $tempFile
, send your mail and unlink($tempFile)
.
Upvotes: 1
Reputation: 212412
Quoting from the PHP docs:
As of PHP 5.0.0, this function can also be used with some URL wrappers. Refer to Supported Protocols and Wrappers to determine which wrappers support stat() family of functionality.
Of the standard streams, the following support stat()
and the following do not
While the following are limited
Upvotes: 0