|
| 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 | +} |
0 commit comments