Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Renaming attachments not working #35

Closed
Bogardo opened this issue Feb 2, 2014 · 7 comments
Closed

Renaming attachments not working #35

Bogardo opened this issue Feb 2, 2014 · 7 comments

Comments

@Bogardo
Copy link

Bogardo commented Feb 2, 2014

I haven't been able to send attachments using the new method to rename the attachments.

Here is the test code I used:

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

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

# Make the call to the client.

try {
    //Version 1 | from docs (old)
    $result = $mgClient->sendMessage("$domain",
        array(
            'from'    => 'Excited User <me@samples.mailgun.org>',
            'to'      => 'foo@bar.com',
            'subject' => 'Version 1 | from docs (old)',
            'text'    => 'Testing some Mailgun awesomness!',
            'html'    => '<html>HTML version of the body</html>'
        ),
        array(
            'attachment' => array(
                '@C:\Projects\Sandbox\mailgun\testfile_1.txt',
                '@C:\Projects\Sandbox\mailgun\testfile_2.txt'
            )
        )
    );
} catch (Exception $e) {
    var_dump($e->getMessage());
}

try {
    // Version 2 | New version with attachment renaming (without 'attachment' key)
    $result = $mgClient->sendMessage("$domain",
        array(
            'from'    => 'Excited User <me@samples.mailgun.org>',
            'to'      => 'foo@bar.com',
            'subject' => "Version 2 | New version with attachment renaming (without 'attachmnt' key)",
            'text'    => 'Testing some Mailgun awesomness!',
            'html'    => '<html>HTML version of the body</html>'
        ),
        array(
            array(
                'filePath' => '@C:\Projects\Sandbox\mailgun\testfile_1.txt',
                'remoteName' => 'NumberOne.txt'
            ),
            array(
                'filePath'   => '@C:\Projects\Sandbox\mailgun\testfile_2.txt', 
                'remoteName' => 'NumberTwo.txt'
            )
        )
    );
} catch (Exception $e) {
    var_dump($e->getMessage());
}

try {
    //Version 3 | New version with attachment renaming (with 'attachment' key) 
    $result = $mgClient->sendMessage("$domain",
        array(
            'from'    => 'Excited User <me@samples.mailgun.org>',
            'to'      => 'foo@bar.com',
            'subject' => "Version 3 | New version with attachment renaming (with 'attachment' key)",
            'text'    => 'Testing some Mailgun awesomness!',
            'html'    => '<html>HTML version of the body</html>'
        ),
        array(
            'attachment' => array(
                'filePath' => '@C:\Projects\Sandbox\mailgun\testfile_1.txt',
                'remoteName' => 'NumberOne.txt'
            ),
            'attachment' => array(
                'filePath'   => '@C:\Projects\Sandbox\mailgun\testfile_2.txt', 
                'remoteName' => 'NumberTwo.txt'
            )
        )
    );
} catch (Exception $e) {
    var_dump($e->getMessage());
}

After running the above I only receive the first two emails.
Here are the results per version/mail:

  • Version 1: No problems, email arrives with the two attachments
  • Version 2: Email arrives without attachments
  • Version 3: Throws exception "Unable to open NumberTwo.txt for reading" (see below)
Fatal error: Uncaught exception 'Guzzle\Common\Exception\InvalidArgumentException' with message 'Unable to open NumberTwo.txt for reading' in C:\Projects\Sandbox\mailgun\vendor\guzzle\guzzle\src\Guzzle\Http\Message\PostFile.php:53 
Stack trace: 
#0 C:\Projects\Sandbox\mailgun\vendor\guzzle\guzzle\src\Guzzle\Http\Message\PostFile.php(28): Guzzle\Http\Message\PostFile->setFilename('NumberTwo.txt') 
#1 C:\Projects\Sandbox\mailgun\vendor\guzzle\guzzle\src\Guzzle\Http\Message\EntityEnclosingRequest.php(199): Guzzle\Http\Message\PostFile->__construct('attachment', 'NumberTwo.txt', NULL, NULL) 
#2 C:\Projects\Sandbox\mailgun\vendor\mailgun\mailgun-php\src\Mailgun\Connection\RestClient.php(55): Guzzle\Http\Message\EntityEnclosingRequest->addPostFile('attachment', 'NumberTwo.txt') 
#3 C:\Projects\Sandbox\mailgun\vendor\mailgun\mailgun-php\src\Mailgun\Mailgun.php(60): Mailgun\Connection\RestClient->post('******/...', Array, Array) 
#4 C:\Projects\Sandbox\mailgun\vendor\mailgun\mailgun-php\src\Mailgun\Mailgun.php(38): Mailgun\Mail in C:\Projects\Sandbox\mailgun\vendor\guzzle\guzzle\src\Guzzle\Http\Message\PostFile.php on line 53
@travelton
Copy link
Contributor

On Windows, you may need to format the filepath as such:

