cspoo/swiftmailer-mailgun-bundle

Swiftmailer Mailgun 扩展包

安装次数: 1,044,117

依赖项: 0

建议者: 0

安全性: 0

星级: 104

关注者: 11

分支: 49

开放问题: 16

类型:symfony-bundle

2.0.0 2021-09-13 11:40 UTC

This package is auto-updated.

Last update: 2024-09-13 17:55:51 UTC


README

Latest Stable Version codecov.io Total Downloads Monthly Downloads Software License

此扩展包为 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'