Drupal-7: Mailing with Mailgun using PHP

Mailgun provides email sending and marketing tools including SMTP sending services. Drupal defaults to using its server system to send email. However, because servers can be configured in so many different ways, and because mail servers are notoriously difficult to secure, sending email securely using modern mail transport protocols can be quite problematic. Mailgun's RESTful API services let you efficiently and securely send email through Mailgun SMTP servers using HTTP / HTTPS transport protocols. If you are dubious of the need to download Composer packages and all of their attendant dependencies, or you are dubious of the need to tie into the Drupal mail service interface just to send an email from a webform, your doubts are well-founded. It does not have to be such a convoluted process to send an email! Here are two different methods to send email from any PHP CMS platform (such as Drupal) through the Mailgun mailing services. All that is required is that you have an active Mailgun account with an API key. You can find your API key under Settings | API security | API keys | Private API key. It will look something like: key-examplehexnumber (eg: key-0123456789abcdef0123456789abcdef)

Using the Mailgun API through shell exec

If your PHP installation supports executing arbitrary shell commands and you have curl installed, this function will send the desired email through your Mailgun SMTP service:

function dequote( $str ) {
  return strtr($str, "'", "`");
}

function mailgun_shell($from, $to, $subject, $body, $pwd='key-apiexamplehexkey', $user='api') {
  $output = [];
  $return = 0;
  $from = dequote($from);
  $to = dequote($to);
  $subject = dequote($subject);
  $body = dequote($body);

  $exec = "curl -s --user '$user:$pwd' " .
    "https://api.mailgun.net/exampledomain.com/messages " .
    "-F from='$from' " .
    "-F to='$to' " .
    "-F subject='$subject' " .
    "-F text='$body'";
  exec( $exec, $output, $return );
}

The main problems with this approach are:
  1. Shell execution is slow and fraught with hacking danger.
  2. You have to use the dequote function to get rid of nasty little apostrophes that might be in user-generated data, and then you can't easily get them back in again
On the bright side, this method is so general-purpose, you could implement it easily from within almost any back-end platform.

Using the Mailgun API through PHP's curl library

If you have installed the PHP curl library, then you can send directly from PHP. There are almost no downsides to this approach and no need to install Composer, the Drupal Mailgun module, nor any of their dependencies:
function mailgun_phpcurl($from, $to, $subject, $body, $pwd='key-apiexamplehexkey', $user='api'){
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, "https://api.mailgun.net/exampledomain.com/messages");
  curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
  curl_setopt($ch, CURLOPT_USERPWD, "$user:$pwd");
  curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  curl_setopt($ch, CURLOPT_POSTFIELDS, [
    'from' => $from,
    'to' => $to,
    'subject' => $subject,
    'text' => $body ] );
  $ret = curl_exec($ch);
  curl_close( $ch );
}
References:
2 / 2020