Skip to content

Commit

Permalink
Merge pull request #12 from NIOLAB/niolab
Browse files Browse the repository at this point in the history
Attachment support
  • Loading branch information
Julian-B90 authored Jun 19, 2019
2 parents 70980f7 + f991a24 commit 607571c
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 18 deletions.
45 changes: 35 additions & 10 deletions Mailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,32 +131,57 @@ public function getResponse()
protected function sendMessage($message)
{

$recipients = [];
$to = $cc = $bcc = [];

foreach ($message->to as $email => $name) {

$newRecipient = [];

if (!empty($email)) {
$newRecipient['Email'] = $email;
$address = '<' . $email . '>';
if (!empty($name)) {
$address = '"' . $name . '" ' . $address;
}
$to[] = $address;
}

foreach ($message->cc as $email => $name) {
$address = '<' . $email . '>';
if (!empty($name)) {
$newRecipient['Name'] = $name;
$address = '"' . $name . '" ' . $address;
}
$cc[] = $address;
}


$recipients[] = $newRecipient;
foreach ($message->bcc as $email => $name) {
$address = '<' . $email . '>';
if (!empty($name)) {
$address = '"' . $name . '" ' . $address;
}
$bcc[] = $address;
}


$body = [
'Subject' => $message->subject,
'Text-part' => $message->textBody,
'Html-part' => $message->htmlBody,
'Recipients' => $recipients,
'To' => join(', ',$to),
];

if ($cc) {
$body['Cc'] = join(', ',$cc);
}
if ($bcc) {
$body['Bcc'] = join(', ', $bcc);
}

if ($message->attachments) {
$body['Attachments'] = $message->attachments;
}
if ($message->inlineAttachments) {
$body['Inline_attachments'] = $message->inlineAttachments;
}

//Adds Reply-To to header
if(!empty($message->replyTo)){
if(!empty($message->replyTo)) {
$body['Headers']['Reply-to'] = $message->replyTo;
}

Expand Down
65 changes: 57 additions & 8 deletions Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,24 @@ class Message extends BaseMessage {

private $_from;

private $_to;
private $_to = [];

private $_replyTo;

private $_cc;
private $_cc = [];

private $_bcc;
private $_bcc = [];

private $_subject;

private $_textBody;

private $_htmlBody;

private $_attachments;

private $_inline_attachments;

/**
* @inheritdoc
*/
Expand Down Expand Up @@ -80,7 +84,7 @@ public function getTo() {
* @inheritdoc
*/
public function setTo($to) {
if (!is_array($to)){
if (!is_array($to)) {
$to = [$to => ''];
}
$this->_to = $to;
Expand Down Expand Up @@ -113,6 +117,9 @@ public function getCc() {
* @inheritdoc
*/
public function setCc($cc) {
if (!is_array($cc)) {
$cc = [$cc => ''];
}
$this->_cc = $cc;
return $this;
}
Expand All @@ -128,6 +135,9 @@ public function getBcc() {
* @inheritdoc
*/
public function setBcc($bcc) {
if (!is_array($bcc)){
$bcc = [$bcc => ''];
}
$this->_bcc = $bcc;
return $this;
}
Expand Down Expand Up @@ -181,28 +191,52 @@ public function setHtmlBody($html) {
* @inheritdoc
*/
public function attach($fileName, array $options = []) {
throw new Exception('Not Implemented');
$attachment = [
'Content-type' => isset($options['Content-type']) ? $options['Content-type'] : \yii\helpers\FileHelper::getMimeType($fileName),
'Filename' => isset($options['fileName']) ? $options['fileName'] : basename($fileName),
'content' => base64_encode(file_get_contents($fileName)),
];
$this->_attachments[] = $attachment;
return $this;
}

/**
* @inheritdoc
*/
public function attachContent($content, array $options = []) {
throw new Exception('Not Implemented');
$attachment = [
'Content-type' => isset($options['Content-type']) ? $options['Content-type'] : 'text/plain',
'Filename' => isset($options['fileName']) ? $options['fileName'] : 'attachment.txt',
'content' => base64_encode($content),
];
$this->_attachments[] = $attachment;
return $this;
}

/**
* @inheritdoc
*/
public function embed($fileName, array $options = []) {
throw new Exception('Not Implemented');
$attachment = [
'Content-type' => isset($options['Content-type']) ? $options['Content-type'] : \yii\helpers\FileHelper::getMimeType($fileName),
'Filename' => isset($options['fileName']) ? $options['fileName'] : basename($fileName),
'content' => base64_encode(file_get_contents($fileName)),
];
$this->_inline_attachments[] = $attachment;
return 'cid:' . $attachment['Filename'];
}

/**
* @inheritdoc
*/
public function embedContent($content, array $options = []) {
throw new Exception('Not Implemented');
$attachment = [
'Content-type' => isset($options['Content-type']) ? $options['Content-type'] : 'text/plain',
'Filename' => isset($options['fileName']) ? $options['fileName'] : 'attachment.txt',
'content' => base64_encode($content),
];
$this->_inline_attachments[] = $attachment;
return 'cid:' . $attachment['Filename'];
}

/**
Expand All @@ -214,4 +248,19 @@ public function toString() {
. $this->getTextBody();
}

/**
* @return mixed
*/
public function getAttachments()
{
return $this->_attachments;
}

/**
* @return mixed
*/
public function getInlineAttachments()
{
return $this->_inline_attachments;
}
}

0 comments on commit 607571c

Please sign in to comment.