|
5 | 5 | use Dacastro4\LaravelGmail\Services\Message\Mail; |
6 | 6 | use Google_Service_Gmail; |
7 | 7 | use Google_Service_Gmail_Message; |
| 8 | +use Symfony\Component\Mime\Address; |
8 | 9 | use Symfony\Component\Mime\Email; |
9 | 10 | use Illuminate\Container\Container; |
10 | 11 | use Illuminate\Mail\Markdown; |
@@ -401,49 +402,63 @@ private function getMessageBody() |
401 | 402 | { |
402 | 403 | $body = new Google_Service_Gmail_Message(); |
403 | 404 |
|
404 | | - if ($this->from) { |
405 | | - $this->symfonyEmail->from($this->from, ($this->nameFrom ? $this->nameFrom : '')); |
406 | | - } |
407 | | - |
408 | | - if ($this->to) { |
409 | | - $this->symfonyEmail->to($this->to, ($this->nameTo ? $this->nameTo : '')); |
410 | | - } |
411 | | - |
412 | | - if ($this->cc) { |
413 | | - if (is_array($this->cc)) { |
414 | | - foreach ($this->cc as $emailCc => $nameCc) { |
415 | | - $this->symfonyEmail->addCc($emailCc, $nameCc); |
416 | | - } |
417 | | - } else { |
418 | | - $this->symfonyEmail->cc($this->cc, ($this->nameCc ? $this->nameCc : '')); |
419 | | - } |
420 | | - } |
421 | | - |
422 | | - $bccString = ""; |
423 | | - if ($this->bcc) { |
424 | | - if (is_array($this->bcc)) { |
425 | | - foreach ($this->bcc as $emailBcc => $nameBcc) { |
426 | | - $bccString .= "Bcc: " . $nameBcc . " <" . $emailBcc . ">\r\n"; |
427 | | - } |
428 | | - } else { |
429 | | - $bccString .= "Bcc: " . ($this->nameBcc ? $this->nameBcc . " " : "") . "<" . $this->bcc . ">\r\n"; |
430 | | - } |
431 | | - } |
432 | | - |
433 | | - $this->symfonyEmail->subject($this->subject) |
434 | | - ->html($this->message) |
435 | | - ->priority($this->priority) |
436 | | - ; |
437 | | - |
438 | | - foreach ($this->attachments as $file) { |
439 | | - $this->symfonyEmail->attachFromPath($file); |
440 | | - } |
441 | | - |
442 | | - $body->setRaw($this->base64_encode($bccString . $this->symfonyEmail->toString())); |
| 405 | + $this->symfonyEmail |
| 406 | + ->from($this->fromAddress()) |
| 407 | + ->to($this->toAddress()) |
| 408 | + ->cc($this->returnCopies($this->cc)) |
| 409 | + ->bcc($this->returnCopies($this->bcc)) |
| 410 | + ->subject($this->subject) |
| 411 | + ->html($this->message) |
| 412 | + ->priority($this->priority); |
| 413 | + |
| 414 | + foreach ($this->attachments as $file) { |
| 415 | + $this->symfonyEmail->attachFromPath($file); |
| 416 | + } |
| 417 | + |
| 418 | + $body->setRaw($this->base64_encode($this->symfonyEmail->toString())); |
443 | 419 |
|
444 | 420 | return $body; |
445 | 421 | } |
446 | 422 |
|
| 423 | + /** |
| 424 | + * @param array|string $cc |
| 425 | + * @return array|string |
| 426 | + */ |
| 427 | + public function returnCopies($cc) |
| 428 | + { |
| 429 | + if ($cc) { |
| 430 | + $final = $this->cc; |
| 431 | + |
| 432 | + if (is_array($this->cc)) { |
| 433 | + foreach ($this->cc as $emailCc => $nameCc) { |
| 434 | + $final[] = new Address($emailCc, $nameCc); |
| 435 | + } |
| 436 | + } |
| 437 | + |
| 438 | + return $final; |
| 439 | + } |
| 440 | + |
| 441 | + return []; |
| 442 | + } |
| 443 | + |
| 444 | + public function toAddress() |
| 445 | + { |
| 446 | + if ($this->to) { |
| 447 | + return new Address($this->to, $this->nameTo ?: ''); |
| 448 | + } |
| 449 | + |
| 450 | + return []; |
| 451 | + } |
| 452 | + |
| 453 | + public function fromAddress() |
| 454 | + { |
| 455 | + if ($this->from) { |
| 456 | + return new Address($this->from, $this->nameFrom ?: ''); |
| 457 | + } |
| 458 | + |
| 459 | + return []; |
| 460 | + } |
| 461 | + |
447 | 462 | private function base64_encode($data) |
448 | 463 | { |
449 | 464 | return rtrim(strtr(base64_encode($data), ['+' => '-', '/' => '_']), '='); |
|
0 commit comments