Email

How to quickly bake Mailgun with PHP

When I started coding, I remember debating whether I would want to learn ASP.NET vs PHP. I read thousands of bulletin boards on threads about each language to help me make my decision.

PUBLISHED ON

PUBLISHED ON

When I started coding, I remember debating whether I would want to learn ASP.NET vs PHP. I read thousands of bulletin boards on threads about each language to help me make my decision.

I chose PHP mainly for the community, the availability of examples and for the fact that it was open source. I’m sure these are similar reasons why you’ve chosen to adventure yourself in the world of PHP.

PHP is one of the most widely used languages, yet there is a lot of confusing or outdated material on the web.

The aim of this particular post is to provide you with a detailed overview on how to use Mailgun in your PHP environment with simple copy paste examples

Using the official Mailgun-PHP library

A very straightforward way to use Mailgun and leverage its outstanding API to deliver emails is to install the official PHP library in your development environment, written and maintained as an open source project by our Mailgunner Travis.

Steps

  • Open your terminal and navigate to your server’s public folder. This is where you store your website and varies depending on installation and web-server used - cd /var/www/html

  • Create an empty directory for this project - mkdir mailgun-php

  • Enter the directory and execute the following commands - cd mailgun-php (changes directory)

  • Now execute the following command to download and install Composer. - curl -sS https://getcomposer.org/installer | php

Composer, when used this way, allows us to download PHP libraries with ease locally within the folder. Now let’s use composer to install the Mailgun library itself.

  • Let’s run this to tell composer we’re going to use (and therefore we need it downloaded) the mailgun library (here we use 1.7.1) and its dependencies. - php composer.phar require mailgun/mailgun-php:~1.7.1

Congratulations, you’ve installed the Mailgun official library and you are now ready to write some code!

Open a file and include the vendor/autoload.php file to get started. All the examples below assume you've already included this in your file:

Then, follow these instructions from the official documentation to send a message using the SDK:

Remember: $domain must match to the domain you have configured on app.mailgun.com.

Using Swift Mailer

Using Mailgun with Swift Mailer is a breeze.

Make sure you have your domain’s SMTP credentials to hand! You can find these here they’re named:

  • Default SMTP Login

  • Default Password

Steps

  • Create a folder called Swiftmailer - mkdir swiftmailer, cd swiftmailer

  • Download Composer to install swiftmailer and its dependencies

    • curl -sS https://getcomposer.org/installer | php

  • Install Swiftmailer

    • php composer.phar require swiftmailer/swiftmailer @stable

  • Files will be downloaded, once finished simply create your own empty file to test that phpmailer works. Within your terminal you can achieve that by doing:

    • touch mailgun_with_swiftmailer.php

    • vim mailgun_with_swiftmailer.php

Here we’re using vim, but you can edit the file mailgun_with_swiftmailer.php in whichever editor you feel most comfortable with.

Further information on sending much more complicated emails can be found in the official Swiftmailer documentation available online over here.

Using PHP Mailer

Mailgun can also be used in conjunction with one of the most famous PHP emailing libraries: PHP Mailer.

PHP Mailer is a very valid alternative to the in-built php mail() function, as it speeds up and allows you to heavily customise the way you send emails with PHP. Linux distributions and Mac OS X have an in-built function to send emails although unreliable. By the way, it is worth mentioning that Windows hasn’t got anything at all to send emails, thus I suggest you keep reading.

Steps

  • Create a folder called phpmailer

    • mkdir phpmailer

    • cd phpmailer

  • Download composer to install phpmailer and its dependencies

    • curl -sS https://getcomposer.org/installer | php

  • Composer is ready, let’s install Phpmailer(5.2.8) now:

    • php composer.phar require phpmailer/phpmailer:5.2.8

  • Files will be downloaded, once finished simply create a your own empty file to test that phpmailer works

    • touch mailgun_with_phpmailer.php

    • vim mailgun_with_phpmailer.php

Here we’re using vim, but you can edit the file mailgun_with_phpmailer.php in whichever editor you feel most comfortable with.

The settings for authenticating with Mailgun’s SMTP server can be found over here, just like with Swiftmailer.

Log into your Mailgun control panel, pick a domain (or the sandbox domain that comes with every fresh account if you have not configured a custom domain), and copy paste these details (Default SMTP Login, Default Password) in the code snippet above in the smtp username and password fields.

Baking is awesome

Do you use other libraries in conjunction with Mailgun? If so we’d be happy to feature it in our next PHP blog post with full coverage and examples!

Happy Sending!

Sign Up

It's easy to get started. And it's free.

See what you can accomplish with the world’s best email delivery platform.

Related readings

What is SMTP relay, how does it work, and why use it?

Remember relay races in school? Just as runners pass along a baton, an SMTP relay passes along an email from one server to another. Sinch Mailgun’s free SMTP relay...

Read more

What are SMTP commands and what do you need to know about them?

Why do we need SMTP commands and what do they do? Well, we use SMTP (Simple Mail Transfer Protocol) commands to communicate with email servers. These commands are...

Read more

What is SMTP and how does it work?

SMTP, though a pillar of email delivery, often gets lost in the jumble of tech terms and acronyms. But if you're ready to send impactful emails, this is one of those acronyms that...

Read more

Popular posts

Email inbox.

Build Laravel 10 email authentication with Mailgun and Digital Ocean

When it was first released, Laravel version 5.7 added a new capability to verify user’s emails. If you’ve ever run php artisan make:auth within a Laravel app you’ll know the...

Read more

Mailgun statistics.

Sending email using the Mailgun PHP API

It’s been a while since the Mailgun PHP SDK came around, and we’ve seen lots of changes: new functionalities, new integrations built on top, new API endpoints…yet the core of PHP...

Read more

Statistics on deliverability.

Here’s everything you need to know about DNS blocklists

The word “blocklist” can almost seem like something out of a movie – a little dramatic, silly, and a little unreal. Unfortunately, in the real world, blocklists are definitely something you...

Read more

See what you can accomplish with the world's best email delivery platform. It's easy to get started.Let's get sending
CTA icon