|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace FFMVC\Helpers; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Mail Helper Class. |
|
7
|
|
|
* |
|
8
|
|
|
* For getPhpMailer add to composer.json: |
|
9
|
|
|
* "phpmailer/phpmailer": "dev-master" |
|
10
|
|
|
* |
|
11
|
|
|
* @author Vijay Mahrra <[email protected]> |
|
12
|
|
|
* @copyright (c) Copyright 2016 Vijay Mahrra |
|
13
|
|
|
* @license GPLv3 (http://www.gnu.org/licenses/gpl-3.0.html) |
|
14
|
|
|
*/ |
|
15
|
|
|
class Mail extends \Prefab |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* Return an instance of PHPMailer populated with application settings |
|
19
|
|
|
* |
|
20
|
|
|
* @param array $data |
|
21
|
|
|
* @return \PHPMailer |
|
22
|
|
|
* @url https://github.com/PHPMailer/PHPMailer |
|
23
|
|
|
*/ |
|
24
|
|
|
public static function &getPhpMailer(array $data = []): \PHPMailer |
|
25
|
|
|
{ |
|
26
|
|
|
$f3 = \Base::instance(); |
|
27
|
|
|
|
|
28
|
|
|
$mail = new \PHPMailer(); |
|
29
|
|
|
$mail->isSMTP(); |
|
30
|
|
|
$mail->isHTML(true); |
|
31
|
|
|
$mail->CharSet = $f3->get('ENCODING'); |
|
32
|
|
|
$mail->Username = $f3->get('email.user'); |
|
33
|
|
|
$mail->Password = $f3->get('email.pass'); |
|
34
|
|
|
$mail->Port = $f3->get('email.port'); |
|
35
|
|
|
$mail->Host = $f3->get('email.host'); |
|
36
|
|
|
$mail->Timeout = $f3->get('ttl.email'); |
|
37
|
|
|
|
|
38
|
|
|
if ($f3->get('email.sendmail')) { |
|
39
|
|
|
$mail->Sendmail = 'smtp://' . $f3->get('email.host') . ':' . $f3->get('email.port'); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
$mail->FromName = $f3->get('email.from_name'); |
|
43
|
|
|
$mail->From = $f3->get('email.from'); |
|
44
|
|
|
|
|
45
|
|
|
// PHPMailer doesn't take 'To' so we have to do that with addAddress() |
|
46
|
|
|
if (array_key_exists('To', $data)) { |
|
47
|
|
|
$mail->addAddress($data['To']); |
|
48
|
|
|
unset($data['To']); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
// finally set other values like overrides |
|
52
|
|
|
foreach ($data as $k => $v) { |
|
53
|
|
|
$mail->$k = $v; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
return $mail; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Return an instance of f3 \SMTP populated with application settings |
|
62
|
|
|
* |
|
63
|
|
|
* @param array $data |
|
64
|
|
|
* @return \SMTP |
|
65
|
|
|
*/ |
|
66
|
|
|
public static function &getMailer(array $data = []): \SMTP |
|
67
|
|
|
{ |
|
68
|
|
|
$f3 = \Base::instance(); |
|
69
|
|
|
|
|
70
|
|
|
$smtp = new \SMTP( |
|
71
|
|
|
$f3->get('email.host'), |
|
72
|
|
|
$f3->get('email.port'), |
|
73
|
|
|
$f3->get('email.scheme'), |
|
74
|
|
|
$f3->get('email.user'), |
|
75
|
|
|
$f3->get('email.pass') |
|
76
|
|
|
); |
|
77
|
|
|
|
|
78
|
|
|
$smtp->set('From', $f3->get('email.from')); |
|
79
|
|
|
|
|
80
|
|
|
// finally set other values like overrides |
|
81
|
|
|
foreach ($data as $k => $v) { |
|
82
|
|
|
$smtp->set($k, $v); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
return $smtp; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
} |
|
89
|
|
|
|