-
Notifications
You must be signed in to change notification settings - Fork 0
/
NerdsvilleMailGun.php
74 lines (66 loc) · 2.65 KB
/
NerdsvilleMailGun.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<?php
class NerdsvilleMailGun{
function __construct($from, $apiVersion, $domain, $secret, $trackClicks, $trackOpens, $fileNames=null, $filetypes=null, $attachments=null, $isImage=false){
$this->from = $from;
$this->apiVersion = $apiVersion;
$this->domain = $domain;
$this->secret = $secret;
$this->trackClicks = $trackClicks;
$this->trackOpens = $trackOpens;
$this->attachments = $attachments;
$this->fileNames = $fileNames;
$this->filetypes = $filetypes;
$this->isImage = $isImage;
}
function sendPlainText($to, $subject, $message){
$postfields = $this->setPostFields($to, $subject, $message);
$this->initializeCurl($postfields);
}
function sendHTMLMessage($to, $subject, $htmlMessage){
$postfields = $this->setPostFields($to, $subject, "", $htmlMessage);
$this->initializeCurl($postfields);
}
function sendPlainTextOrHTML($to, $subject, $message, $htmlMessage){
$postfields = $this->setPostFields($to, $subject, $message, $htmlMessage);
$this->initializeCurl($postfields);
}
function setPostFields($to, $subject, $message="", $htmlMessage=""){
$postfields = array(
"to"=>$to,
"from"=>$from,
"subject"=>$subject,
"text"=>$message,
"html"=>$htmlMessage=="" ? $message : $htmlMessage,
"o:tracking-clicks"=>$this->trackClicks ? "yes" : "no",
"o:tracking-opens"=>$this->trackOpens ? "yes" : "no");
if($this->isImage){
foreach($this->attachments as $key=>$attachment){
$postfields["attachment[".$key."]"] = "@".$attachment.
';filename=' . $this->fileNames[$key].
';type='. $this->filetypes[$key];
}
}
return $postfields;
}
function initializeCurl($postfields){
$curlURL = "https://api.mailgun.net/v" + $this->apiVersion+"/" + $this->domain;
$curlURL .= "/messages"; //Using messages API endpoint for POST
$headers = $isImage ? array("Content-Type:multipart/form-data") : array();
$ch = curl_init();
/*CURL OPTIONS*/
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, 'api:' + $this->secret);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_URL, $curlURL);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
curl_exec($ch);
$info = curl_getinfo($ch);
if(!curl_errno($ch) || $info['http_code'] != 200) {
print 'SUCCESS!';
} else {
print 'CAN HAZ ERROR?!?!?' . curl_error($ch);
}
curl_close($ch);
}
}