<?php
/*
twzFilewatchMGA.class.php
----------------------------------------------------------------------------
This file contains two PHP classes:
twzFilewatchMGA ... extends twzFilewatch to send mail using the Mailgun API
instead of the native PHP mail() function.
MailgunAPI ........ a simple class to access the Mailgun API
----------------------------------------------------------------------------
This demonstrates one way to extend twzFilewatch with a custom _emailSend()
method. We have chosen to use the Mailgun API, but the same idea can be used
for any custom or 3rd party method for sending email.
To use this as is, you will need to register with mailgun.com to obtain your
unique base URL and API key. You'll also need to add some DNS records to your
domain; mailgun.com will walk you through this.
Once this is done, you should change these lines in your calling script:
require 'twzFileWatch.class.php';
$fw = new twzFilewatch($SiteName, $CheckFolder, $RecurseLevel, $EmailTo);
to this:
require 'twzFileWatch.class.php';
require 'twzFilewatchMGA.class.php';
$fw = new twzFilewatchMGA($SiteName, $CheckFolder, $RecurseLevel, $EmailTo);
$fw->mailgunSetup('YOUR_UNIQUE_BASE_URL', 'YOUR_UNIQUE_API_KEY');
*/
class twzFilewatchMGA extends twzFilewatch {
private $MailgunBaseUrl='';
private $MailgunApiKey='';
private $SendResult=null;
public function mailgunSetup($BaseURL, $ApiKey)
{
$this->MailgunBaseUrl=$BaseURL;
$this->MailgunApiKey=$ApiKey;
}
public function getSendResult()
{ return $this->SendResult; }
protected function _emailSend($EmailTo, $EmailSubject, $EmailBody, $EmailHeaders)
{
// Sends an email; returns true on success or false on error.
// $EmailHeaders will be an array with indexes 'From', 'Cc', 'Bcc' and 'Reply-To'
if(!$this->MailgunBaseUrl or !$this->MailgunApiKey) // FATAL
{ die('twzFW: mailgunSetup() has not been called'); }
$mg=new MailgunAPI($this->MailgunBaseUrl, $this->MailgunApiKey, $EmailHeaders['From']);
$mg->setCc($EmailHeaders['Cc']);
$mg->setBcc($EmailHeaders['Bcc']);
$mg->setReplyTo($EmailHeaders['Reply-To']);
$mg->setEmailFormat('plain');
$sentOK=$mg->send($EmailTo, $EmailSubject, $EmailBody);
$this->SendResult=$mg->getSendResult();
return $sentOK;
}
} // end class twzFilewatchMGA
###################################################################################################
/*
MailgunAPI.class.php v0.3L 2016-08-21
(c) 2015-6 dev@tweezy.net.au
EXAMPLE USAGE:
require 'MailgunAPI.class.php';
$MailgunBaseUrl = 'https://api.mailgun.net/v3/mydomain.com';
$MailgunApiKey = 'key-XXXXXXXXXXXXXXXXX';
$EmailFrom = 'test@mydomain.com';
$EmailTo = 'you@yourdomain.com';
$EmailSubject = 'Test email';
$EmailBody = "<p>This is a test</p>\r\n<p>This email is for <b>testing</b> - let's hope it works!</p>";
$mg=new MailgunAPI($MailgunBaseUrl, $MailgunApiKey, $EmailFrom);
$mg->send($EmailTo, $EmailSubject, $EmailBody);
*/
class MailgunAPI {
private $apiKey;
private $apiBaseUrl;
private $emailFrom;
private $replyTo='';
private $emailCc='';
private $emailBcc='';
private $customHeaders=false;
private $emailFormat='auto';
private $sendResult=false;
public function __construct($apiBaseUrl, $apiKey, $emailFrom, $replyTo='')
{
$this->apiBaseUrl=$apiBaseUrl;
$this->apiKey=$apiKey;
$this->setFrom($emailFrom);
if($replyTo)
{ $this->setReplyTo($replyTo); }
}
public function send($EmailTo, $EmailSubject, $EmailBody)
{
$this->sendResult=false;
$Field = array();
$Field['from'] = $this->emailFrom;
$Field['to'] = $EmailTo;
$Field['subject'] = $EmailSubject;
if($this->replyTo) { $Field['h:Reply-To'] = $this->replyTo; }
if($this->emailCc) { $Field['cc'] = $this->emailCc; }
if($this->emailBcc) { $Field['bcc'] = $this->emailBcc; }
if('auto'==$this->emailFormat)
{
if(substr_count($EmailBody, '<') > 3) // if more than 3 '<' chars, assume it's html
{ $this->emailFormat='html'; }
else
{ $this->emailFormat='plain'; }
}
switch($this->emailFormat)
{
case 'html':
$Field['html']=$EmailBody;
break;
case 'plain':
$Field['text']=$EmailBody;
break;
case 'both':
$Field['html']=$EmailBody;
$Field['text']=strip_tags(str_replace(array('<br />', '<br/>', '<br>'), "\r\n", $EmailBody));
break;
}
if(is_array($this->customHeaders))
{
foreach($this->customHeaders as $Name=>$Val)
{ if(is_string($Val)) { $Field['h:'.$Name] = $Val; } }
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->apiBaseUrl.'/messages');
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, 'api:'.$this->apiKey);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS,$Field);
$result = curl_exec($ch); // json
$curlInfo = curl_getinfo($ch);
curl_close($ch);
$this->sendResult=$result;
return ($curlInfo['http_code'] == 200);
}
public function getSendResult($format='array') // format='array|object|json'
{
if(is_string($this->sendResult)) // json or custom error
{ return $this->sendResult; }
elseif('object'==$format)
{ return json_decode($this->sendResult); }
else // array
{ return json_decode($this->sendResult, true); }
}
public function setCc($emailCc)
{ $this->emailCc=$emailCc; }
public function setBcc($emailBcc)
{ $this->emailBcc=$emailBcc; }
public function setFrom($emailFrom)
{ $this->emailFrom=$emailFrom; }
public function setReplyTo($replyTo)
{ $this->replyTo=$replyTo; }
public function setCustomHeaders($NewHeaders)
{
// Adds one or more custom headers
// eg param: array('Content-Transfer-Encoding'=>'quoted-printable')
// NOTE: multiple calls will not append new headers - all custom headers should be set once!!
// Call with $NewHeaders==false to clear custom headers.
$this->customHeaders=$NewHeaders;
}
public function setEmailFormat($format)
{
if(in_array($format, array('html','plain','text','both','auto')))
{
if('text'==$format) { $format='plain'; } // text is an alias for plain
$this->emailFormat=$format;
}
}
} // end class MailgunAPI
?>
|