步骤如下:

  1. 通过 composer 获取代码库
  2. 获取 API Key 以及 domain
  3. 编写代码发送邮件

1. 首先通过 composer 获取依赖代码库,参考官网给的命令
https://documentation.mailgun.com/en/latest/libraries.html#php

composer require mailgun/mailgun-php php-http/guzzle6-adapter php-http/message

运行完成后在当前目录下生成 composer.json、composer.lock、vendor文件夹


2. 进入 mailgun 管理后台,获取 API Key 以及 domain

获取 domain
获取 API Key

3. 使用HTML和文本部分发送消息。此示例还将两个文件附加到邮件中:

# 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'      => 'foo@example.com',
    'cc'      => 'baz@example.com',
    'bcc'     => 'bar@example.com',
    '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')
));

发送成功后如下: