Call to undefined method Mailgun\Mailgun::sendMessage()
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.
Same issue here
try with $mgClient->messages()->send($domain, array(your definitions));
I have the same issue and I can't even send regular messages without attachments. I get a 500 response
@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 I actually do use this syntax. It still does not work for some reason.
@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.
@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
Above syntax doesnt work for me.
Composer v 1.10.1, Mac OS X, php 7.3.9
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?
Is this still a problem? Does anyone have problems sending an email with an attachment?
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