mapkyca/swiftmailer-mailgun-transport

这是Swiftmailer Mailgun Bundle的一个分支,仅暴露传输层

1.2.0 2018-12-17 15:48 UTC

This package is auto-updated.

Last update: 2024-08-28 01:33:08 UTC


README

这是一个对Mailgun Bundle的分支,由于工作压力,需要让Mailgun传输层与现代symfony构建工作。你可能会选择使用原始项目

Swiftmailer Mailgun Bundle

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

此bundle向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>

添加到你的bundle

// 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'