mailgun / mailgun-php

Mailgun's Official SDK for PHP
http://www.mailgun.com
MIT License
1.1k stars 314 forks source link

unable to use mailgun php 2.1.1 #143

Closed dfrozenprogrammer closed 6 years ago

dfrozenprogrammer commented 8 years ago

when i am trying to send a mail using the 2.1.1 version of mailgun api i get this particular error :- : No stream factories found. To use Guzzle or Diactoros factories install php-http/message and the chosen message implementation. please tell me if any one has a solution for this .

Nyholm commented 8 years ago

The error message tells you what to do. Just install guzzlehttp/psr7. Or zendframework/diactoros.

dfrozenprogrammer commented 8 years ago

its just saying that no stream factories are found . and according to the error its saying to either use guzzle or diactoros .

aalicki commented 8 years ago

Also having the same problem, after installing psr/http-message via composer, the error still exists.

Nyholm commented 8 years ago

Please make sure to run the following command to fix the issue

$ composer require guzzlehttp/psr7 psr/http-message
aalicki commented 8 years ago

I've ran both of those commands, however the problem still exists.

Nyholm commented 8 years ago

What error message do you get?

aalicki commented 8 years ago

: No stream factories found. To use Guzzle or Diactoros factories install php-http/message and the chosen message implementation.

This same as the OP. This was after running the two commands you provided.

YupItsZac commented 8 years ago

@aalicki, installing those dependencies should have fixed the problem. What was the output after you ran the composer command?

aalicki commented 8 years ago

Composer reported that those libs were installed properly, and I can verify they exist within the /vendors directory.

