From 175ad2149a143c41d6b5fd0302d30b59d2191f06 Mon Sep 17 00:00:00 2001 From: kerogs Date: Mon, 2 Sep 2024 22:06:07 +0200 Subject: [PATCH] Debug sendMail. Now sends mail correctly. No change in your codes is necessary --- .gitignore | 3 ++- composer.json | 2 +- readme.md | 23 ++++++++++++++++++++++- src/sendmail.php | 49 +++++++++++++++++++++--------------------------- 4 files changed, 46 insertions(+), 31 deletions(-) diff --git a/.gitignore b/.gitignore index 0012f7d..987c45f 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -index.php \ No newline at end of file +index.php +sendmailCorpse.htm \ No newline at end of file diff --git a/composer.json b/composer.json index 7cc4bd4..f1cad40 100644 --- a/composer.json +++ b/composer.json @@ -18,5 +18,5 @@ "Kerogs\\KerogsPhp\\": "src/" } }, - "version": "1.2" + "version": "1.3" } diff --git a/readme.md b/readme.md index 5615ea3..9ab3a9f 100644 --- a/readme.md +++ b/readme.md @@ -108,4 +108,25 @@ addLog( Encrypt or decrypt a log file. ```php encryptDecryptFile(string $filePath, bool $encrypt): void -``` \ No newline at end of file +``` + +### Kerogs\KerogsPhp\Sendmail +You can send your e-mails directly without rewriting the code. + +#### Example of us +```php +require_once __DIR__.'/vendor/autoload.php'; + +use Kerogs\KerogsPhp\Sendmail; + +const from = "froms@example.com"; +const to = "to@example.com"; + +$sendmail = new Sendmail(from); + +if($sendmail->sendMail(to, "Subject test", "hello world"))) + echo "Email sent successfully"; +else + echo "Email not sent"; +``` +The default content type is ``text/HTML`` \ No newline at end of file diff --git a/src/sendmail.php b/src/sendmail.php index 1531688..00866af 100644 --- a/src/sendmail.php +++ b/src/sendmail.php @@ -14,43 +14,36 @@ class Sendmail { private $customHeader; private $contentType; - - public function __construct( /** * Constructor method for Sendmail class. * * @param string $from The sender's email address. - * @param string $customHeader The custom header for the email. If not specified, a default header will be used. - * @param string $contentType The content type of the email. If not specified, a default content type of 'text/html' will be used. + * @param string|null $customHeader The custom header for the email. If not specified, a default header will be used. + * @param string|null $contentType The content type of the email. If not specified, a default content type of 'text/html' will be used. * * @throws \Exception If the sender's email address is not valid. */ - string $from = "", + public function __construct( + string $from, string $customHeader = null, string $contentType = null ) { - if(!$from){ - throw new \Exception("need a 'from' address"); - } else{ - if(!filter_var($from, FILTER_VALIDATE_EMAIL)) { - throw new \Exception("invalid 'from' address"); - } else{ - $this->from = $from; - } + if (empty($from)) { + throw new \Exception("Sender's email address is required."); } - if($customHeader !== null) { - $this->customHeader = $customHeader; - } else{ + if (!filter_var($from, FILTER_VALIDATE_EMAIL)) { + throw new \Exception("Invalid sender's email address."); + } - if($contentType !== null) { - $this->$customHeader = "Content-Type: ".$contentType."; charset=utf-8\r\n"; - } else{ - $this->$customHeader = "Content-Type: 'text/html'; charset=utf-8\r\n"; - } + $this->from = $from; - $this->$customHeader .= "From: " . $this->from."\r\n"; - } + // Set content type + $this->contentType = $contentType ?? 'text/html'; + + // Build headers + $this->customHeader = $customHeader ?? "Content-Type: " . $this->contentType . "; charset=utf-8\r\n"; + $this->customHeader .= "From: " . $this->from . "\r\n"; } /** @@ -64,11 +57,11 @@ public function __construct( * * @throws \Exception If the recipient's email address is not valid. */ - public function sendMail($to, $subject, $message):bool { - if(!filter_var($to, FILTER_VALIDATE_EMAIL)) { - throw new \Exception("invalid 'to' address"); - } else{ - return mail($to, $subject, $message, $this->customHeader); + public function sendMail(string $to, string $subject, string $message): bool { + if (!filter_var($to, FILTER_VALIDATE_EMAIL)) { + throw new \Exception("Invalid recipient's email address."); } + + return mail($to, $subject, $message, $this->customHeader); } }