Search code examples
phpattachmentmailgunrackspace

Mailgun : Fatal error: Uncaught exception 'Guzzle\Common\Exception\InvalidArgumentException' with message 'Unable to open


I have used the following code and I have placed these files in the same directory as of the script I am running to send the mail. The exception continues to be thrown even when I try to give an online link.

I am trying this piece of PHP code in my local host which you can figure out from the error below

# Include the Autoloader (see "Libraries" for install instructions)
require 'mail/vendor/autoload.php';
use Mailgun\Mailgun;

# Instantiate the client.
$mgClient = new Mailgun('key-secret');
$domain = "mydomain";

    $result = $mgClient->sendMessage($domain, array(
    'from'    => 'email',
             'to'      => 'email',
             'bcc'     => 'email',
             'subject' => "Sending Attachment 2",
             'text'    => "Sending Attachment",
             'html'    => "Sending Attachment",
             'attachment-1'=> 'test.xml'
),  array("attachment" => array(
                                    array('filePath'   => '@/file.txt', 
                                          'remoteName' => 'file.txt'),
                                    array('filePath'   => '@/test.txt', 
                                          'remoteName' => 'test.txt'))));

Fatal error: Uncaught exception 'Guzzle\Common\Exception\InvalidArgumentException' with message 'Unable to open /file.txt for reading' in C:\xampp\htdocs\zendeskapis\mail\vendor\guzzle\guzzle\src\Guzzle\Http\Message\PostFile.php:53 Stack trace: #0 C:\xampp\htdocs\zendeskapis\mail\vendor\guzzle\guzzle\src\Guzzle\Http\Message\PostFile.php(28): Guzzle\Http\Message\PostFile->setFilename('@/file.txt') #1 C:\xampp\htdocs\zendeskapis\mail\vendor\guzzle\guzzle\src\Guzzle\Http\Message\EntityEnclosingRequest.php(199): Guzzle\Http\Message\PostFile->__construct('attachment', '@/file.txt', NULL, 'file.txt') #2 C:\xampp\htdocs\zendeskapis\mail\vendor\mailgun\mailgun-php\src\Mailgun\Connection\RestClient.php(52): Guzzle\Http\Message\EntityEnclosingRequest->addPostFile('attachment', '@/file.txt', NULL, 'file.txt') #3 C:\xampp\htdocs\zendeskapis\mail\vendor\mailgun\mailgun-php\src\Mailgun\Mailgun.php(80): Mailgun\Connection\RestClient->post('hotelpalmshore....', Array, Array) #4 C:\xampp\htdocs\zendeskapis\mail\vendor\mailgun\mailgun-php\ in C:\xampp\htdocs\zendeskapis\mail\vendor\guzzle\guzzle\src\Guzzle\Http\Message\PostFile.php on line 53


Solution

  • As the exception state it, your file path doesn't seem to be correct.

    Assuming you're on a nix system, your file.txt should be right under / which seems quite unlikely to me.

    If you're on a windows system the path /file.txt does not have any meaning.

    Try to hardcode first your path, once it will work set it dynamically