Skip to content

Commit

Permalink
🎉 created base project
Browse files Browse the repository at this point in the history
  • Loading branch information
mychidarko committed Sep 19, 2021
0 parents commit 454a0bf
Show file tree
Hide file tree
Showing 7 changed files with 324 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# These are supported funding model platforms

open_collective: leaf
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
test
Experimental
vendor
composer.lock
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<!-- markdownlint-disable no-inline-html -->
<p align="center">
<br><br>
<img src="https://leafphp.netlify.app/assets/img/leaf3-logo.png" height="100"/>
<h1 align="center">Leaf Mail Module</h1>
<br><br>
</p>

# Leaf PHP

[![Latest Stable Version](https://poser.pugx.org/leafs/mail/v/stable)](https://packagist.org/packages/leafs/mail)
[![Total Downloads](https://poser.pugx.org/leafs/mail/downloads)](https://packagist.org/packages/leafs/mail)
[![License](https://poser.pugx.org/leafs/mail/license)](https://packagist.org/packages/leafs/mail)

Leaf's mail template engine packaged as a serve-yourself module.

## Installation

You can easily install Leaf using [Composer](https://getcomposer.org/).

```bash
composer require leafs/mail
```

## View Leaf's docs [here](https://leafphp.netlify.app/#/)

Built with ❤ by [**Mychi Darko**](https://mychi.netlify.app)
32 changes: 32 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"name": "leafs/mail",
"description": "Leaf PHP bareui templating engine",
"keywords": [
"template",
"simple templating",
"view",
"leaf",
"php",
"framework"
],
"homepage": "https://leafphp.netlify.app/#/",
"type": "library",
"license": "MIT",
"authors": [
{
"name": "Michael Darko",
"email": "[email protected]",
"homepage": "https://mychi.netlify.app",
"role": "Developer"
}
],
"autoload": {
"psr-4": {
"Leaf\\": "src"
}
},
"minimum-stability": "stable",
"require": {
"phpmailer/phpmailer": "^6.5"
}
}
224 changes: 224 additions & 0 deletions src/Mail.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,224 @@
<?php

namespace Leaf;

use \Leaf\Mail\SMTP;

/**
* Leaf Mail
* ------------------------------
* Leaf PHP Framework's implementation of PHPMailer\PHPMailer
*
* @author Michael Darko
* @version 1.0.0
* @since 2.0.0
*/
class Mail extends \PHPMailer\PHPMailer\PHPMailer
{
private $errs;

public function __construct(String $host = null, int $port = null, array $auth = [], bool $debug = true, bool $server_debug = false)
{
parent::__construct($debug);

if ($host) {
$this->smtp_connect($host, $port, empty($auth) ? false : true, empty($auth) ? null : $auth["username"], empty($auth) ? null : $auth["password"]);
}

if ($server_debug == true) {
$this->smtp_debug("SERVER");
}
$this->form = new Form;
}

public function smtp_debug($debug = "SERVER")
{
if ($debug == "SERVER") {
$this->SMTPDebug = SMTP::DEBUG_SERVER;
} else {
$this->SMTPDebug = $debug;
}
}

/**
* Quickly Initialise a new SMTP connection
*/
public function smtp_connect(String $host, int $port, bool $uses_auth = false, String $username = null, String $password = null, $security = "STARTTLS")
{
$this->isSMTP();
$this->Host = $host;
$this->Port = $port;
if ($uses_auth == true) {
$this->SMTPAuth = true;
$this->Username = $username;
$this->Password = $password;
}
if ($security == "STARTTLS") {
$this->SMTPSecure = self::ENCRYPTION_STARTTLS;
} else {
$this->SMTPSecure = $security;
}
}

/**
* Send a basic email
*/
public function basic(String $subject, String $content, String $recepient_email, String $sender_name, String $sender_email = null, String $cc = null, String $bcc = null)
{
if ($sender_email == null) {
$sender_email = $this->Username;
}

if (!$this->form->isEmail($recepient_email)) {
$this->errs = "Recepient email not valid";
return false;
}

if ($sender_email != null && !$this->form->isEmail($sender_email)) {
$this->errs = "Sender email not valid";
return false;
}

$this->Subject = $subject;
$this->isHTML(true);
$this->Body = $content;
$this->addAddress($recepient_email);
$this->setFrom($sender_email, $sender_name);

if ($cc) {
$this->addCC($cc);
}

if ($bcc) {
$this->addBCC($bcc);
}

return $this;
}

/**
* Quickly write an email
*
* Use these parameters in your email array
* - subject
* - body | template
* - recepient_email
* - sender_name
* - sender_email
* - attachment
* - cc
* - bcc
*/
public function write(array $email)
{
if (!isset($email["sender_email"])) {
$email["sender_email"] = $this->Username;
}

if (isset($email["recepient_email"]) && !$this->form->isEmail($email["recepient_email"])) {
$this->errs = "Recepient email not valid";
return false;
}

if (isset($email["sender_email"]) && !$this->form->isEmail($email["sender_email"])) {
$this->errs = "Sender email not valid";
return false;
}

if (isset($email["cc"])) {
$this->addCC($email["cc"]);
}

if (isset($email["bcc"])) {
$this->addBCC($email["bcc"]);
}

if (isset($email["subject"])) {
$this->Subject = $email["subject"];
} else {
$this->errs = "Subject (subject) is required";
return false;
}

if (isset($email["body"])) {
$this->isHTML(true);
$this->Body = $email["body"];
}

if (isset($email["recepient_email"])) {
$this->addAddress($email["recepient_email"]);
} else {
$this->errs = "Recepient email (recepient_email) is required";
return false;
}

if (isset($email["attachment"])) {
$this->addAttachment($email["attachment"]);
}

if (isset($email["template"])) {
$this->loadTemplate($email["template"]);
}

if (isset($email["sender_name"])) {
$this->setFrom($email["sender_email"], $email["sender_name"]);
} else {
$this->errs = "Sender name (sender_name) is required";
return false;
}

return $this;
}

/**
* Load an already prepared template(file) as email body
*/
public function loadTemplate(String $path, bool $isReturned = false)
{
$this->isHTML(true);
$this->Body = file_get_contents($path);
if ($isReturned == true) {
return $this->Body;
}
}

/**
* Add an attachment from a path on the filesystem.
* Never use a user-supplied path to a file!
* Returns false if the file could not be found or read.
* Explicitly *does not* support passing URLs; PHPMailer is not an HTTP client.
* If you need to do that, fetch the resource yourself and pass it in via a local file or string.
*
* @param string $path Path to the attachment
* @param string $name Overrides the attachment name
* @param string $encoding File encoding (see $Encoding)
* @param string $type File extension (MIME) type
* @param string $disposition Disposition to use
*
* @throws Exception
*
* @return bool
*/
public function attach($path, $name = "", $encoding = parent::ENCODING_BASE64, $type = '', $disposition = 'attachment')
{
return $this->addAttachment($path, $name, $encoding, $type, $disposition);
}

/**
* Finalise and send an email
*/
// public function send() {
// if (strlen($this->Subject) == 0) {
// $this->errs = "Email Subject not found, this is required";
// return false;
// }
// }

/**
* Return any errors
*/
public function errors()
{
return $this->ErrorInfo . $this->errs;
}
}
17 changes: 17 additions & 0 deletions src/Mail/Exception.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Leaf\Mail;

/**
* Leaf Mail Exception
* -------------------------
* Leaf PHP Framework's implementation of PHPMailer\PHPMailer\Exception
*
* @author Michael Darko
* @version 1.0.0
* @since 2.0.0
*/
class Exception extends \PHPMailer\PHPMailer\Exception
{
# Add whatever you want here
}
17 changes: 17 additions & 0 deletions src/Mail/SMTP.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Leaf\Mail;

/**
* Leaf Mail SMTP email transport class.
* --------------------
* Leaf PHP Framework's implementation of PHPMailer\SMTP
*
* @author Michael Darko
* @version 1.0.0
* @since 2.0.0
*/
class SMTP extends \PHPMailer\PHPMailer\SMTP
{
# Place any custom methods you'll need here
}

0 comments on commit 454a0bf

Please sign in to comment.