PHP 调用Mailgun发送邮件方法总结

PHP 调用Mailgun发送邮件方法总结

本篇博客参考Mailgun 官方API github链接: https://github.com/mailgun/mailgun-php

1.Mailgun是依赖composer工具,因此在使用之前需要先确认已经安装了composer.如何安装composer,非常简单,下面方法展示如何安装composer工具:
curl -sS https://getcomposer.org/installer | php

2.Mailgun Api的客户端没有硬连接到Guzzle或任何其他发送HTTP消息的库,它使用一个称为HTTPlug的抽象,可以灵活的选择PSR-7或者HTTP客户端.如果你只是想快速开始,你应该运行以下命令:
php composer.phar require mailgun/mailgun-php php-http/curl-client guzzlehttp/psr7

3.ok,以上工作完成只有,你就可以使用Mailgun进行email的发送啦~,使用方法参考http://www.mailgun.com/官方教程,下面是一个例子:

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

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

4.备注:
当然也可以发送html形式的邮件,只需要将上面例子中的 'text'=>$text 改写成 'html'=>$html即可,同样如果想要CC或者BCC等功能,方法于php相同,只需要在上面的array里增加'cc'=>'[email protected]','bcc'=>'[email protected]',即可.

猜你喜欢

转载自blog.csdn.net/houzhiwen_yy/article/details/53462341