Skip to content

Commit b530681

Browse files
committed
code review
1 parent ee6825f commit b530681

4 files changed

Lines changed: 105 additions & 47 deletions

File tree

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,21 +41,14 @@
4141
];
4242

4343
// PhpMailer config.
44-
/*$mailerConfig = [
45-
'mailer' => MailHandler::USE_MANDRILL, // (or USE_POSTMARKAPP, USE_MANDRILL)
44+
$mailerConfig = [
45+
'mailer' => MailHandler::USE_PHPMAILER, // (or USE_POSTMARKAPP, USE_MANDRILL)
4646
'host' => 'smtp.gmail.com',
4747
'user' => 'YOUR EMAIL',
4848
'password' => 'YOUR PASSWORD',
4949
'protocol' => 'tls',
5050
'port' => 587,
5151
'attachmentsSizeLimit' => 8000000, // around 8MB.
52-
];*/
53-
54-
// Mandrill config.
55-
$mailerConfig = [
56-
'mailer' => MailHandler::USE_MANDRILL, // (or USE_POSTMARKAPP, USE_MANDRILL)
57-
'password' => '_5mPSvb39BQqnA7G_dOaAA',
58-
'attachmentsSizeLimit' => 8000000, // around 8MB.
5952
];
6053

6154
$fileManager = new FileManager([

examples/form2email-mandrill.php

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
// enable errors => debug mode.
4+
ini_set('display_errors', 1);
5+
error_reporting(E_ALL);
6+
7+
// init autoload.
8+
require __DIR__ . '/../vendor/autoload.php';
9+
10+
use JustCoded\FormHandler\FileManager\FileManager;
11+
use JustCoded\FormHandler\FormHandler;
12+
use JustCoded\FormHandler\Handlers\MailHandler;
13+
use JustCoded\FormHandler\DataObjects\MailMessage;
14+
15+
$validation = [
16+
'fields' => [
17+
'name' => ['required'],
18+
'email' => ['required', 'email'],
19+
'subject' => ['required'],
20+
'message' => [
21+
'required',
22+
['lengthMin', 5]
23+
],
24+
'cv_file' => [
25+
[
26+
'required',
27+
'message' => 'Please upload {field}',
28+
],
29+
[
30+
'file',
31+
['jpeg', 'jpg', 'png'], // types.
32+
2000000, // size limit 2 MB.
33+
'message' => '{field} should be up to 2MB and allows only file types jpeg, png.',
34+
],
35+
],
36+
], // according to Valitron doc for mapFieldsRules.
37+
'labels' => [
38+
'name' => 'Name',
39+
'email' => 'Email address'
40+
] // according to Valitron doc.
41+
];
42+
43+
// Mandrill config.
44+
$mailerConfig = [
45+
'mailer' => MailHandler::USE_MANDRILL, // (or USE_POSTMARKAPP, USE_MANDRILL)
46+
'apiKey' => '_5mPSvb39BQqnA7G_dOaAA',
47+
'attachmentsSizeLimit' => 8000000, // around 8MB.
48+
];
49+
50+
$fileManager = new FileManager([
51+
'uploadPath' => __DIR__ . '/attachments',
52+
'uploadUrl' => 'http://MY-DOMAIN.COM/attachments',
53+
]);
54+
55+
$message = [
56+
'from' => ['hello@justcoded.co.uk' => 'FROM NAME'],
57+
'to' => ['kostant21@yahoo.com' => 'TO NAME'],
58+
// 'cc' => ['email' => 'name'],
59+
// 'bcc' => ['email' => 'name'],
60+
'subject' => 'Contact request from {name}',
61+
'bodyTemplate' => __DIR__ . '/template-html.php',
62+
'altBodyTemplate' => __DIR__ . '/template-plain.php',
63+
'attachments' => $fileManager->upload([
64+
'cv_file', 'image_file'
65+
])
66+
];
67+
68+
69+
$mailer = new MailHandler($mailerConfig, new MailMessage($message));
70+
$formHandler = new FormHandler($validation, $mailer);
71+
72+
if ($formHandler->validate($_POST)) {
73+
$formHandler->process();
74+
}
75+
76+
echo json_encode($formHandler->response());

examples/index.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<html>
22
<body>
33

4-
<form action="form.php" method="post" enctype="multipart/form-data">
4+
<form action="form2email-basic.php" method="post" enctype="multipart/form-data">
55
Name: <input type="text" name="name"><br>
66
E-mail: <input type="text" name="email"><br>
77
Subject: <input type="text" name="subject"><br>
@@ -10,6 +10,16 @@
1010
<p>File2:<input type="file" name="image_file"></p>
1111
<input type="submit">
1212
</form>
13+
<hr>
14+
<form action="form2email-mandrill.php" method="post" enctype="multipart/form-data">
15+
Name: <input type="text" name="name"><br>
16+
E-mail: <input type="text" name="email"><br>
17+
Subject: <input type="text" name="subject"><br>
18+
Message: <textarea name="message"></textarea>
19+
<p>File1:<input type="file" name="cv_file"></p>
20+
<p>File2:<input type="file" name="image_file"></p>
21+
<input type="submit">
22+
</form>
1323
1424
</body>
1525
</html>

src/Mailer/MandrillMailer.php

Lines changed: 16 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class MandrillMailer extends DataObject implements MailerInterface
3434
*
3535
* @var string
3636
*/
37-
protected $password;
37+
protected $apiKey;
3838

3939
/**
4040
* List of errors
@@ -53,50 +53,32 @@ class MandrillMailer extends DataObject implements MailerInterface
5353
public function send(MailMessage $message)
5454
{
5555
try {
56-
$mandrill = new Mandrill($this->password);
56+
$mandrill = new Mandrill($this->apiKey);
5757

5858
$mandrillMessage = array(
5959
'html' => $message->getBody(),
60-
'text' => 'Example text content',
6160
'subject' => $message->getSubject(),
6261
'from_email' => $message->getFrom()->getEmail(),
6362
'from_name' => $message->getFrom()->getName(),
6463
);
6564

6665
// Recipients.
67-
if ($to = $message->getTo()) {
68-
$toArray = [];
69-
foreach ($to as $address) {
70-
$toArray[] = [
71-
'email' => $address->getEmail(),
72-
'name' => $address->getName(),
73-
'type' => 'to'
66+
$recipients = ['to' => $message->getTo(), 'cc' => $message->getCc(), 'bcc' => $message->getBcc()];
67+
68+
$to = [];
69+
foreach ($recipients as $type => $emails) {
70+
if (empty($emails)) continue;
71+
foreach ($emails as $email) {
72+
$to[] = [
73+
'email' => $email->getEmail(),
74+
'name' => $email->getName(),
75+
'type' => $type
7476
];
7577
}
76-
77-
if ($cc = $message->getCc()) {
78-
foreach ($cc as $address) {
79-
$toArray[] = [
80-
'email' => $address->getEmail(),
81-
'name' => $address->getName(),
82-
'type' => 'cc'
83-
];
84-
}
85-
}
86-
87-
if ($bcc = $message->getBcc()) {
88-
foreach ($bcc as $address) {
89-
$toArray[] = [
90-
'email' => $address->getEmail(),
91-
'name' => $address->getName(),
92-
'type' => 'bcc'
93-
];
94-
}
95-
}
96-
97-
$mandrillMessage['to'] = $toArray;
9878
}
9979

80+
$mandrillMessage['to'] = $to;
81+
10082
// Attachments.
10183
if (0 < $message->getAttachmentsSize() && $message->getAttachmentsSize() < $this->attachmentsSizeLimit
10284
&& $attachments = $message->getAttachments()
@@ -113,10 +95,7 @@ public function send(MailMessage $message)
11395
$mandrillMessage['attachments'] = $attachmentsArray;
11496
}
11597

116-
$async = false;
117-
$ip_pool = 'Main Pool';
118-
$send_at = date('Y-m-d h:i:s');
119-
$result = $mandrill->messages->send($mandrillMessage, $async, $ip_pool, $send_at);
98+
$result = $mandrill->messages->send($mandrillMessage);
12099

121100
return $result;
122101
} catch (Mandrill_Error $e) {
@@ -134,4 +113,4 @@ public function getErrors()
134113
{
135114
return $this->errors;
136115
}
137-
}
116+
}

0 commit comments

Comments
 (0)