Saqib Razzaq (SakyTalks)

How to setup and use Mailgun with PHP (complete guide)

Mailgun is an email service and has a higher mail size limit (25MB) compared to Amazon SES (10MB) and some other similar services. The below guide will show you how to set up and start sending emails using Mailgun and their official PHP SDK. 

  1. Create an account at https://www.mailgun.com/

  2. Click "Add a domain" and enter the domain you intend to use. It is highly recommended to use a separate subdomain for the purpose of sending emails. 

  3. Now open the domain management area and you'll be presented with some DNS records that need to be added.

  4. Head over to where your domain is originally hosted and add TXT, MX, and CNAME records that were given to you by Mailgun

  5. It can take a little time for DNS changes to take effect so either check back a few hours or wait for their confirmation email.

  6. Once the domain has been verified you can proceed to the next step and start sending emails.

  7. Next, we are going to install Mailgun's official PHP SDK which is open source and available on GitHub https://github.com/mailgun/mailgun-php

  8. Run the following if you don't have composer or skip to the next step curl -sS https://getcomposer.org/installer | php

  9. Now run below command to install SDK composer require mailgun/mailgun-php kriswallsmith/buzz nyholm/psr7

  10. Below is a sample PHP function for sending emails 

require 'vendor/autoload.php';
use Mailgun\Mailgun;

function sendMail($params) {
	$from = isset($params['from']) ? $params['from'] : 'default@from.com';
	$mailParams = array();
	$attachments = array();
	$mailParams['from'] = $params['sender_name'] . " <$from>";
	$mailParams['to'] = $params['to'];

	#$m->setFrom($params['sender_name' ] . " <$from>");
	if (isset($params['attachment'])) {
		$attachments[] = array('filePath' => $params['attachment']['path'], 'filename' => $params['attachment']['name']);
	}

	if (isset($params['custom_attachment'])) {
		foreach ($params['custom_attachment'] as $key => $file) {
			$attachments[] = array('filePath' => $file['path'], 'filename' => $file['name']);
		}
	}

	if (isset($params['replyTo'])) {
		if (is_array($params['replyTo'])) {
			foreach ($params['replyTo'] as $key => $email) {
				$mailParams['h:Reply-To'][] = $email;
			}
		} else {
			$mailParams['h:Reply-To'] = $params['replyTo'];
		}
	}

	$mailParams['subject'] = $params['subject'];
	$isHtml = $params['message'] != strip_tags($params['message']);
	if ($isHtml) {
		$mailParams['html'] = $params['message'];
	} else {
		$mailParams['text'] = $params['message'];
	}

	if (!empty($attachments)) {
		$mailParams['attachment'] = $attachments;
	}

	$mg = Mailgun::create('api_key_here');
	$status = $mg->messages()->send('mail.collectiveproperties.net', $mailParams);
	return $status;
}// sending email

$params = array();
$params['to'] = 'frank@court.com';
$params['from'] = 'wilson@prison.com';
$params['sender_name'] = 'Frank Castle';
$params['subject'] = 'IM COMING FOR YA';
$params['message'] = 'One batch! Two batch! Penny & dime!';
$params['replyTo'] = 'matt@nelsonandmurdock.com';
$params['attachment'] = array('path' => 'test.jpg', 'name' => 'test.jpg');
sendMail($params);

#how to #mailgun #php