cspoo / swiftmailer-mailgun-bundle
Swiftmailer Mailgun 扩展包
2.0.0
2021-09-13 11:40 UTC
Requires
- php: ^7.3 || ^8.0
- mailgun/mailgun-php: ^3.0
- psr/log: ^1.1
- swiftmailer/swiftmailer: ^6.0
- symfony/config: ^3.3 || ^4.0 || ^5.0
- symfony/dependency-injection: ^3.3 || ^4.0 || ^5.0
- symfony/http-kernel: ^3.3 || ^4.0 || ^5.0
Requires (Dev)
- doctrine/annotations: ^1.3
- laminas/laminas-diactoros: ^2.0
- matthiasnoback/symfony-dependency-injection-test: ^2.3.1 || ^4.0
- nyholm/symfony-bundle-test: ^1.4
- php-http/curl-client: ^2.0
- symfony/framework-bundle: ^3.3 || ^4.0 ||^5.0
- symfony/phpunit-bridge: ^4.0 || ^5.0
- symfony/swiftmailer-bundle: ^2.5.1 || ^3.2
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'