'filePath'   => 'C:\\Projects\\Sandbox\\mailgun\\testfile_2.txt', 

Can you try that out and report the result?

@Bogardo
Copy link
Author

Bogardo commented Feb 3, 2014

I just tried it with the new filepath format and it has the exact same outcome:

  • Version 1: No problems, email arrives with the two attachments
  • Version 2: Email arrives without attachments
  • Version 3: Throws exception "Unable to open NumberTwo.txt for reading" (see below)

I will boot up a Vagrant instance with Ubuntu to see if this problem exists only on Windows based machines.

And the results are in:
I booted up an instance of Vagrant running Ubuntu 12.04 x64.
I used the exact same code and only replaced the filepaths with: /var/www/testfile_1.txt and /var/www/testfile_2.txt.

Unfortunately I get the same results again:

  • Version 1: No problems, email arrives with the two attachments
  • Version 2: Email arrives without attachments
  • Version 3: Throws exception "Unable to open NumberTwo.txt for reading" (see below)
Fatal error: Uncaught exception 'Guzzle\Common\Exception\InvalidArgumentException' with message 'Unable to open NumberTwo.txt for reading' in /var/www/vendor/guzzle/guzzle/src/Guzzle/Http/Message/PostFile.php:53 
Stack trace: 
#0 /var/www/vendor/guzzle/guzzle/src/Guzzle/Http/Message/PostFile.php(28): Guzzle\Http\Message\PostFile->setFilename('NumberTwo.txt') 
#1 /var/www/vendor/guzzle/guzzle/src/Guzzle/Http/Message/EntityEnclosingRequest.php(199): Guzzle\Http\Message\PostFile->__construct('attachment', 'NumberTwo.txt', NULL, NULL) 
#2 /var/www/vendor/mailgun/mailgun-php/src/Mailgun/Connection/RestClient.php(55): Guzzle\Http\Message\EntityEnclosingRequest->addPostFile('attachment', 'NumberTwo.txt') 
#3 /var/www/vendor/mailgun/mailgun-php/src/Mailgun/Mailgun.php(60): Mailgun\Connection\RestClient->post('********/...', Array, Array) 
#4 /var/www/vendor/mailgun/mailgun-php/src/Mailgun in /var/www/vendor/guzzle/guzzle/src/Guzzle/Http/Message/PostFile.php on line 53

@Bogardo
Copy link
Author

Bogardo commented Feb 3, 2014

I had some spare time so I decided to dig into the code myself.

In Pull Request #29 you posted the following code example for making use of the ability to rename attachments:

# First, instantiate the SDK with your API credentials and define your domain. 
$mg = new Mailgun("key-example");
$domain = "example.com";

# Now, compose and send your message.
$mg->sendMessage($domain, array('from'    => 'bob@example.com', 
                                'to'      => 'sally@example.com', 
                                'subject' => 'The PHP SDK is awesome!', 
                                'text'    => 'It is so simple to send a message.'), 
                                array(array('filePath'   => '@/receipts/customer892374892.pdf', 
                                            'remoteName' => 'Receipt.pdf'),
                                      array('filePath'   => '@/taxforms/IRS/1099.pdf', 
                                            'remoteName' => 'IRS-1099.pdf')));

The above will not work because of the missing "attachment" array.
In the post method in Mailgun/Connection/RestClient.php on line 33 it is always looking for an "attachment" key (or inline)

If you change the code example to the one below, it will work as expected.

# First, instantiate the SDK with your API credentials and define your domain. 
$mg = new Mailgun("key-example");
$domain = "example.com";

# Now, compose and send your message.
$mg->sendMessage($domain, array('from'    => 'bob@example.com', 
                                'to'      => 'sally@example.com', 
                                'subject' => 'The PHP SDK is awesome!', 
                                'text'    => 'It is so simple to send a message.'), 
                                array("attachment" => array(
                                    array('filePath'   => '@/receipts/customer892374892.pdf', 
                                          'remoteName' => 'Receipt.pdf'),
                                    array('filePath'   => '@/taxforms/IRS/1099.pdf', 
                                          'remoteName' => 'IRS-1099.pdf'))));

I took a look at the tests and I noticed that for testing the attachments you always use the MessageBuilder class, which does in fact work correctly. Are there any plans to add tests for attachments without MessageBuilder?

@Bogardo
Copy link
Author

Bogardo commented Feb 6, 2014

@travelton Could you please confirm this?

@travelton
Copy link
Contributor

@Bogardo Yes. However, Message Builder is simply building an array of arrays. So, given that the array of attachments is formatted properly, you should be able to include them without using Message Builder. With that said, they should be tested.

@travelton
Copy link
Contributor

Closing this issue now, the tests will be included in the next version.

@kushalappamp
Copy link

using the same code for attachments,

i am getting error lik
Caught exception: Unable to open \uploads\file.txt for reading

array('attachment' => array(
array('filePath' => '@\uploads\file.txt',
'remoteName' => 'file.txt'),

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants