Skip to content

Commit a9f1c5c

Browse files
authored
Merge pull request #3 from justcoded/feature/16410929_mandrill
Mandrill app mailer driver integration
2 parents 88edea0 + ec4cce8 commit a9f1c5c

8 files changed

Lines changed: 275 additions & 9 deletions

File tree

composer.json

Lines changed: 2 additions & 1 deletion
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

Lines changed: 47 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
] // according to Valitron doc.
4141
];
4242

43+
// PhpMailer config.
4344
$mailerConfig = [
4445
'mailer' => MailHandler::USE_PHPMAILER, // (or USE_POSTMARKAPP, USE_MANDRILL)
4546
'host' => 'smtp.gmail.com',

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/DataObjects/File.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,16 @@ public function getExtension()
8787
return pathinfo($this->name, PATHINFO_EXTENSION);
8888
}
8989

90+
/**
91+
* Represent image in base64 encode format
92+
*
93+
* @return string
94+
*/
95+
public function getBase64()
96+
{
97+
return base64_encode(file_get_contents($this->uploadPath));
98+
}
99+
90100
/**
91101
* Magic method for template converting
92102
*

src/Mailer/MailerFactory.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,22 @@
1212
class MailerFactory
1313
{
1414
/**
15-
* Creating Mailer
15+
* Create mailer depends on the type config
1616
*
17-
* @param string $type Type mailer
17+
* @param string $type Type of Mailer
1818
* @param array $config Mailer config
1919
*
20-
* @return PHPMailer
20+
* @return MandrillMailer|PHPMailer
2121
*/
22-
public static function create(string $type, array $config) {
22+
public static function create(string $type, array $config)
23+
{
2324
switch ($type) {
2425
case MailHandler::USE_PHPMAILER:
2526
$mailer = new PHPMailer($config);
2627
break;
28+
case MailHandler::USE_MANDRILL:
29+
$mailer = new MandrillMailer($config);
30+
break;
2731
default:
2832
new \Exception("MailerFactory: unable to find mailer for type \"{$type}\"");
2933
}

src/Mailer/MandrillMailer.php

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
<?php
2+
3+
namespace JustCoded\FormHandler\Mailer;
4+
5+
use JustCoded\FormHandler\DataObjects\DataObject;
6+
use JustCoded\FormHandler\DataObjects\EmailAttachment;
7+
use JustCoded\FormHandler\DataObjects\MailMessage;
8+
use Mandrill;
9+
use Mandrill_Error;
10+
11+
/**
12+
* Class MandrillMailer
13+
*
14+
* @package JustCoded\FormHandler\Mailer
15+
*/
16+
class MandrillMailer extends DataObject implements MailerInterface
17+
{
18+
/**
19+
* Attachments size limit
20+
*
21+
* @var int
22+
*/
23+
protected $attachmentsSizeLimit = 8000000;
24+
25+
/**
26+
* User from user config
27+
*
28+
* @var string
29+
*/
30+
protected $user;
31+
32+
/**
33+
* Password from user config
34+
*
35+
* @var string
36+
*/
37+
protected $apiKey;
38+
39+
/**
40+
* List of errors
41+
*
42+
* @var array
43+
*/
44+
protected $errors = array();
45+
46+
/**
47+
* Sending form
48+
*
49+
* @param MailMessage $message Mailer message
50+
*
51+
* @return array|bool
52+
*/
53+
public function send(MailMessage $message)
54+
{
55+
try {
56+
$mandrill = new Mandrill($this->apiKey);
57+
58+
$mandrillMessage = array(
59+
'html' => $message->getBody(),
60+
'subject' => $message->getSubject(),
61+
'from_email' => $message->getFrom()->getEmail(),
62+
'from_name' => $message->getFrom()->getName(),
63+
);
64+
65+
// Recipients.
66+
$recipients = [
67+
'to' => $message->getTo(),
68+
'cc' => $message->getCc(),
69+
'bcc' => $message->getBcc()
70+
];
71+
72+
$to = [];
73+
foreach ($recipients as $type => $emails) {
74+
if (empty($emails)) continue;
75+
foreach ($emails as $email) {
76+
$to[] = [
77+
'email' => $email->getEmail(),
78+
'name' => $email->getName(),
79+
'type' => $type
80+
];
81+
}
82+
}
83+
84+
$mandrillMessage['to'] = $to;
85+
86+
// Attachments.
87+
if (0 < $message->getAttachmentsSize() && $message->getAttachmentsSize() < $this->attachmentsSizeLimit
88+
&& $attachments = $message->getAttachments()
89+
) {
90+
$attachmentsArray = [];
91+
foreach ($attachments as $attachment) {
92+
$attachmentsArray[] = [
93+
'type' => $attachment->type,
94+
'name' => $attachment->name,
95+
'content' => $attachment->getBase64()
96+
];
97+
}
98+
99+
$mandrillMessage['attachments'] = $attachmentsArray;
100+
}
101+
102+
$result = $mandrill->messages->send($mandrillMessage);
103+
104+
return $result;
105+
} catch (Mandrill_Error $e) {
106+
$this->errors[] = 'A mandrill error occurred: ' . get_class($e) . ' - ' . $e->getMessage();
107+
return false;
108+
}
109+
}
110+
111+
/**
112+
* Getting list of errors
113+
*
114+
* @return array
115+
*/
116+
public function getErrors()
117+
{
118+
return $this->errors;
119+
}
120+
}

0 commit comments

Comments
 (0)