SoFunction
Updated on 2025-04-04

PHP calls Mailgun to send mail

Summary of the method of sending emails by PHP by calling Mailgun for your reference. The specific content is as follows

This blog refers to the official Mailgun API github link:/mailgun/mailgun-php

It depends on the composer tool, so you need to confirm that composer has been installed before using it. How to install composer is very simple. The following method shows how to install composer:

curl -sS /installer | php

The Api's client is not hard-connected to Guzzle or any other library that sends HTTP messages. It uses an abstraction called HTTPlug that allows you to flexibly choose PSR-7 or HTTP clients. If you just want to start quickly, you should run the following command:

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

, Only after completing the above work, you can use Mailgun to send email~, please refer to the usage method/The official tutorial, here is an example:

require 'vendor/';
use Mailgun\Mailgun;
# First, instantiate the SDK with your API credentials and define your domain. 
$mg = new Mailgun("key-example");
$domain = "";

# Now, compose and send your message.
$mg->sendMessage($domain, array('from' => 'bob@', 
        'to'  => 'sally@', 
        'subject' => 'The PHP SDK is awesome!', 
        'text' => 'It is so simple to send a message.'));


4. Notes:

Of course, you can also send html emails, just need to use the above example'text'=>$text Rewrite it into'html'=>$htmlThat's it. If you want CC or BCC functions, the method is the same as php. You just need to add 'cc'=>'jack@','bcc'=>'jenny@' to the above array.

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.