Skip to content

Commit 0c57e66

Browse files
committedJan 29, 2018
added mandrill lib
1 parent efdec2c commit 0c57e66

File tree

6 files changed

+263
-60
lines changed

6 files changed

+263
-60
lines changed
 

‎composer.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
"name": "justcoded/form-handler",
33
"require": {
44
"vlucas/valitron": "^1.4",
5-
"phpmailer/phpmailer": "^6.0"
5+
"phpmailer/phpmailer": "^6.0",
6+
"mandrill/mandrill": "1.0.*"
67
},
78
"autoload": {
89
"files": [

‎composer.lock

+47-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎examples/form.php

+19-20
Original file line numberDiff line numberDiff line change
@@ -22,44 +22,43 @@
2222
'fields' => ['email'],
2323
'message' => '{field} is not a valid email address'
2424
],
25-
'file' => [
26-
'fields' => ['cv_file', 'image_file'],
27-
'allowType' => ['jpeg', 'jpg', 'pdf', 'png'],
28-
'allowSize' => 10000000 //10 MB
29-
]
30-
], // acoording to Valitron doc.
25+
'file' => [
26+
'fields' => ['cv_file', 'image_file'],
27+
'allowType' => ['jpeg', 'jpg', 'pdf', 'png'],
28+
'allowSize' => 10000000 // 10 MB
29+
]
30+
], // according to Valitron doc.
3131
'labels' => [
3232
'name' => 'Name',
3333
'email' => 'Email address'
34-
] // acoording to Valitron doc.
34+
] // according to Valitron doc.
3535
];
3636

3737
$mailerConfig = [
38-
'mailer' => MailHandler::USE_PHPMAILER, // (or USE_POSTMARKAPP, USE_MANDRILL)
38+
'mailer' => MailHandler::USE_MANDRILL, // (or USE_POSTMARKAPP, USE_MANDRILL)
3939
'host' => 'smtp.gmail.com',
40-
'user' => 'YOUR EMAIL',
41-
'password' => 'YOUR PASSWORD',
40+
'user' => 'roz-mary',
41+
'password' => '_5mPSvb39BQqnA7G_dOaAA',
4242
'protocol' => 'tls',
4343
'port' => 587,
4444
];
4545

4646
$fileManager = new FileManager([
47-
'uploadPath' => __DIR__ . '/attachments',
48-
'uploadUrl' => $_SERVER['HTTP_ORIGIN'] . '/attachments',
49-
]
50-
);
47+
'uploadPath' => __DIR__ . '/attachments',
48+
'uploadUrl' => $_SERVER['HTTP_ORIGIN'] . '/attachments',
49+
]);
5150

5251
$message = [
53-
'from' => ['hello@justcoded.co.uk' => 'FROM NAME'],
54-
'to' => ['kostant21@yahoo.com' => 'TO NAME'],
52+
'from' => ['hello@justcoded.co.uk' => 'FROM NAME'],
53+
'to' => ['kostant21@yahoo.com' => 'TO NAME'],
5554
// 'cc' => ['email' => 'name'],
5655
// 'bcc' => ['email' => 'name'],
5756
'subject' => 'Contact request from {name}',
58-
'bodyTemplate' => __DIR__ . '/template-html.php',
57+
'bodyTemplate' => __DIR__ . '/template-html.php',
5958
'altBodyTemplate' => __DIR__ . '/template-plain.php',
60-
'attachments' => $fileManager->upload([
61-
'cv_file', 'image_file'
62-
])
59+
'attachments' => $fileManager->upload([
60+
'cv_file', 'image_file'
61+
])
6362
];
6463

6564

‎src/DataObjects/MailMessage.php

+51-34
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,14 @@
22

33
namespace JustCoded\FormHandler\DataObjects;
44

5+
/**
6+
* Class MailMessage
7+
*
8+
* @package JustCoded\FormHandler\DataObjects
9+
*/
510
class MailMessage extends DataObject
611
{
7-
const ATTACHMENTS_SIZE_LIMIT = 8000000;
12+
const ATTACHMENTS_SIZE_LIMIT = 8000000;
813

914
/**
1015
* @var EmailAddress
@@ -56,14 +61,14 @@ class MailMessage extends DataObject
5661
*/
5762
protected $tokens;
5863

59-
/**
60-
* @var array
61-
*/
64+
/**
65+
* @var array
66+
*/
6267
protected $attachments = [];
6368

64-
/**
65-
* @var array
66-
*/
69+
/**
70+
* @var array
71+
*/
6772
protected $files = [];
6873

6974
/**
@@ -168,6 +173,8 @@ public function getBody()
168173
}
169174

170175
/**
176+
* Get message alt body
177+
*
171178
* @return string
172179
*/
173180
public function getAltBody()
@@ -179,33 +186,43 @@ public function getAltBody()
179186
}
180187
}
181188

189+
/**
190+
* Set attachments file
191+
*
192+
* @return bool
193+
*/
182194
public function setFiles()
183-
{
184-
foreach ($this->attachments as $file)
185-
{
186-
/** @var File $file */
187-
if (!$file->size > self::ATTACHMENTS_SIZE_LIMIT) {
188-
$this->addFile([$file->uploadPath => $file->name]);
189-
}
190-
}
191-
192-
return true;
193-
}
194-
195-
/**
196-
* @return array
197-
*/
198-
public function getFiles()
199-
{
200-
return $this->files;
201-
}
202-
203-
/**
204-
* @param $data
205-
*/
206-
protected function addFile($data)
207-
{
208-
$this->files[] = new EmailAttachment($data);
209-
}
195+
{
196+
foreach ($this->attachments as $file) {
197+
/**
198+
* File object
199+
*
200+
* @var File $file
201+
*/
202+
if (!$file->size > self::ATTACHMENTS_SIZE_LIMIT) {
203+
$this->addFile([$file->uploadPath => $file->name]);
204+
}
205+
}
206+
207+
return true;
208+
}
209+
210+
/**
211+
* Get attachments file
212+
*
213+
* @return array
214+
*/
215+
public function getFiles()
216+
{
217+
return $this->files;
218+
}
219+
220+
/**
221+
* @param $data
222+
*/
223+
protected function addFile($data)
224+
{
225+
$this->files[] = new EmailAttachment($data);
226+
}
210227

211228
}

‎src/Mailer/MailerFactory.php

+13-2
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,26 @@
33
namespace JustCoded\FormHandler\Mailer;
44

55
use JustCoded\FormHandler\Handlers\MailHandler;
6-
use JustCoded\FormHandler\Mailer\PHPMailer;
76

87
class MailerFactory
98
{
10-
public static function create($type, $config) {
9+
/**
10+
* Create mailer depends on the type config
11+
*
12+
* @param string $type Type of Mailer
13+
* @param array $config Mailer config
14+
*
15+
* @return MandrillMailer|PHPMailer
16+
*/
17+
public static function create(string $type, array $config)
18+
{
1119
switch ($type) {
1220
case MailHandler::USE_PHPMAILER:
1321
$mailer = new PHPMailer($config);
1422
break;
23+
case MailHandler::USE_MANDRILL:
24+
$mailer = new MandrillMailer($config);
25+
break;
1526
default:
1627
new \Exception("MailerFactory: unable to find mailer for type \"{$type}\"");
1728
}

0 commit comments

Comments
 (0)
Please sign in to comment.