Skip to content

Commit f2b3f68

Browse files
committed
Merged latest changes
2 parents 1f35c92 + 53ef3d7 commit f2b3f68

7 files changed

Lines changed: 185 additions & 85 deletions

File tree

examples/form.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
'mailer' => MailHandler::USE_PHPMAILER, // (or USE_POSTMARKAPP, USE_MANDRILL)
3333
'host' => 'smtp.gmail.com',
3434
'user' => 'kos1985.dev@gmail.com',
35-
'pass' => '',
35+
'password' => 'kos409834',
3636
];
3737

3838
$message = [
@@ -46,12 +46,12 @@
4646
];
4747

4848

49-
$mailerHandler = new MailHandler($mailerConfig, new Message($message));
49+
$mailerHandler = new MailHandler($mailerConfig);
5050

5151
$formHandler = new FormHandler($validation, $mailerHandler);
5252

5353
if ($formHandler->validate($_POST)) {
54-
$formHandler->process();
54+
$formHandler->process(new Message($message));
5555
}
5656

5757
echo json_encode($formHandler->response());

src/DataObject/DataObject.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
namespace justcoded\form2email\DataObject;
4+
5+
6+
class DataObject
7+
{
8+
/**
9+
* @var array
10+
*/
11+
public $config;
12+
13+
public function __construct(array $config)
14+
{
15+
$this->config = $config;
16+
}
17+
18+
/**
19+
* @return mixed|null
20+
*/
21+
public function getMailerId()
22+
{
23+
if (array_key_exists('mailer', $this->config)) {
24+
return $this->config['mailer'];
25+
}
26+
27+
return null;
28+
}
29+
30+
/**
31+
* @return mixed|null
32+
*/
33+
public function getHost()
34+
{
35+
if (array_key_exists('host', $this->config)) {
36+
return $this->config['host'];
37+
}
38+
39+
return null;
40+
}
41+
42+
/**
43+
* @return mixed|null
44+
*/
45+
public function getUser()
46+
{
47+
if (array_key_exists('user', $this->config)) {
48+
return $this->config['user'];
49+
}
50+
51+
return null;
52+
}
53+
54+
/**
55+
* @return mixed|null
56+
*/
57+
public function getPassword()
58+
{
59+
if (array_key_exists('password', $this->config)) {
60+
return $this->config['password'];
61+
}
62+
63+
return null;
64+
}
65+
}

src/FormHandler.php

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,47 @@
44

55

66
use justcoded\form2email\Handler\HandlerInterface;
7+
use justcoded\form2email\Message\Message;
78
use Valitron\Validator;
89

