Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"name": "justcoded/form-handler",
"require": {
"vlucas/valitron": "^1.4",
"phpmailer/phpmailer": "^6.0"
"phpmailer/phpmailer": "^6.0",
"mandrill/mandrill": "1.0.*"
},
"autoload": {
"files": [
Expand Down
50 changes: 47 additions & 3 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 10 additions & 2 deletions examples/form.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,22 @@
] // according to Valitron doc.
];

$mailerConfig = [
'mailer' => MailHandler::USE_PHPMAILER, // (or USE_POSTMARKAPP, USE_MANDRILL)
// PhpMailer config.
/*$mailerConfig = [
'mailer' => MailHandler::USE_MANDRILL, // (or USE_POSTMARKAPP, USE_MANDRILL)
'host' => 'smtp.gmail.com',
'user' => 'YOUR EMAIL',
'password' => 'YOUR PASSWORD',
'protocol' => 'tls',
'port' => 587,
'attachmentsSizeLimit' => 8000000, // around 8MB.
];*/

// Mandrill config.
$mailerConfig = [
'mailer' => MailHandler::USE_MANDRILL, // (or USE_POSTMARKAPP, USE_MANDRILL)
'password' => '_5mPSvb39BQqnA7G_dOaAA',
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's not password - it's apiKey!

'attachmentsSizeLimit' => 8000000, // around 8MB.
];
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer to have another example for mandrill to keep files simple.

Let's rename files as:

  • form2email-basic.php
  • form2email-mandrill.php


$fileManager = new FileManager([
Expand Down
10 changes: 10 additions & 0 deletions src/DataObjects/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,16 @@ public function getExtension()
return pathinfo($this->name, PATHINFO_EXTENSION);
}

/**
* Represent image in base64 encode format
*
* @return string
*/
public function getBase64()
{
return base64_encode(file_get_contents($this->uploadPath));
}

/**
* Magic method for template converting
*
Expand Down
12 changes: 8 additions & 4 deletions src/Mailer/MailerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,22 @@
class MailerFactory
{
/**
* Creating Mailer
* Create mailer depends on the type config
*
* @param string $type Type mailer
* @param string $type Type of Mailer
* @param array $config Mailer config
*
* @return PHPMailer
* @return MandrillMailer|PHPMailer
*/
public static function create(string $type, array $config) {
public static function create(string $type, array $config)
{
switch ($type) {
case MailHandler::USE_PHPMAILER:
$mailer = new PHPMailer($config);
break;
case MailHandler::USE_MANDRILL:
$mailer = new MandrillMailer($config);
break;
default:
new \Exception("MailerFactory: unable to find mailer for type \"{$type}\"");
}
Expand Down
137 changes: 137 additions & 0 deletions src/Mailer/MandrillMailer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
<?php

namespace JustCoded\FormHandler\Mailer;

use JustCoded\FormHandler\DataObjects\DataObject;
use JustCoded\FormHandler\DataObjects\EmailAttachment;
use JustCoded\FormHandler\DataObjects\MailMessage;
use Mandrill;
use Mandrill_Error;

/**
* Class MandrillMailer
*
* @package JustCoded\FormHandler\Mailer
*/
class MandrillMailer extends DataObject implements MailerInterface
{
/**
* Attachments size limit
*
* @var int
*/
protected $attachmentsSizeLimit = 8000000;

/**
* User from user config
*
* @var string
*/
protected $user;

/**
* Password from user config
*
* @var string
*/
protected $password;

/**
* List of errors
*
* @var array
*/
protected $errors = array();

/**
* Sending form
*
* @param MailMessage $message Mailer message
*
* @return array|bool
*/
public function send(MailMessage $message)
{
try {
$mandrill = new Mandrill($this->password);

$mandrillMessage = array(
'html' => $message->getBody(),
'text' => 'Example text content',
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is this?

'subject' => $message->getSubject(),
'from_email' => $message->getFrom()->getEmail(),
'from_name' => $message->getFrom()->getName(),
);

// Recipients.
if ($to = $message->getTo()) {
$toArray = [];
foreach ($to as $address) {
$toArray[] = [
'email' => $address->getEmail(),
'name' => $address->getName(),
'type' => 'to'
];
}

if ($cc = $message->getCc()) {
foreach ($cc as $address) {
$toArray[] = [
'email' => $address->getEmail(),
'name' => $address->getName(),
'type' => 'cc'
];
}
}

if ($bcc = $message->getBcc()) {
foreach ($bcc as $address) {
$toArray[] = [
'email' => $address->getEmail(),
'name' => $address->getName(),
'type' => 'bcc'
];
}
}

$mandrillMessage['to'] = $toArray;
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a bit of spaghetti code. $toArray should be defined before if() - otherwise you can get undefined var.

and you can write 1 code for all parts like:

$recipients = ['to' => $message->getTo(), 'cc' => $message->getCc(), ...]

foreach($recipients as $type => $emails) {
 if (empty($emails)) continue;
foreach($emails as $email) {
  $to[] = [email, name, type => $type]
}
}


// Attachments.
if (0 < $message->getAttachmentsSize() && $message->getAttachmentsSize() < $this->attachmentsSizeLimit
&& $attachments = $message->getAttachments()
) {
$attachmentsArray = [];
foreach ($attachments as $attachment) {
$attachmentsArray[] = [
'type' => $attachment->type,
'name' => $attachment->name,
'content' => $attachment->getBase64()
];
}

$mandrillMessage['attachments'] = $attachmentsArray;
}

$async = false;
$ip_pool = 'Main Pool';
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is this?

$send_at = date('Y-m-d h:i:s');
$result = $mandrill->messages->send($mandrillMessage, $async, $ip_pool, $send_at);

return $result;
} catch (Mandrill_Error $e) {
$this->errors[] = 'A mandrill error occurred: ' . get_class($e) . ' - ' . $e->getMessage();
return false;
}
}

/**
* Getting list of errors
*
* @return array
*/
public function getErrors()
{
return $this->errors;
}
}