mailgun-php icon indicating copy to clipboard operation
mailgun-php copied to clipboard

Call to undefined method Mailgun\Mailgun::sendMessage()

Open octoxan opened this issue 7 years ago • 12 comments

If you follow along with the latest version of the docs, you can't send messages with attachments.

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

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

# Make the call to the client.
$result = $mgClient->sendMessage($domain, array(
    'from'    => 'Excited User <YOU@YOUR_DOMAIN_NAME>',
    'to'      => '[email protected]',
    'cc'      => '[email protected]',
    'bcc'     => '[email protected]',
    'subject' => 'Hello',
    'text'    => 'Testing some Mailgun awesomness!',
    'html'    => '<html>HTML version of the body</html>'
), array(
    'attachment' => array('/path/to/file.txt', '/path/to/file.txt')
));

This errors out when calling new Mailgun since the first parameter must be a configurator (not mentioned anywhere in the docs) and not a string.

Then when you go to call sendMessage it says that is an undefined method.

octoxan avatar Nov 05 '19 15:11 octoxan

Same issue here

SonamJagetia avatar Nov 22 '19 10:11 SonamJagetia

try with $mgClient->messages()->send($domain, array(your definitions));

trondhuso avatar Nov 22 '19 11:11 trondhuso

I have the same issue and I can't even send regular messages without attachments. I get a 500 response

raneeshgomez avatar Nov 26 '19 13:11 raneeshgomez

@codeSmart2307 - most likely it is because you are using the new api/version and the old syntax.

This is the new syntax:

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

// First, instantiate the SDK with your API credentials $mg = Mailgun::create('YOUR_API_KEY');

// Now, compose and send your message.

$mg->messages()->send('YOUR_DOMAIN_NAME', [ 'from' => 'Excited User <mailgun@YOUR_DOMAIN_NAME>', 'to' => 'Baz <YOU@YOUR_DOMAIN_NAME>', 'subject' => 'Hello', 'text' => 'Testing some Mailgun awesomness!' ]);

// For attachements (untested, but I believe this is how it is done):

$mg->messages()->send('YOUR_DOMAIN_NAME', [ 'from' => 'Excited User <mailgun@YOUR_DOMAIN_NAME>', 'to' => 'Baz <YOU@YOUR_DOMAIN_NAME>', 'subject' => 'Hello', 'text' => 'Testing some Mailgun awesomness!' ], ['attachments' => array('/path/to/file.txt', '/path/to/file.txt')] );

trondhuso avatar Nov 26 '19 13:11 trondhuso

@trondhuso I actually do use this syntax. It still does not work for some reason.

raneeshgomez avatar Nov 26 '19 13:11 raneeshgomez

@codeSmart2307 and @octoxan

This is how you send a message with attachment:

$mg->messages()->send('YOUR_DOMAIN_NAME', [
'from' => 'Excited User <mailgun@YOUR_DOMAIN_NAME>',
'to' => 'Baz <YOU@YOUR_DOMAIN_NAME>',
'subject' => 'Sending an e-mail with an attachment',
'text' => 'Sending an e-mail with an attachment',
'attachment' => array(array('filename' => 'nameoffile.txt', 'filePath' => '/path/to/file/text.txt'))]);


The most important part here is the double array where you define the filename and file path.

Hope it helps someone. And I also hope someone updates the documentation.

trondhuso avatar Nov 26 '19 15:11 trondhuso

@codeSmart2307 - most likely it is because you are using the new api/version and the old syntax.

This is the new syntax:

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

// First, instantiate the SDK with your API credentials $mg = Mailgun::create('YOUR_API_KEY');

// Now, compose and send your message.

$mg->messages()->send('YOUR_DOMAIN_NAME', [ 'from' => 'Excited User <mailgun@YOUR_DOMAIN_NAME>', 'to' => 'Baz <YOU@YOUR_DOMAIN_NAME>', 'subject' => 'Hello', 'text' => 'Testing some Mailgun awesomness!' ]);

// For attachements (untested, but I believe this is how it is done):

$mg->messages()->send('YOUR_DOMAIN_NAME', [ 'from' => 'Excited User <mailgun@YOUR_DOMAIN_NAME>', 'to' => 'Baz <YOU@YOUR_DOMAIN_NAME>', 'subject' => 'Hello', 'text' => 'Testing some Mailgun awesomness!' ], ['attachments' => array('/path/to/file.txt', '/path/to/file.txt')] );

It Works fine for me without attachments

Gautham-GM avatar Jan 27 '20 11:01 Gautham-GM

Above syntax doesnt work for me.

progfrosty avatar Apr 13 '20 10:04 progfrosty

Composer v 1.10.1, Mac OS X, php 7.3.9

progfrosty avatar Apr 13 '20 10:04 progfrosty

also having this issue. i was stuck using an older version due to php being 5.6 after sorting that out and going to 7.3 and updating to latest mailgun sdk nothing works anymore. getting Fatal error: Uncaught TypeError: Argument 1 passed to Mailgun\Api\HttpApi::__construct() must implement interface. documents contradict themselves.... what a mess! any help?

SeanYeaney avatar Apr 16 '20 01:04 SeanYeaney

Is this still a problem? Does anyone have problems sending an email with an attachment?

DavidGarciaCat avatar Sep 25 '20 18:09 DavidGarciaCat

I think you need to be aware of what versions you have installed. You should also be able to read the code or the docs: https://github.com/mailgun/mailgun-php/tree/master/doc

Nyholm avatar Jan 30 '21 06:01 Nyholm