Skip to content

Commit

Permalink
Email::SES fixed up
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefano Azzolini committed Mar 15, 2016
1 parent fcc3a04 commit a5af0d4
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 184 deletions.
6 changes: 3 additions & 3 deletions classes/Email/Native.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function send(){
$head[] = "Content-Type: multipart/mixed; boundary=\"".$uid."\"";

$body[] = "--$uid";
$body[] = "Content-type: text/html; charset=UTF-8";
$body[] = "Content-Type: text/html; charset=UTF-8";
$body[] = "Content-Transfer-Encoding: quoted-printable";
$body[] = '';
$body[] = quoted_printable_encode($this->message);
Expand All @@ -73,15 +73,15 @@ public function send(){
}

$body[] = "--$uid";
$body[] = "Content-type: application/octet-stream; name=\"".$name."\"";
$body[] = "Content-Type: application/octet-stream; name=\"".$name."\"";
$body[] = "Content-Transfer-Encoding: base64";
$body[] = "Content-Disposition: attachment; filename=\"".$name."\"";
$body[] = '';
$body[] = chunk_split(base64_encode($data));
$body[] = '';
}

$body[] = "--$uid--";
$body[] = "--$uid";

$success = true;
$head = implode("\r\n",$head);
Expand Down
190 changes: 16 additions & 174 deletions classes/Email/Ses.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,187 +12,29 @@

namespace Email;

