Skip to content

Commit

Permalink
Debug sendMail. Now sends mail correctly. No change in your codes is …
Browse files Browse the repository at this point in the history
…necessary
  • Loading branch information
kerogs committed Sep 2, 2024
1 parent a47240c commit 175ad21
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 31 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
index.php
index.php
sendmailCorpse.htm
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@
"Kerogs\\KerogsPhp\\": "src/"
}
},
"version": "1.2"
"version": "1.3"
}
23 changes: 22 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,25 @@ addLog(
Encrypt or decrypt a log file.
```php
encryptDecryptFile(string $filePath, bool $encrypt): void
```
```

### 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 = "[email protected]";
const to = "[email protected]";

$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``
49 changes: 21 additions & 28 deletions src/sendmail.php
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}

/**
Expand All @@ -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);
}
}

0 comments on commit 175ad21

Please sign in to comment.