Skip to content

Commit

Permalink
add class mail(new)
Browse files Browse the repository at this point in the history
  • Loading branch information
amirfaramarzi committed Aug 16, 2021
1 parent ebad903 commit f2276eb
Show file tree
Hide file tree
Showing 4 changed files with 287 additions and 2 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"symfony/polyfill-ctype": "^1.8",
"elasticsearch/elasticsearch": "^7.0",
"vlucas/phpdotenv": "^5.3",
"ext-redis": "*"
"ext-redis": "*",
"phpmailer/phpmailer": "^6.5"
} ,
"autoload": {
"psr-4": {
Expand Down
74 changes: 73 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public function onWorkerStart(): void

Elastic::setInstance();

Mail::setInstance();

$this->setRedis();

$this->setDatabase();
Expand Down
210 changes: 210 additions & 0 deletions src/Mail.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
<?php

namespace Tower;

use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\PHPMailer;

class Mail
{
protected static PHPMailer $instance;

public static function setInstance()
{
$config = include configPath() . "mail.php";

self::$instance = new PHPMailer(true);

switch (env('MAIL_MAILER' , 'smtp')) {
case 'smtp':
self::$instance->isSMTP();
if ($config['username'] != null && $config['password'] != null)
self::$instance->SMTPAuth = true;
self::$instance->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
break;
case 'mail':
self::$instance->isMail();
break;
case 'sendmail':
self::$instance->isSendmail();
break;
case 'qmail':
self::$instance->isQmail();
break;
}

self::$instance->Host = $config['host'];
self::$instance->Username = $config['username'];
self::$instance->Password = $config['password'];
self::$instance->Port = $config['port'];

try {
self::$instance->setFrom(env('MAIL_FROM_ADDRESS', 'hello@example.com'), env('MAIL_FROM_NAME', 'Example'));
} catch (Exception $e) {
(new Log())->channel('mailer')->error($e->getMessage());
}
}

public function to(string|array $address): self
{
if (is_string($address))
try {
self::$instance->addAddress($address);
} catch (Exception $e) {
(new Log())->channel('mailer')->error($e->getMessage());
}
else
foreach ($address as $value){
if (is_string($value))
try {
self::$instance->addAddress($value);
} catch (Exception $e) {
(new Log())->channel('mailer')->error($e->getMessage());
}

if (is_array($value) && isset($value['address']) && isset($value['name']))
try {
self::$instance->addAddress($value['address'], $value['name']);
} catch (Exception $e) {
(new Log())->channel('mailer')->error($e->getMessage());
}
}

return $this;
}

public function replyTo(string|array $address): self
{
if (is_string($address))
try {
self::$instance->addReplyTo($address);
} catch (Exception $e) {
(new Log())->channel('mailer')->error($e->getMessage());
}
else
foreach ($address as $value){
if (is_string($value))
try {
self::$instance->addReplyTo($value);
} catch (Exception $e) {
(new Log())->channel('mailer')->error($e->getMessage());
}

if (is_array($value) && isset($value['address']) && isset($value['name']))
try {
self::$instance->addReplyTo($value['address'], $value['name']);
} catch (Exception $e) {
(new Log())->channel('mailer')->error($e->getMessage());
}
}

return $this;
}

public function cc(string|array $address): self
{
if (is_string($address))
try {
self::$instance->addCC($address);
} catch (Exception $e) {
(new Log())->channel('mailer')->error($e->getMessage());
}
else
foreach ($address as $value){
if (is_string($value))
try {
self::$instance->addCC($value);
} catch (Exception $e) {
(new Log())->channel('mailer')->error($e->getMessage());
}

if (is_array($value) && isset($value['address']) && isset($value['name']))
try {
self::$instance->addCC($value['address'], $value['name']);
} catch (Exception $e) {
(new Log())->channel('mailer')->error($e->getMessage());
}
}

return $this;
}

public function bcc(string|array $address): self
{
if (is_string($address))
try {
self::$instance->addBCC($address);
} catch (Exception $e) {
(new Log())->channel('mailer')->error($e->getMessage());
}
else
foreach ($address as $value){
if (is_string($value))
try {
self::$instance->addBCC($value);
} catch (Exception $e) {
(new Log())->channel('mailer')->error($e->getMessage());
}

if (is_array($value) && isset($value['address']) && isset($value['name']))
try {
self::$instance->addBCC($value['address'], $value['name']);
} catch (Exception $e) {
(new Log())->channel('mailer')->error($e->getMessage());
}
}

return $this;
}

public function withFile(string|array $file): self
{
if (is_string($file))
try {
self::$instance->addAttachment($file);
} catch (Exception $e) {
(new Log())->channel('mailer')->error($e->getMessage());
}
else
foreach ($file as $value)
try {
self::$instance->addAttachment($value);
} catch (Exception $e) {
(new Log())->channel('mailer')->error($e->getMessage());
}

return $this;
}

public function subject(string $subject): self
{
self::$instance->Subject = $subject;

return $this;
}

public function body(string $body): self
{
self::$instance->Body = $body;

return $this;
}

public function altBody(string $altBody): self
{
self::$instance->AltBody = $altBody;

return $this;
}

public function send()
{
self::$instance->isHTML(true);

try {
self::$instance->send();
} catch (Exception $e) {
(new Log())->channel('mailer')->error($e->getMessage());
}
}
}

0 comments on commit f2276eb

Please sign in to comment.