cspoo / swiftmailer-mailgun-bundle
Swiftmailer Mailgun bundle
Installs: 1 052 728
Dependents: 0
Suggesters: 0
Security: 0
Stars: 104
Watchers: 11
Forks: 49
Open Issues: 16
Type:symfony-bundle
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
This bundle adds an extra transport to the swiftmailer service that uses the mailgun http interface for sending messages.
Installation
composer require cspoo/swiftmailer-mailgun-bundle php-http/guzzle5-adapter
Note: You can use any of these adapters
Configuration
Symfony 3.4
Also add to your AppKernel:
new cspoo\Swiftmailer\MailgunBundle\cspooSwiftmailerMailgunBundle(),
Configure your application with the credentials you find on the domain overview on the Mailgun.com dashboard.
// 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
Note that the swiftmailer configuration is the same as the standard one - you just change the mailer_transport parameter.
Symfony 4.1
Add your Mailgun credentials
# both .env and .env.dist files MAILGUN_DOMAIN=<your domain> MAILGUN_API_KEY=<your key> MAILGUN_SENDER=<your sender>
Adding to you bundle
// config/bundles.php return [ ... cspoo\Swiftmailer\MailgunBundle\cspooSwiftmailerMailgunBundle::class => ['all' => true], ];
Configure your Mailgun credentials:
// 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)%']
Finally, add the following line on swiftmailer config:
// config/packages/swiftmailer.yaml swiftmailer: # url: '%env(MAILER_URL)%' transport: 'mailgun' spool: { type: 'memory' }
Note: Not sure if url line should be commented.
Usage
First craft a message:
$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) ) ) ;
Then send it as you normally would with the mailer
service. Your configuration ensures that you will be using the Mailgun transport.
$this->container->get('mailer')->send($message);
You can also test through terminal using:
bin/console swiftmailer:email:send --from=<from email> --to=<to email> --subject="Foo" --body="Bar"
Choose HTTP client
Mailgun 2.0 is no longer coupled to Guzzle5. Thanks to Httplug you can now use any library to transport HTTP messages. You can rely on discovery to automatically find an installed client or you can use HttplugBundle and provide a client service name to the mailgun configuration.
// app/config/config.yml: cspoo_swiftmailer_mailgun: http_client: 'httplug.client'