910
class FormHandler
1011
{
12+
/**
13+
* @var array
14+
*/
1115
protected $validation;
1216

17+
/**
18+
* @var HandlerInterface
19+
*/
1320
protected $handler;
1421

22+
/**
23+
* @var string
24+
*/
1525
protected $response;
1626

27+
/**
28+
* @var array
29+
*/
1730
protected $errors = [];
1831

32+
/**
33+
* @var int
34+
*/
1935
protected $status = 0;
2036

37+
/**
38+
* @var array
39+
*/
2140
protected $formFields;
2241

42+
/**
43+
* FormHandler constructor.
44+
* @param array $validation
45+
* @param HandlerInterface $handler
46+
* @param string $response
47+
*/
2348
public function __construct(array $validation, HandlerInterface $handler, string $response = 'json')
2449
{
2550
$this->validation = $validation;
@@ -29,6 +54,10 @@ public function __construct(array $validation, HandlerInterface $handler, string
2954
$this->response = $response;
3055
}
3156

57+
/**
58+
* @param $post
59+
* @return bool
60+
*/
3261
public function validate($post)
3362
{
3463
$this->formFields = $post;
@@ -50,11 +79,17 @@ public function validate($post)
5079
}
5180
}
5281

53-
public function process()
82+
/**
83+
* @param Message $message
84+
*/
85+
public function process(Message $message)
5486
{
55-
$this->handler->process($this->formFields);// sending email
87+
$this->handler->process($this->formFields, $message);// sending email
5688
}
5789

90+
/**
91+
* @return array
92+
*/
5893
public function response()
5994
{
6095
return [

src/Handler/HandlerInterface.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
namespace justcoded\form2email\Handler;
44

5+
6+
use justcoded\form2email\Message\Message;
7+
58
interface HandlerInterface
69
{
7-
public function process($formFields);
10+
public function process($formFields, Message $message);
811
}

src/Handler/MailHandler.php

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,36 @@
22

33
namespace justcoded\form2email\Handler;
44

5+
use justcoded\form2email\DataObject\DataObject;
56
use justcoded\form2email\Mailer\PhpHandlerSend;
67
use justcoded\form2email\Message\Message;
78

8-
class MailHandler implements HandlerInterface
9+
class MailHandler extends DataObject implements HandlerInterface
910
{
1011
const USE_PHPMAILER = 1;
1112
const USE_POSTMARKAPP = 2;
1213
const USE_MANDRILL = 3;
1314

14-
protected $config;
15-
protected $message;
16-
17-
public function __construct(array $config, Message $message)
18-
{
19-
$this->config = $config;
20-
$this->message = $message;
21-
}
22-
15+
/**
16+
* @return PhpHandlerSend
17+
* @throws \Exception
18+
*/
2319
public function getMailer()
2420
{
25-
$mailerId = $this->config['mailer'];
26-
27-
switch ($mailerId) {
21+
switch ($this->getMailerId()) {
2822
case self::USE_PHPMAILER:
29-
return new PhpHandlerSend($this->config, $this->message);
23+
return new PhpHandlerSend($this->config);
3024
}
3125

3226
throw new \Exception('Bad config');
3327
}
3428

35-
public function process($data)
29+
/**
30+
* @param $data
31+
* @param Message $message
32+
*/
33+
public function process($data, Message $message)
3634
{
37-
$this->getMailer()->send($data);
35+
$this->getMailer()->send($data, $message);
3836
}
3937
}

src/Mailer/PhpHandlerSend.php

Lines changed: 13 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -2,64 +2,15 @@
22

33
namespace justcoded\form2email\Mailer;
44

5-
5+
use justcoded\form2email\DataObject\DataObject;
66
use justcoded\form2email\Message\Message;
77
use PHPMailer\PHPMailer\PHPMailer;
88
use PHPMailer\PHPMailer\Exception;
99

10-
class PhpHandlerSend
10+
class PhpHandlerSend extends DataObject
1111
{
12-
protected $message;
13-
14-
protected $host;
15-
16-
protected $userName;
17-
18-
protected $password;
19-
20-
public function __construct(array $config, Message $message)
21-
{
22-
if (array_key_exists('host', $config)) {
23-
$this->host = $config['host'];
24-
}
25-
26-
if (array_key_exists('user', $config)) {
27-
$this->userName = $config['user'];
28-
}
29-
30-
if (array_key_exists('pass', $config)) {
31-
$this->password = $config['pass'];
32-
}
33-
34-
$this->message = $message;
35-
}
36-
37-
/**
38-
* @return mixed
39-
*/
40-
public function getHost()
41-
{
42-
return $this->host;
43-
}
44-
45-
/**
46-
* @return mixed
47-
*/
48-
public function getUserName()
49-
{
50-
return $this->userName;
51-
}
52-
53-
/**
54-
* @return mixed
55-
*/
56-
public function getPassword()
57-
{
58-
return $this->password;
59-
}
60-
6112

62-
public function send($formFields)
13+
public function send($formFields, Message $message)
6314
{
6415
$mail = new PHPMailer(true); // Passing `true` enables exceptions
6516
try {
@@ -68,24 +19,24 @@ public function send($formFields)
6819
$mail->isSMTP(); // Set mailer to use SMTP
6920
$mail->Host = $this->getHost(); // Specify main and backup SMTP servers
7021
$mail->SMTPAuth = true; // Enable SMTP authentication
71-
$mail->Username = $this->getUserName(); // SMTP username
22+
$mail->Username = $this->getUser(); // SMTP username
7223
$mail->Password = $this->getPassword(); // SMTP password
7324
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
7425
$mail->Port = 587; // TCP port to connect to
7526

7627
//Recipients
77-
if ($this->message->getFromAddress() != '' && $this->message->getFromName() != '') {
78-
$mail->setFrom($this->message->getFromAddress(), $this->message->getFromName());
28+
if ($message->getFromAddress() != '' && $message->getFromName() != '') {
29+
$mail->setFrom($message->getFromAddress(), $message->getFromName());
7930
}
8031
// $mail->addAddress($this->message->getFrom()); // Add a recipient
8132
// $mail->addReplyTo('info@example.com', 'Information');
8233

83-
if ($this->message->getCcAddress() != '' && $this->message->getBccName() != '') {
84-
$mail->addCC($this->message->getCcAddress(), $this->message->getBccName());
34+
if ($message->getCcAddress() != '' && $message->getBccName() != '') {
35+
$mail->addCC($message->getCcAddress(), $message->getBccName());
8536
}
8637

87-
if ($this->message->getBccAddress() != '' && $this->message->getBccName() != '') {
88-
$mail->addBCC($this->message->getBccAddress(), $this->message->getBccName());
38+
if ($message->getBccAddress() != '' && $message->getBccName() != '') {
39+
$mail->addBCC($message->getBccAddress(), $message->getBccName());
8940
}
9041

9142
//Attachments
@@ -94,9 +45,9 @@ public function send($formFields)
9445

9546
//Content
9647
$mail->isHTML(true); // Set email format to HTML
97-
$mail->Subject = $this->message->getSubject();
98-
$mail->Body = $this->message->getTemplate($formFields);
99-
$mail->AltBody = $this->message->getAltBody();
48+
$mail->Subject = $message->getSubject();
49+
$mail->Body = $message->getTemplate($formFields);
50+
$mail->AltBody = $message->getAltBody();
10051

10152
$mail->send();
10253

0 commit comments

Comments
 (0)