class Ses implements Driver {

protected
$recipients = [],
$attachments = [],
$from,
$replyTo,
$subject,
$message,
$socket,
$host,
$secure,
$port,
$lastCode,
$lastMessage,
$username,
$password;
class Ses extends Smtp {

public function __construct($options = null) {
$options = (object)$options;
$this->host = isset($options->host) ? $options->host : 'localhost';
$this->username = isset($options->username) ? $options->username : false;
$this->secure = isset($options->secure) ? $options->secure : ($this->username ? true : false);
$this->port = isset($options->port) ? $options->port : ($this->secure ? 465 : 25);
$this->password = isset($options->password) ? $options->password : false;
}

protected function connect(){
if ($this->socket) $this->close();
$url = ($this->username ? 'ssl' : 'tcp') ."://{$this->host}";
$this->socket = fsockopen( $url, $this->port, $errno, $errstr, 30 );
if ( ! $this->socket ) throw new \Exception("Unable to connect to $url on port {$this->port}.");
$this->lastMessage = '';
$this->lastCode = 0;
}

public function close(){
$this->socket && @fclose($this->socket);
}
$options = (object)$options;
$region = isset($options->region) ? $options->region : 'eu-west-1';
if (empty($options->username) || empty($options->password))
throw new \Exception("[Core.Email.SES] You must provide Amazon SES SMTP username and password", 1);

protected function write($data, $nl = 1){
$payload = $data . str_repeat("\r\n",$nl);
fwrite($this->socket, $payload);
}

protected function expectCode($code){

$this->lastMessage = '';
while (substr($this->lastMessage, 3, 1) != ' '){
$this->lastMessage = fgets($this->socket, 256);
}
Smtp::__construct([
'host' => "email-smtp.{$region}.amazonaws.com",
'secure' => true,
'port' => 465,
'username' => $options->username,
'password' => $options->password,
]);

$this->lastCode = 1 * substr($this->lastMessage, 0, 3);
return $code == $this->lastCode;
if (!empty($options->from)) $this->from($options->from);
}

public function addAddress($email,$name=''){
if (!$email) return;
if (empty($name) ) $name = strtok($email,'@');
$this->recipients[] = (object)['name'=>$name,'email'=>$email,'full'=>"$name <{$email}>"];
}

public function from($email,$name=''){
if (!$email) return;
if (empty($name) ) $name = strtok($email,'@');
$this->from = (object)['name'=>$name,'email'=>$email,'full'=>"$name <{$email}>"];
}

public function replyTo($email,$name=''){
if (!$email) return;
if (empty($name) ) $name = strtok($email,'@');
$this->replyTo = (object)['name'=>$name,'email'=>$email,'full'=>"$name <{$email}>"];
}

public function subject($text){
$this->subject = $text;
}

public function message($text){
$this->message = $text;
}

public function addAttachment($file){
$this->attachments[] = $file;
}

protected function SMTPmail($to,$subject,$body,$heads=''){
$this->connect();
$this->expectCode(220);

$this->write("EHLO {$this->localhost}");
$this->expectCode(250);

if ($this->username){
$this->write("AUTH LOGIN");
$this->expectCode(334);
$this->write(base64_encode($this->username));
$this->expectCode(334);
$this->write(base64_encode($this->password));
$this->expectCode(334);
}

$this->write("MAIL FROM: <{$this->from->email}>");
$this->expectCode(250);

$this->write("RCPT TO: <{$to}>");
$this->expectCode(250);

$this->write("DATA");
$this->expectCode(354);

$this->write("Subject: {$subject}");

$this->write($heads);
$this->write($body);

$this->write(".");
$success = $this->expectCode(250);

$this->write("QUIT");

$this->close();
return $success;
}


public function send(){
$uid = md5(uniqid(time()));
$headers = [];

if($this->from) $headers[] = 'From: '.$this->from->full;
if($this->replyTo) $headers[] = 'Reply-To: '.$this->replyTo->full;

$headers[] = 'MIME-Version: 1.0';
$headers[] = "Content-Type: multipart/mixed; boundary=\"".$uid."\"";
$headers[] = "This is a multi-part message in MIME format.";
$headers[] = "--$uid";
$headers[] = "Content-type: text/html; charset=UTF-8";
$headers[] = "Content-Transfer-Encoding: quoted-printable";
$headers[] = '';
$headers[] = quoted_printable_encode($this->message);
$headers[] = '';


foreach ($this->attachments as $file) {

if (is_string($file)) {
$name = basename($file);
$data = file_get_contents($file);
} else {
$name = $file['name'];
$data = $file['content'];
}

$headers[] = "--$uid";
$headers[] = "Content-type: application/octet-stream; name=\"".$name."\"";
$headers[] = "Content-Transfer-Encoding: base64";
$headers[] = "Content-Disposition: attachment; filename=\"".$name."\"";
$headers[] = '';
$headers[] = chunk_split(base64_encode($data));
$headers[] = '';
}

$headers[] = "--$uid--";

$success = true;

$body = implode("\r\n",$headers);
foreach ($this->recipients as $to) {

$current_success = $this->SMTPmail(
$to->email,
$this->subject,
'',
$body
);

\Event::trigger('core.email.send',$to->full,$this->from->full,$this->subject,$body,$success);
$success = $success && $current_success;
}
return $success;
if (empty($this->from->full))
throw new \Exception("[Core.Email.SES] Amazon SES needs a registered `from` address", 1);
return Smtp::send();
}

}
Expand Down
13 changes: 7 additions & 6 deletions classes/Email/Smtp.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function __construct($options = null) {

protected function connect(){
if ($this->socket) $this->close();
$url = ($this->username ? 'ssl' : 'tcp') ."://{$this->host}";
$url = ($this->secure ? 'tls' : 'tcp') ."://{$this->host}";
$this->socket = fsockopen( $url, $this->port, $errno, $errstr, 30 );
if ( ! $this->socket ) throw new \Exception("Unable to connect to $url on port {$this->port}.");
$this->lastMessage = '';
Expand Down Expand Up @@ -139,17 +139,17 @@ protected function SMTPmail($to,$subject,$body,$heads=''){


public function send(){
$uid = md5(uniqid(time()));
$uid = '_CORE_'.md5(uniqid(time()));
$headers = [];

if($this->from) $headers[] = 'From: '.$this->from->full;
if($this->replyTo) $headers[] = 'Reply-To: '.$this->replyTo->full;

$headers[] = 'MIME-Version: 1.0';
$headers[] = "Content-Type: multipart/mixed; boundary=\"".$uid."\"";
$headers[] = "This is a multi-part message in MIME format.";
$headers[] = "";
$headers[] = "--$uid";
$headers[] = "Content-type: text/html; charset=UTF-8";
$headers[] = "Content-Type: text/html; charset=UTF-8";
$headers[] = "Content-Transfer-Encoding: quoted-printable";
$headers[] = '';
$headers[] = quoted_printable_encode($this->message);
Expand All @@ -167,19 +167,20 @@ public function send(){
}

$headers[] = "--$uid";
$headers[] = "Content-type: application/octet-stream; name=\"".$name."\"";
$headers[] = "Content-Type: application/octet-stream; name=\"".$name."\"";
$headers[] = "Content-Transfer-Encoding: base64";
$headers[] = "Content-Disposition: attachment; filename=\"".$name."\"";
$headers[] = '';
$headers[] = chunk_split(base64_encode($data));
$headers[] = '';
}

$headers[] = "--$uid--";
$headers[] = "--$uid";

$success = true;

$body = implode("\r\n",$headers);

foreach ($this->recipients as $to) {

$current_success = $this->SMTPmail(
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "Components for rapid application development.",
"keywords": ["framework","core","sdk","toolbox"],
"homepage": "http://caffeina.com",
"version": "1.6.0",
"version": "1.6.1",
"license": "MIT",
"authors": [
{
Expand Down

0 comments on commit a5af0d4

Please sign in to comment.