Skip to content

Commit

Permalink
Fixed new swift mail
Browse files Browse the repository at this point in the history
  • Loading branch information
dacastro4 committed Aug 31, 2022
1 parent 585c39f commit a852b5c
Showing 1 changed file with 54 additions and 39 deletions.
93 changes: 54 additions & 39 deletions src/Traits/Replyable.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Dacastro4\LaravelGmail\Services\Message\Mail;
use Google_Service_Gmail;
use Google_Service_Gmail_Message;
use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\Email;
use Illuminate\Container\Container;
use Illuminate\Mail\Markdown;
Expand Down Expand Up @@ -401,49 +402,63 @@ private function getMessageBody()
{
$body = new Google_Service_Gmail_Message();

if ($this->from) {
$this->symfonyEmail->from($this->from, ($this->nameFrom ? $this->nameFrom : ''));
}

if ($this->to) {
$this->symfonyEmail->to($this->to, ($this->nameTo ? $this->nameTo : ''));
}

if ($this->cc) {
if (is_array($this->cc)) {
foreach ($this->cc as $emailCc => $nameCc) {
$this->symfonyEmail->addCc($emailCc, $nameCc);
}
} else {
$this->symfonyEmail->cc($this->cc, ($this->nameCc ? $this->nameCc : ''));
}
}

$bccString = "";
if ($this->bcc) {
if (is_array($this->bcc)) {
foreach ($this->bcc as $emailBcc => $nameBcc) {
$bccString .= "Bcc: " . $nameBcc . " <" . $emailBcc . ">\r\n";
}
} else {
$bccString .= "Bcc: " . ($this->nameBcc ? $this->nameBcc . " " : "") . "<" . $this->bcc . ">\r\n";
}
}

$this->symfonyEmail->subject($this->subject)
->html($this->message)
->priority($this->priority)
;

foreach ($this->attachments as $file) {
$this->symfonyEmail->attachFromPath($file);
}

$body->setRaw($this->base64_encode($bccString . $this->symfonyEmail->toString()));
$this->symfonyEmail
->from($this->fromAddress())
->to($this->toAddress())
->cc($this->returnCopies($this->cc))
->bcc($this->returnCopies($this->bcc))
->subject($this->subject)
->html($this->message)
->priority($this->priority);

foreach ($this->attachments as $file) {
$this->symfonyEmail->attachFromPath($file);
}

$body->setRaw($this->base64_encode($this->symfonyEmail->toString()));

return $body;
}

/**
* @param array|string $cc
* @return array|string
*/
public function returnCopies($cc)
{
if ($cc) {
$final = $this->cc;

if (is_array($this->cc)) {
foreach ($this->cc as $emailCc => $nameCc) {
$final[] = new Address($emailCc, $nameCc);
}
}

return $final;
}

return [];
}

public function toAddress()
{
if ($this->to) {
return new Address($this->to, $this->nameTo ?: '');
}

return [];
}

public function fromAddress()
{
if ($this->from) {
return new Address($this->from, $this->nameFrom ?: '');
}

return [];
}

private function base64_encode($data)
{
return rtrim(strtr(base64_encode($data), ['+' => '-', '/' => '_']), '=');
Expand Down

0 comments on commit a852b5c

Please sign in to comment.