alexbrons / swiftmailer-mailgun-bundle
Swiftmailer Mailgun 扩展包
v1.2.1
2019-10-31 15:56 UTC
Requires
- php: ^7.0
- mailgun/mailgun-php: ^3.0
- psr/log: ^1.1
- swiftmailer/swiftmailer: ^6.0
- symfony/config: ^3.3 || ^4.0
- symfony/dependency-injection: ^3.3 || ^4.0
- symfony/http-kernel: ^3.3 || ^4.0
Requires (Dev)
- doctrine/annotations: ^1.3
- matthiasnoback/symfony-dependency-injection-test: ^2.3.1 || ^3.0
- nyholm/symfony-bundle-test: ^1.4
- php-http/curl-client: ^1.7
- symfony/framework-bundle: ^3.3 || ^4.0
- symfony/phpunit-bridge: ^4.0
- symfony/swiftmailer-bundle: ^2.5.1 || ^3.2
- zendframework/zend-diactoros: ^1.7
Suggests
- azine/mailgunwebhooks-bundle: Allows to handle Mailgun event webhooks
- php-http/httplug-bundle: To manage your http clients
README
此扩展包为 Swiftmailer 服务添加了一个额外的传输方式,该方式使用 Mailgun HTTP 接口发送消息。
安装
composer require cspoo/swiftmailer-mailgun-bundle php-http/guzzle5-adapter
注意:您可以使用以下适配器中的任何一个。
配置
Symfony 3.4
同时,将以下内容添加到您的 AppKernel 中:
new cspoo\Swiftmailer\MailgunBundle\cspooSwiftmailerMailgunBundle(),
使用您在 Mailgun.com 控制面板的域名概览中找到的凭证配置您的应用程序。
// app/config/config.yml: cspoo_swiftmailer_mailgun: key: "key-xxxxxxxxxx" domain: "mydomain.com" endpoint: "https://api.eu.mailgun.net" # Optional. Use this config for EU region. Defaults to "https://api.mailgun.net" http_client: "httplug.client" # Optional. Defaults to null and uses discovery to find client. # Swiftmailer Configuration swiftmailer: transport: "mailgun" spool: { type: memory } # This will start sending emails on kernel.terminate event
注意:Swiftmailer 的配置与标准配置相同 - 您只需更改 mailer_transport 参数。
Symfony 4.1
添加您的 Mailgun 凭证
# both .env and .env.dist files MAILGUN_DOMAIN=<your domain> MAILGUN_API_KEY=<your key> MAILGUN_SENDER=<your sender>
添加到您的扩展包
// config/bundles.php return [ ... cspoo\Swiftmailer\MailgunBundle\cspooSwiftmailerMailgunBundle::class => ['all' => true], ];
配置您的 Mailgun 凭证
// config/packages/mailgun.yaml cspoo_swiftmailer_mailgun: key: '%env(MAILGUN_API_KEY)%' domain: "%env(MAILGUN_DOMAIN)%" services: Mailgun\Mailgun: class: Mailgun\Mailgun factory: ['Mailgun\Mailgun', create] arguments: ['%env(MAILGUN_API_KEY)%']
最后,在 Swiftmailer 配置中添加以下行
// config/packages/swiftmailer.yaml swiftmailer: # url: '%env(MAILER_URL)%' transport: 'mailgun' spool: { type: 'memory' }
注意:不确定 url 行是否应该被注释。
使用方法
首先制作一条消息
$message = \Swift_Message::newInstance() ->setSubject('Hello Email') ->setFrom('send@example.com') ->setTo('recipient@example.com') ->setBody( $this->renderView( 'HelloBundle:Hello:email.txt.twig', array('name' => $name) ) ) ;
然后像往常一样使用 mailer
服务发送。您的配置确保您将使用 Mailgun 传输。
$this->container->get('mailer')->send($message);
您也可以通过终端进行测试
bin/console swiftmailer:email:send --from=<from email> --to=<to email> --subject="Foo" --body="Bar"
选择 HTTP 客户端
Mailgun 2.0 已不再与 Guzzle5 相关联。感谢 Httplug,您现在可以使用任何库来传输 HTTP 消息。您可以通过 发现 自动找到已安装的客户端,或者您可以使用 HttplugBundle 并向 Mailgun 配置提供客户端服务名称。
// app/config/config.yml: cspoo_swiftmailer_mailgun: http_client: 'httplug.client'