Skip to content

Commit ee6825f

Browse files
committed
added Mandrill mailer
1 parent b0a2805 commit ee6825f

3 files changed

Lines changed: 92 additions & 68 deletions

File tree

examples/form.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,22 @@
4040
] // according to Valitron doc.
4141
];
4242

43-
$mailerConfig = [
43+
// PhpMailer config.
44+
/*$mailerConfig = [
4445
'mailer' => MailHandler::USE_MANDRILL, // (or USE_POSTMARKAPP, USE_MANDRILL)
4546
'host' => 'smtp.gmail.com',
46-
'user' => 'roz-mary',
47-
'password' => '_5mPSvb39BQqnA7G_dOaAA',
47+
'user' => 'YOUR EMAIL',
48+
'password' => 'YOUR PASSWORD',
4849
'protocol' => 'tls',
4950
'port' => 587,
5051
'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.
5159
];
5260

5361
$fileManager = new FileManager([

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/MandrillMailer.php

Lines changed: 71 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use JustCoded\FormHandler\DataObjects\EmailAttachment;
77
use JustCoded\FormHandler\DataObjects\MailMessage;
88
use Mandrill;
9+
use Mandrill_Error;
910

1011
/**
1112
* Class MandrillMailer
@@ -15,115 +16,120 @@
1516
class MandrillMailer extends DataObject implements MailerInterface
1617
{
1718
/**
18-
* @var string
19+
* Attachments size limit
20+
*
21+
* @var int
1922
*/
20-
protected $host;
23+
protected $attachmentsSizeLimit = 8000000;
2124

2225
/**
26+
* User from user config
27+
*
2328
* @var string
2429
*/
2530
protected $user;
2631

2732
/**
33+
* Password from user config
34+
*
2835
* @var string
2936
*/
3037
protected $password;
3138

3239
/**
33-
* @var string|bool
34-
*/
35-
protected $protocol = false;
36-
37-
/**
38-
* @var string
39-
*/
40-
protected $port = 25;
41-
42-
/**
40+
* List of errors
41+
*
4342
* @var array
4443
*/
4544
protected $errors = array();
4645

4746
/**
48-
* Your api key
47+
* Sending form
4948
*
50-
* @var string
49+
* @param MailMessage $message Mailer message
50+
*
51+
* @return array|bool
5152
*/
52-
protected $apiKey;
53-
5453
public function send(MailMessage $message)
5554
{
56-
$mail = new Mandrill(true); // Passing `true` enables exceptions.
5755
try {
58-
// Enable SMTP if host is set.
59-
if ( ! empty($this->host)) {
60-
$mail->SMTPDebug = 0;
61-
$mail->isSMTP();
62-
$mail->Host = $this->host;
63-
$mail->Port = $this->port;
64-
if ( ! empty($this->user)) {
65-
$mail->SMTPAuth = true;
66-
$mail->Username = $this->user;
67-
$mail->Password = $this->password;
68-
}
69-
if ( ! empty($this->protocol)) {
70-
$mail->SMTPSecure = $this->protocol;
71-
}
72-
}
56+
$mandrill = new Mandrill($this->password);
7357

74-
// Set From.
75-
if ($address = $message->getFrom()) {
76-
$mail->setFrom($address->getEmail(), $address->getName());
77-
}
58+
$mandrillMessage = array(
59+
'html' => $message->getBody(),
60+
'text' => 'Example text content',
61+
'subject' => $message->getSubject(),
62+
'from_email' => $message->getFrom()->getEmail(),
63+
'from_name' => $message->getFrom()->getName(),
64+
);
7865

7966
// Recipients.
8067
if ($to = $message->getTo()) {
68+
$toArray = [];
8169
foreach ($to as $address) {
82-
$mail->addAddress($address->getEmail(), $address->getName());
70+
$toArray[] = [
71+
'email' => $address->getEmail(),
72+
'name' => $address->getName(),
73+
'type' => 'to'
74+
];
8375
}
8476

85-
}
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+
}
8686

87-
if ($cc = $message->getCc()) {
88-
foreach ($cc as $address) {
89-
$mail->addCC($address->getEmail(), $address->getName());
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+
}
9095
}
9196

97+
$mandrillMessage['to'] = $toArray;
9298
}
9399

94-
if ($bcc = $message->getBcc()) {
95-
foreach ($bcc as $address) {
96-
$mail->addBCC($address->getEmail(), $address->getName());
100+
// Attachments.
101+
if (0 < $message->getAttachmentsSize() && $message->getAttachmentsSize() < $this->attachmentsSizeLimit
102+
&& $attachments = $message->getAttachments()
103+
) {
104+
$attachmentsArray = [];
105+
foreach ($attachments as $attachment) {
106+
$attachmentsArray[] = [
107+
'type' => $attachment->type,
108+
'name' => $attachment->name,
109+
'content' => $attachment->getBase64()
110+
];
97111
}
98112

113+
$mandrillMessage['attachments'] = $attachmentsArray;
99114
}
100115

101-
//Attachments
102-
if ($attachments = $message->getFiles()) {
103-
foreach ($attachments as $attachment) {
104-
/** @var EmailAttachment $attachment */
105-
$mail->addAttachment($attachment->getPath(), $attachment->getName()); // Optional name
106-
}
107-
}
108-
109-
// Content.
110-
$mail->Subject = $message->getSubject();
111-
if ($body = $message->getBody()) {
112-
$mail->isHTML(true);
113-
$mail->Body = $message->getBody();
114-
$mail->AltBody = $message->getAltBody();
115-
} else {
116-
$mail->Body = $message->getAltBody();
117-
}
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);
118120

119-
$this->errors = array();
120-
return $mail->send();
121-
} catch (PhpMailerException $e) {
122-
$this->errors[] = 'Message could not be sent. Mailer Error: ' . $mail->ErrorInfo;
121+
return $result;
122+
} catch (Mandrill_Error $e) {
123+
$this->errors[] = 'A mandrill error occurred: ' . get_class($e) . ' - ' . $e->getMessage();
123124
return false;
124125
}
125126
}
126127

128+
/**
129+
* Getting list of errors
130+
*
131+
* @return array
132+
*/
127133
public function getErrors()
128134
{
129135
return $this->errors;

0 commit comments

Comments
 (0)