The exact error (now different after re-installing the Guzzle and PSR packages:

Type: InvalidArgumentException

Message: Invalid resource type: object

File: /websites/carrepair.com/vendor/guzzlehttp/psr7/src/functions.php

Line: 116
YupItsZac commented 8 years ago

@aalicki, what's your code look like where this fails? Possibly related to https://github.com/guzzle/guzzle/issues/814?

aalicki commented 8 years ago

Here is the code with the issues. I checked into guzzle/guzzle#814, but didn't see much relation.

# Instantiate the client.
        $mgClient = new Mailgun('xxxxxx');
        $domain = "xxxxxxx";

        # Make the call to the client.
        $mgClient->sendMessage("$domain",
            array('from'  => 'Mailgun Sandbox <postmaster@xxxxxx>',
                'to'      => 'Adam <adam@xxxxxx>',
                'subject' => 'Contact Request',
                'text'    => 'Sample email text'));
Nyholm commented 8 years ago

If you have the latest version of composer, and you have been running this command:

composer require mailgun/mailgun-php php-http/curl-client guzzlehttp/psr7 php-http/message

... and you still get an error message complaining about the stream factories ("No stream factories found.") Then is the only think I can think of now is that you are not using the composer autoloader. Is that right?

shakaran commented 7 years ago

@aalicki could you update the feedback for this issue? So devs knows about if it is still a problem or they could close it. If not probably will be closed by inactivity at some point

dani0332 commented 6 years ago

I think the issue still stands, I am using PHP 7.0.8 AND Code-igniter 3.1.6 and encounters the same issue.

Error looks like: screencapture-localhost-81-darzee-public_html-cart-cartmail-1512653387519

files in root_dir > application > vendor looks like

capture

I installed the following libraries via composer

Note: when I try to install above libs composer there's nothing to install or update.

My Controller looks like:

require '../vendor/autoload.php';
use Mailgun\Mailgun;

class CartU extends User_Controller
{
    /**
     * CartU constructor.
     */
    public function __construct()
    {
        parent::__construct();
        // $this->data['user']['head']['pageLevelPlugins']['css'] = [];
        $this->data['user']['foot']['pageLevelPlugins']['js'] = [];
        $this->data['user']['page_js'] = 'cart/user-js';

        //$this->load->model('Dress_categories_m', 'dc', TRUE);
        $this->load->library('my_encrypt');
    }   

    public function cartMail()
    {
        # Instantiate the client.
        $mgClient = new Mailgun('key-381e75f9e099ae4786faa32cf259f539');
        $domain = "rs2a246a80c26d44c7b2d4a16a4fb999cc.mailgun.org";

        # Make the call to the client.
        $result = $mgClient->sendMessage($domain, array(
            'from' => 'Excited User <mailgun@knblogodesign.com>',
            'to' => 'Daniyal <dani.0332@gmail.com>',
            'subject' => 'Hello',
            'text' => 'Testing some Mailgun awesomness!'
        ));

    }
}
DavidGarciaCat commented 6 years ago

Hey @dani0332

SDK have had a few changes, can you please confirm the current installed version?

composer show mailgun/*

Cheers,

Nyholm commented 6 years ago

As the exception says, you are missing a PSR7 implementation. Try installing guzzlehttp/psr7.

Also see: http://docs.php-http.org/en/latest/discovery.html#common-errors

dani0332 commented 6 years ago

@DavidGarciaCat, it says mailgun/mailgun-php 2.3.4 The Mailgun SDK provides methods for all API functions.

@Nyholm After running composer require guzzlehttp/psr7

I got:

Using version ^1.4 for guzzlehttp/psr7
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Nothing to install or update
Generating autoload files

My composer.json file looks like:

{
    "description": "The CodeIgniter framework",
    "name": "codeigniter/framework",
    "type": "project",
    "homepage": "https://codeigniter.com",
    "license": "MIT",
    "support": {
        "forum": "http://forum.codeigniter.com/",
        "wiki": "https://github.com/bcit-ci/CodeIgniter/wiki",
        "slack": "https://codeigniterchat.slack.com",
        "source": "https://github.com/bcit-ci/CodeIgniter"
    },
    "require": {
        "php": ">=7.0.8",
        "mailgun/mailgun-php": "^2.3",
        "php-http/guzzle6-adapter": "^1.1",
        "php-http/message": "^1.6",
        "guzzlehttp/psr7": "^1.4",
        "php-http/curl-client": "^1.7"
    },
    "suggest": {
        "paragonie/random_compat": "Provides better randomness in PHP 5.x"
    },
    "require-dev": {
        "mikey179/vfsStream": "1.1.*",
        "phpunit/phpunit": "4.* || 5.*"
    }
}
Nyholm commented 6 years ago

Great. Im happy it worked out for you.

DavidGarciaCat commented 6 years ago

Hey @dani0332

Please consider to use the new way offered by this SDK to make your API calls, the method that you are using is flagged as deprecated:

$mailgun = Mailgun::create('your API key goes here's);
$mailgun->messages()->send(
    'domain',
    [
        'argumentKey' => 'argumentValue',
    ]
);
DavidGarciaCat commented 6 years ago

Mailgun::create('your API key goes here'); Mobile spell checker added the final s, please ignore it

dani0332 commented 6 years ago

@Nyholm I never said it worked out, the error still as it was at the start.

@DavidGarciaCat I updated my method like this (but still the same error).

public function cartMail()
    {
        $apiKey = "API KEY";
        $domain = "DOMAIN";

        $mailgun = Mailgun::create($apiKey);
        $mailgun->messages()->send(
            $domain,
            [
                'from' => 'Excited User <mailgun@knblogodesign.com>',
                'to' => 'Daniyal <dani.0332@gmail.com>',
                'subject' => 'Hello',
                'text' => 'Testing some Mailgun awesomness!'
            ]
        );

    }
DavidGarciaCat commented 6 years ago

Hey @dani0332

Based on the screenshot you added, the php-http/message should be installed

screen shot 2017-12-08 at 12 17 09

However I can see it is already on your composer.json file so:

1) Can you please confirm it was installed and what version?

composer show php-http/*

2) Can you please provide an up-to-date backtrace of the error, now that you are using the new method provided by the SDK?

dani0332 commented 6 years ago

After running: composer show php-http/* I got:

php-http/client-common            1.7.0  Common HTTP Client implementations and tools for HTTPlug
php-http/curl-client              v1.7.0 cURL client for PHP-HTTP
php-http/discovery                1.3.0  Finds installed HTTPlug implementations and PSR-7 message factories
php-http/guzzle6-adapter          v1.1.1 Guzzle 6 HTTP Adapter
php-http/httplug                  v1.1.0 HTTPlug, the HTTP client abstraction for PHP
php-http/message                  1.6.0  HTTP Message related tools
php-http/message-factory          v1.0.2 Factory interfaces for PSR-7 HTTP Message
php-http/multipart-stream-builder 1.0.0  A builder class that help you create a multipart stream
php-http/promise                  v1.0.0 Promise used for asynchronous HTTP requests

The Error BackTrace looks like: screencapture-localhost-81-darzee-public_html-cart-cartmail-1512740408191

dani0332 commented 6 years ago

I realized I had two vendor folders

  1. root_dir > application > vendor
  2. root_dir > vendor

So, I moved (merged) vendor folder out from root_dir > application > vendor to root_dir > vendor

After that I Re-run my version commands you asked for:

E:\xampp\htdocs\darzee>composer show mailgun/*
mailgun/mailgun-php v2.4.0 The Mailgun SDK provides methods for all API functions.

E:\xampp\htdocs\darzee>composer show php-http/*
php-http/client-common            1.7.0  Common HTTP Client implementations and tools for HTTPlug
php-http/curl-client              v1.7.0 cURL client for PHP-HTTP
php-http/discovery                1.3.0  Finds installed HTTPlug implementations and PSR-7 message factories
php-http/guzzle6-adapter          v1.1.1 Guzzle 6 HTTP Adapter
php-http/httplug                  v1.1.0 HTTPlug, the HTTP client abstraction for PHP
php-http/message                  1.6.0  HTTP Message related tools
php-http/message-factory          v1.0.2 Factory interfaces for PSR-7 HTTP Message
php-http/multipart-stream-builder 1.0.0  A builder class that help you create a multipart stream
php-http/promise                  v1.0.0 Promise used for asynchronous HTTP requests

Which in-turn change the error to:

screencapture-localhost-81-darzee-public_html-cart-cartmail-1512741234414

And for ref. my controller class looks like this:

require '../vendor/autoload.php';
use Mailgun\Mailgun;

class CartU extends User_Controller
{   
    public function __construct()
    {
        parent::__construct();
        // $this->data['user']['head']['pageLevelPlugins']['css'] = [];
        $this->data['user']['foot']['pageLevelPlugins']['js'] = [];
        $this->data['user']['page_js'] = 'cart/user-js';

        //$this->load->model('Dress_categories_m', 'dc', TRUE);
        $this->load->library('my_encrypt');
    } 

    public function cartMail()
    {
        $apiKey = "API KEY";
        $domain = "Domain Name";

        $mailgun = Mailgun::create($apiKey);
        $mailgun->messages()->send(
            $domain,
            [
                'from' => 'Excited User <mailgun@knblogodesign.com>',
                'to' => 'Daniyal <dani.0332@gmail.com>',
                'subject' => 'Hello',
                'text' => 'Testing some Mailgun awesomness!'
            ]
        );
    }   
}
DavidGarciaCat commented 6 years ago

Hey @Nyholm

Is there any known issue with the SSL connection? Potential issue with local config maybe?

Nyholm commented 6 years ago

Not as far as I know. But Windows, PHP and SSL is usually an issue...

TylerVigario commented 6 years ago

I am getting this error as well. Shouldn't Mailgun include all required packages via Composer?

TylerVigario commented 6 years ago

@dani0332

You need default SSL cert, follow this guide - https://stackoverflow.com/questions/29822686/curl-error-60-ssl-certificate-unable-to-get-local-issuer-certificate

And DO NOT combine the Code Igniter Composer (root/composer.json & root/vendor) with the applications Composer (root/application/composer.json & root/application/vendor) as these are designed to be seperate.

DavidGarciaCat commented 6 years ago

Hey there,

Mailgun uses virtual packages as described on the Readme file, so you should follow the install steps like any other vendor package.

Are you getting an error if you install all of them?

TylerVigario commented 6 years ago

@DavidGarciaCat I was able to get it working properly after installing all the necessary packages. I have never really worked with Guzzle personally so have no idea what the normal protocol is for that. However, this thread helped out a lot.

I guess I missed the install steps like a few others but thanks for your help and clarifications

DavidGarciaCat commented 6 years ago

Hi @MeekLogic

Could you please include the steps that you did? If we're missing anything in our README then we can add it.

Thanks,

TylerVigario commented 6 years ago

@DavidGarciaCat – Well in this thread there were two issues.

Missing Composer Dependencies

No stream factories found. To use Guzzle... and No uri factories found. To use Guzzle...

Taken from the require-dev within composer.json

Curl for PHP on Windows (which is used by the Guzzle library) needs a default root certificate

cURL error 60: SSL certificate: unable to get local issuer certificate...

Followed the directions from top solution in this link – https://stackoverflow.com/questions/29822686/curl-error-60-ssl-certificate-unable-to-get-local-issuer-certificate

DavidGarciaCat commented 6 years ago

Hi @MeekLogic

Missing Composer Dependencies

Please refer to the Installation notes where you will read this:

The Mailgun api client is not hard coupled to Guzzle or any other library that sends HTTP messages.

And this:

Mailgun has a dependency on the virtual package php-http/client-implementation which requires you to install an adapter

As pointed on installation steps:

If you just want to get started quickly you should run the following command:

php composer.phar require mailgun/mailgun-php php-http/curl-client guzzlehttp/psr7

The whole installation process has been tested and verified on this Issue https://github.com/mailgun/mailgun-php/issues/429#issuecomment-358316771

I must assume that the installation steps were not followed as described on the README file, so this is not an issue. My tests on the other issue are an example of this.

"Read the docs!"

Curl for PHP on Windows (which is used by the Guzzle library) needs a default root certificate

As @Nyholm pointed on his comment here https://github.com/mailgun/mailgun-php/issues/143#issuecomment-356865905

Windows, PHP and SSL is usually an issue...

If you have time to send a PR with a fix for this case, we're more than happy to review and merge.

TylerVigario commented 6 years ago

@DavidGarciaCat – Did I misunderstand something here or did you?

I have already acknowledged skipping over the installation of Guzzle and solved my issues as posted above on 1/16/2018.

Your comment above was of no help to me and a bit insulting given I had already told you beforehand that I had solved my issues. Maybe you didn't mean to actually reply to me but just state it to everyone who visits this thread?

The only reason I reiterated over the stumbling I encountered and how I was able to get past that was because you specifically asked me too.

"Get your shit together!"

DavidGarciaCat commented 6 years ago

Hey @MeekLogic - Sorry if that offended you, it was not my intention. I guess it was a misunderstanding, written answers are usually not helping to understand the tone or the intention.

TylerVigario commented 6 years ago

@DavidGarciaCat – I agree that written text can be troublesome (albeit having it's pluses as well) and very much appreciate your apology. There are no hard feelings. :thumbsup:

DavidGarciaCat commented 1 year ago

Hi, @joeleonjr

I cannot see any comment including an API key, after reviewing the comments twice (from my phone).

Please kindly point out the comment so the user who posted it can review it and rotate the keys of needed.

Thanks,

DavidGarciaCat commented 1 year ago

👍 @dani0332 you might want to check your key - revoke that one and get a new pair seems the best course of action, indeed.