ElasticEmailTransport.php
2.7 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?php
namespace FootyRoom\Services\Mail;
use FootyRoom\Config;
use Swift_Transport;
use Swift_Mime_SimpleMessage;
use Illuminate\Mail\Transport\Transport;
class ElasticEmailTransport extends Transport implements Swift_Transport
{
public function __construct(Config $config)
{
$this->apiUser = $config->get('elasticEmail.user');
$this->apiKey = $config->get('elasticEmail.apiKey');
}
/**
* {@inheritdoc}
*/
public function isStarted()
{
return true;
}
/**
* {@inheritdoc}
*/
public function start()
{
return true;
}
/**
* {@inheritdoc}
*/
public function stop()
{
return true;
}
public function send(Swift_Mime_SimpleMessage $message, &$failedRecipients = null)
{
$bodyHtml = '';
$bodyText = '';
$from = key($message->getFrom());
$fromName = $message->getFrom()[$from];
if ($message->getContentType() === 'text/html') {
$bodyHtml = $message->getBody();
} else {
$bodyText = $message->getBody();
}
if (count($message->getChildren())) {
$child = $message->getChildren()[0];
if ($child->getContentType() === 'text/plain') {
$bodyText = $child->getBody();
}
}
$data = 'username='.urlencode($this->apiUser);
$data .= '&api_key='.urlencode($this->apiKey);
$data .= '&from='.urlencode($from);
$data .= '&from_name='.urlencode($fromName);
$data .= '&to='.urlencode(key($message->getTo()));
$data .= '&subject='.urlencode($message->getSubject());
if ($message->getReplyTo()) {
$replyTo = key($message->getReplyTo());
$replyToName = $message->getReplyTo()[$replyTo];
$data .= '&reply_to='.urlencode($replyTo);
$data .= '&reply_to_name='.urlencode($replyToName);
}
if ($bodyHtml) {
$data .= '&body_html='.urlencode($bodyHtml);
}
if ($bodyText) {
$data .= '&body_text='.urlencode($bodyText);
}
$header = "POST /mailer/send HTTP/1.0\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= 'Content-Length: '.strlen($data)."\r\n\r\n";
$fp = fsockopen('ssl://api.elasticemail.com', 443, $errno, $errstr, 30);
if (!$fp) {
return false;
} else {
fputs($fp, $header.$data);
$res = '';
while (!feof($fp)) {
$res .= fread($fp, 1024);
}
fclose($fp);
}
if (substr($res, 9, 3) === '200') {
return true;
}
}
}