Skip to content

Commit 454a0bf

Browse files
committed
🎉 created base project
0 parents  commit 454a0bf

File tree

7 files changed

+324
-0
lines changed

7 files changed

+324
-0
lines changed

.github/FUNDING.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# These are supported funding model platforms
2+
3+
open_collective: leaf

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
test
2+
Experimental
3+
vendor
4+
composer.lock

README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<!-- markdownlint-disable no-inline-html -->
2+
<p align="center">
3+
<br><br>
4+
<img src="https://leafphp.netlify.app/assets/img/leaf3-logo.png" height="100"/>
5+
<h1 align="center">Leaf Mail Module</h1>
6+
<br><br>
7+
</p>
8+
9+
# Leaf PHP
10+
11+
[![Latest Stable Version](https://poser.pugx.org/leafs/mail/v/stable)](https://packagist.org/packages/leafs/mail)
12+
[![Total Downloads](https://poser.pugx.org/leafs/mail/downloads)](https://packagist.org/packages/leafs/mail)
13+
[![License](https://poser.pugx.org/leafs/mail/license)](https://packagist.org/packages/leafs/mail)
14+
15+
Leaf's mail template engine packaged as a serve-yourself module.
16+
17+
## Installation
18+
19+
You can easily install Leaf using [Composer](https://getcomposer.org/).
20+
21+
```bash
22+
composer require leafs/mail
23+
```
24+
25+
## View Leaf's docs [here](https://leafphp.netlify.app/#/)
26+
27+
Built with ❤ by [**Mychi Darko**](https://mychi.netlify.app)

composer.json

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"name": "leafs/mail",
3+
"description": "Leaf PHP bareui templating engine",
4+
"keywords": [
5+
"template",
6+
"simple templating",
7+
"view",
8+
"leaf",
9+
"php",
10+
"framework"
11+
],
12+
"homepage": "https://leafphp.netlify.app/#/",
13+
"type": "library",
14+
"license": "MIT",
15+
"authors": [
16+
{
17+
"name": "Michael Darko",
18+
"email": "mickdd22@gmail.com",
19+
"homepage": "https://mychi.netlify.app",
20+
"role": "Developer"
21+
}
22+
],
23+
"autoload": {
24+
"psr-4": {
25+
"Leaf\\": "src"
26+
}
27+
},
28+
"minimum-stability": "stable",
29+
"require": {
30+
"phpmailer/phpmailer": "^6.5"
31+
}
32+
}

src/Mail.php

Lines changed: 224 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,224 @@
1+
<?php
2+
3+
namespace Leaf;
4+
5+
use \Leaf\Mail\SMTP;
6+
7+
/**
8+
* Leaf Mail
9+
* ------------------------------
10+
* Leaf PHP Framework's implementation of PHPMailer\PHPMailer
11+
*
12+
* @author Michael Darko
13+
* @version 1.0.0
14+
* @since 2.0.0
15+
*/
16+
class Mail extends \PHPMailer\PHPMailer\PHPMailer
17+
{
18+
private $errs;
19+
20+
public function __construct(String $host = null, int $port = null, array $auth = [], bool $debug = true, bool $server_debug = false)
21+
{
22+
parent::__construct($debug);
23+
24+
if ($host) {
25+
$this->smtp_connect($host, $port, empty($auth) ? false : true, empty($auth) ? null : $auth["username"], empty($auth) ? null : $auth["password"]);
26+
}
27+
28+
if ($server_debug == true) {
29+
$this->smtp_debug("SERVER");
30+
}
31+
$this->form = new Form;
32+
}
33+
34+
public function smtp_debug($debug = "SERVER")
35+
{
36+
if ($debug == "SERVER") {
37+
$this->SMTPDebug = SMTP::DEBUG_SERVER;
38+
} else {
39+
$this->SMTPDebug = $debug;
40+
}
41+
}
42+
43+
/**
44+
* Quickly Initialise a new SMTP connection
45+
*/
46+
public function smtp_connect(String $host, int $port, bool $uses_auth = false, String $username = null, String $password = null, $security = "STARTTLS")
47+
{
48+
$this->isSMTP();
49+
$this->Host = $host;
50+
$this->Port = $port;
51+
if ($uses_auth == true) {
52+
$this->SMTPAuth = true;
53+
$this->Username = $username;
54+
$this->Password = $password;
55+
}
56+
if ($security == "STARTTLS") {
57+
$this->SMTPSecure = self::ENCRYPTION_STARTTLS;
58+
} else {
59+
$this->SMTPSecure = $security;
60+
}
61+
}
62+
63+
/**
64+
* Send a basic email
65+
*/
66+
public function basic(String $subject, String $content, String $recepient_email, String $sender_name, String $sender_email = null, String $cc = null, String $bcc = null)
67+
{
68+
if ($sender_email == null) {
69+
$sender_email = $this->Username;
70+
}
71+
72+
if (!$this->form->isEmail($recepient_email)) {
73+
$this->errs = "Recepient email not valid";
74+
return false;
75+
}
76+
77+
if ($sender_email != null && !$this->form->isEmail($sender_email)) {
78+
$this->errs = "Sender email not valid";
79+
return false;
80+
}
81+
82+
$this->Subject = $subject;
83+
$this->isHTML(true);
84+
$this->Body = $content;
85+
$this->addAddress($recepient_email);
86+
$this->setFrom($sender_email, $sender_name);
87+
88+
if ($cc) {
89+
$this->addCC($cc);
90+
}
91+
92+
if ($bcc) {
93+
$this->addBCC($bcc);
94+
}
95+
96+
return $this;
97+
}
98+
99+
/**
100+
* Quickly write an email
101+
*
102+
* Use these parameters in your email array
103+
* - subject
104+
* - body | template
105+
* - recepient_email
106+
* - sender_name
107+
* - sender_email
108+
* - attachment
109+
* - cc
110+
* - bcc
111+
*/
112+
public function write(array $email)
113+
{
114+
if (!isset($email["sender_email"])) {
115+
$email["sender_email"] = $this->Username;
116+
}
117+
118+
if (isset($email["recepient_email"]) && !$this->form->isEmail($email["recepient_email"])) {
119+
$this->errs = "Recepient email not valid";
120+
return false;
121+
}
122+
123+
if (isset($email["sender_email"]) && !$this->form->isEmail($email["sender_email"])) {
124+
$this->errs = "Sender email not valid";
125+
return false;
126+
}
127+
128+
if (isset($email["cc"])) {
129+
$this->addCC($email["cc"]);
130+
}
131+
132+
if (isset($email["bcc"])) {
133+
$this->addBCC($email["bcc"]);
134+
}
135+
136+
if (isset($email["subject"])) {
137+
$this->Subject = $email["subject"];
138+
} else {
139+
$this->errs = "Subject (subject) is required";
140+
return false;
141+
}
142+
143+
if (isset($email["body"])) {
144+
$this->isHTML(true);
145+
$this->Body = $email["body"];
146+
}
147+
148+
if (isset($email["recepient_email"])) {
149+
$this->addAddress($email["recepient_email"]);
150+
} else {
151+
$this->errs = "Recepient email (recepient_email) is required";
152+
return false;
153+
}
154+
155+
if (isset($email["attachment"])) {
156+
$this->addAttachment($email["attachment"]);
157+
}
158+
159+
if (isset($email["template"])) {
160+
$this->loadTemplate($email["template"]);
161+
}
162+
163+
if (isset($email["sender_name"])) {
164+
$this->setFrom($email["sender_email"], $email["sender_name"]);
165+
} else {
166+
$this->errs = "Sender name (sender_name) is required";
167+
return false;
168+
}
169+
170+
return $this;
171+
}
172+
173+
/**
174+
* Load an already prepared template(file) as email body
175+
*/
176+
public function loadTemplate(String $path, bool $isReturned = false)
177+
{
178+
$this->isHTML(true);
179+
$this->Body = file_get_contents($path);
180+
if ($isReturned == true) {
181+
return $this->Body;
182+
}
183+
}
184+
185+
/**
186+
* Add an attachment from a path on the filesystem.
187+
* Never use a user-supplied path to a file!
188+
* Returns false if the file could not be found or read.
189+
* Explicitly *does not* support passing URLs; PHPMailer is not an HTTP client.
190+
* If you need to do that, fetch the resource yourself and pass it in via a local file or string.
191+
*
192+
* @param string $path Path to the attachment
193+
* @param string $name Overrides the attachment name
194+
* @param string $encoding File encoding (see $Encoding)
195+
* @param string $type File extension (MIME) type
196+
* @param string $disposition Disposition to use
197+
*
198+
* @throws Exception
199+
*
200+
* @return bool
201+
*/
202+
public function attach($path, $name = "", $encoding = parent::ENCODING_BASE64, $type = '', $disposition = 'attachment')
203+
{
204+
return $this->addAttachment($path, $name, $encoding, $type, $disposition);
205+
}
206+
207+
/**
208+
* Finalise and send an email
209+
*/
210+
// public function send() {
211+
// if (strlen($this->Subject) == 0) {
212+
// $this->errs = "Email Subject not found, this is required";
213+
// return false;
214+
// }
215+
// }
216+
217+
/**
218+
* Return any errors
219+
*/
220+
public function errors()
221+
{
222+
return $this->ErrorInfo . $this->errs;
223+
}
224+
}

src/Mail/Exception.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Leaf\Mail;
4+
5+
/**
6+
* Leaf Mail Exception
7+
* -------------------------
8+
* Leaf PHP Framework's implementation of PHPMailer\PHPMailer\Exception
9+
*
10+
* @author Michael Darko
11+
* @version 1.0.0
12+
* @since 2.0.0
13+
*/
14+
class Exception extends \PHPMailer\PHPMailer\Exception
15+
{
16+
# Add whatever you want here
17+
}

src/Mail/SMTP.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Leaf\Mail;
4+
5+
/**
6+
* Leaf Mail SMTP email transport class.
7+
* --------------------
8+
* Leaf PHP Framework's implementation of PHPMailer\SMTP
9+
*
10+
* @author Michael Darko
11+
* @version 1.0.0
12+
* @since 2.0.0
13+
*/
14+
class SMTP extends \PHPMailer\PHPMailer\SMTP
15+
{
16+
# Place any custom methods you'll need here
17+
}

0 commit comments

Comments
 (0)